我一直在尝试将Panda3D的文档应用于他们的Bullet Physics。到目前为止,他们的教程起初效果很好,所以我想尝试一下character movement的代码示例。当我将他们的代码复制并修改到我的工作区时,一切进展不顺利。使用语句“ self.attachNewNode(playerNode)”,它告诉我起初未定义self。我决定将其删除,并得到一条错误消息,指出未定义worldNP。
在研究worldNP时,我发现Panda3D的文档中没有进行任何手动声明。似乎self.worldNP是在Panda3D中预定义的,类似于它们的基类。我相信我可能已经错过了某种import语句,并且将一个用worldNP初始化的对象作为其构造函数的一部分。此外,their mentioning from the manual is only towards Panda3D's Bullets Physics.
关于代码,据我所知:
import direct.directbase.DirectStart
from panda3d.core import Vec3
from panda3d.bullet import BulletWorld
from panda3d.bullet import BulletPlaneShape
from panda3d.bullet import BulletRigidBodyNode
from panda3d.bullet import BulletBoxShape
from panda3d.bullet import BulletCharacterControllerNode
from panda3d.bullet import BulletCapsuleShape
from panda3d.bullet import ZUp
base.cam.setPos(0, -10, 0)
base.cam.lookAt(0, 0, 0)
# World
world = BulletWorld()
world.setGravity(Vec3(0, 0, -9.81))
height = 1.75
radius = 0.4
shape = BulletCapsuleShape(radius, height - 2*radius, ZUp)
playerNode = BulletCharacterControllerNode(shape, 0.4, 'Player')
playerNP = self.worldNP.attachNewNode(playerNode) # This is where self.worldNP is being mentioned
playerNP.setPos(-2, 0, 14)
playerNP.setH(45)
playerNP.setCollideMask(BitMask32.allOn())
world.attachCharacter(playerNP.node())
def update(task):
dt = globalClock.getDt()
world.doPhysics(dt)
world.doPhysics(dt)
return task.cont
taskMgr.add(update, 'update')
base.run()
注意:我了解不推荐使用“ import direct.directbase.DirectStart”。虽然,我不认为这是我在worldNP上遇到问题的罪魁祸首。