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

当我发现你的问题时,我只是想做同样的事情。感谢您的代码,它帮助我开始!

我已经设法让它工作了:)

只是备注:您使用任务,没有返回,这使任务运行一次。你应该使用过: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 回答