【发布时间】:2020-10-07 01:20:09
【问题描述】:
我试图按照 Panda3D 的文档了解他们的 Bullet Physics。到目前为止,他们的教程一开始效果很好,所以我想试试他们的代码示例character movement。当我将他们的代码复制并修改到我的工作区时,事情并不顺利。使用语句“self.attachNewNode(playerNode)”,它告诉我一开始,self 没有定义。我决定删除它并得到一个错误,说没有定义 worldNP。
在调查worldNP后,我发现Panda3D的文档中没有手动声明。似乎 self.worldNP 在 Panda3D 中是预定义的,类似于它们的基类。我相信我可能错过了某种导入语句,并且有一个使用 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 上出现问题的罪魁祸首。
【问题讨论】:
标签: python class methods 3d panda3d