使用panda3d实现第三人称带碰撞的相机,给出代码。
时间: 2023-03-15 09:15:10 浏览: 255
我不清楚要求的代码,但是提供一个使用panda3d实现第三人称带碰撞的相机的参考代码:from direct.showbase.ShowBase import ShowBase
from panda3d.core import CollisionTraverser,CollisionNode
from panda3d.core import CollisionHandlerQueue,CollisionRay
from panda3d.core import Filename,AmbientLight,DirectionalLight
from panda3d.core import PandaNode,NodePath,Camera,TextNode
from panda3d.core import Vec3,Vec4,BitMask32class World(ShowBase):
def __init__(self):
ShowBase.__init__(self) # Load the environment model.
self.environ = self.loader.loadModel("environment")
self.environ.reparentTo(self.render)
self.environ.setPos(0, 0, 0) # Create the main character.
self.player = self.loader.loadModel("panda")
self.player.setPos(0, 0, 0)
self.player.reparentTo(self.render) # Create the camera
self.thirdPersonCamera = self.makeCamera()
self.thirdPersonCamera.reparentTo(self.player)
self.thirdPersonCamera.setZ(2.0)
self.thirdPersonCamera.setY(-5.0) # Set up the collisions
self.cTrav = CollisionTraverser()
self.pusher = CollisionHandlerPusher()
self.pusher.setInPattern("%fn-into-%in") self.cTrav.addCollider(self.player, self.pusher) # Make the camera collide
self.camGroundRay = CollisionRay()
self.camGroundRay.setOrigin(0, 0, 100)
self.camGroundRay.setDirection(0, 0, -1)
self.camGroundCol = CollisionNode('camRay')
self.camGroundCol.addSolid(self.camGroundRay)
self.camGroundCol.setFromCollideMask(BitMask32.bit(0))
self.camGroundCol.setIntoCollideMask(BitMask32.allOff())
self.camGroundColNp = self.player.attachNewNode(self.camGroundCol)
self.camGroundHandler = CollisionHandlerQueue()
self.cTrav.addCollider(self.camGroundColNp, self.camGroundHandler) def makeCamera(self):
self.camera = base.makeCamera(base.win, lens=base.cam.node().getLens())
self.camera.reparentTo(self.player)
self.camera.setPos(0, 0, 2.0)
self.camera.setHpr(0, -90, 0)
base.cam.node().setCameraMask(BitMask32.bit(1))
return self.camera def updateCamera(self, task):
if self.camGroundHandler.getNumEntries() > 0:
self.camGroundHandler.sortEntries()
entry = self.camGroundHandler.getEntry(0)
pos = entry.getSurfacePoint(render)
self.thirdPersonCamera.setZ(pos.getZ() + 1.0)
return task.cont
阅读全文