0

ロードされたモデルをクリックして移動したい。チェスのサンプルの例とパンダの 3d チュートリアルのコードを使用しましたが、成功しませんでした。誰かがコードの何が問題なのかを理解できますか。

ありがとう

from math import pi, sin, cos
import sys
from direct.showbase.ShowBase import ShowBase
import direct.directbase.DirectStart
from direct.task import Task
from panda3d.core import TextNode
from direct.gui.OnscreenText import OnscreenText
from panda3d.core import CollisionTraverser, CollisionNode
from panda3d.core import CollisionHandlerQueue, CollisionRay
from panda3d.core import Point3, Vec3, Vec4, BitMask32
from direct.showbase.DirectObject import DirectObject
from panda3d.core import AmbientLight, DirectionalLight, LightAttrib


class MyApp(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        # quit when esc is pressed
        self.accept('escape', sys.exit)

        #base.disableMouse()

        # load the box model
        self.box = self.loader.loadModel("models/xbox")
        self.box.reparentTo(camera)
        self.box.setScale(2.0, 2.0, 2.0)
        self.box.setPos(8, 50, 0)

        self.keyMap = {
            "w" :False ,
            "s" :False,
            "a": False,
            "d": False,
            "mouse1": False,
            "mouse3": False,
        }

        # CollisionTraverser  and a Collision Handler is set up
        self.picker = CollisionTraverser()
        self.pq = CollisionHandlerQueue() 

        self.pickerNode = CollisionNode('mouseRay')
        self.pickerNP = camera.attachNewNode(self.pickerNode)
        self.pickerNode.setFromCollideMask(BitMask32.bit(1))
        self.box.setCollideMask(BitMask32.bit(1)) 
        
        self.pickerRay = CollisionRay()
        self.pickerNode.addSolid(self.pickerRay)
        self.picker.addCollider(self.pickerNP, self.pq)

        self.mouseTask = taskMgr.add(self.mouseTask, 'mouseTask')
        self.accept("mouse1", self.setKey, ["mouse1", True])
        

    def mouseTask(self,task):
        # check if we have access to the mouse
        if base.mouseWatcherNode.hasMouse():

            # get the mouse position
            mpos = base.mouseWatcherNode.getMouse()

            # set the position of the ray based on the mouse position
            self.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())
            self.picker.traverse(render)
            # if we have hit something sort the hits so that the closest is first and highlight the node
            if self.pq.getNumEntries() > 0:
                self.pq.sortEntries()
                pickedObj = self.picker.getEntry(0).getIntoNodePath()
    

    def setKey(self,key,value):
        self.keyMap[key] = value
 
   
app = MyApp()
app.run()
4

1 に答える 1

2

あなたの質問を見つけたとき、私はちょうど同じことをしようとしていました。あなたのコードをありがとう、それは私が始めるのに役立ちます!

私はそれを機能させることができました:)

コメント: リターンなしでタスクを使用すると、タスクが 1 回実行されます。使用する必要がありました: return task.cont

とにかく、ここで panda3d devel (1.8.0+) の作業コード:

import sys
from direct.showbase.ShowBase import ShowBase
from pandac.PandaModules import *


class MyApp(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)


        # quit when esc is pressed
        self.accept('escape',sys.exit)

        #base.disableMouse()


        # load the box model
        box = self.loader.loadModel("models/box")
        box.reparentTo(render)
        box.setScale(2.0, 2.0, 2.0)
        box.setPos(8, 50, 0)

        panda = base.loader.loadModel("models/panda")
        panda.reparentTo(render)
        panda.setPos(0, 10, 0)
        panda.setScale(0.1, 0.1, 0.1)
        cNodePanda = panda.attachNewNode(CollisionNode('cnode_panda'))
        cNodePanda.node().addSolid(CollisionSphere(0,0,5,5))
        cNodePanda.show()

        # CollisionTraverser  and a Collision Handler is set up
        self.picker = CollisionTraverser()
        self.picker.showCollisions(render)
        self.pq = CollisionHandlerQueue() 

        self.pickerNode = CollisionNode('mouseRay')
        self.pickerNP = camera.attachNewNode(self.pickerNode)
        self.pickerNode.setFromCollideMask(BitMask32.bit(1))
        box.setCollideMask(BitMask32.bit(1)) 
        panda.setCollideMask(BitMask32.bit(1))

        self.pickerRay = CollisionRay()
        self.pickerNode.addSolid(self.pickerRay)
        self.picker.addCollider(self.pickerNP,self.pq)

        self.accept("mouse1",self.mouseClick)


    def mouseClick(self):
        print('mouse click')
        # check if we have access to the mouse
        if base.mouseWatcherNode.hasMouse():

            # get the mouse position
            mpos = base.mouseWatcherNode.getMouse()

            # set the position of the ray based on the mouse position
            self.pickerRay.setFromLens(base.camNode,mpos.getX(),mpos.getY())
            self.picker.traverse(render)
            # if we have hit something sort the hits so that the closest is first and highlight the node
            if self.pq.getNumEntries() > 0:
                self.pq.sortEntries()
                pickedObj = self.pq.getEntry(0).getIntoNodePath()
                print('click on ' + pickedObj.getName())

app = MyApp()
app.run()
于 2012-07-05T15:40:44.220 に答える