Panda3D 碰撞检测错误修复:TypeError: CollisionNode.add_solid() argument 1 must be panda3d.core.CollisionSolid
The error you're encountering, "TypeError: CollisionNode.add_solid() argument 1 must be panda3d.core.CollisionSolid, not panda3d.core.NodePath", arises because the addSolid() method expects a CollisionSolid object as its argument, but you're providing a NodePath object instead.
To rectify this, you need to generate a CollisionSolid object. You can achieve this by employing the find() method on the scene model to retrieve the terrain node and then feeding it to addSolid().
Replace the following line in your code:
self.sceneCollisionNode.addSolid(self.scene.find('**/terrain'))
with:
terrainColl = CollisionNode('terrain-coll')
terrainColl.addSolid(CollisionPolygon(*self.scene.find('**/terrain').getTightBounds()))
self.sceneCollisionNode.addSolid(terrainColl)
This modified code accomplishes the following:
- Creates a new
CollisionNodeobject (terrainColl) specifically for the terrain. - Adds a
CollisionPolygonsolid to theterrainCollobject using the tight bounds of the terrain node obtained fromself.scene.find('**/terrain').getTightBounds(). - Finally, it passes this
CollisionNodeobject (terrainColl) toaddSolid()on thesceneCollisionNode.
By making this change, you provide the addSolid() method with the correct object type, thereby resolving the TypeError and enabling your collision detection to function properly.
原文地址: https://www.cveoy.top/t/topic/oj7g 著作权归作者所有。请勿转载和采集!