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:

  1. Creates a new CollisionNode object (terrainColl) specifically for the terrain.
  2. Adds a CollisionPolygon solid to the terrainColl object using the tight bounds of the terrain node obtained from self.scene.find('**/terrain').getTightBounds().
  3. Finally, it passes this CollisionNode object (terrainColl) to addSolid() on the sceneCollisionNode.

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.

Panda3D 碰撞检测错误修复:TypeError: CollisionNode.add_solid() argument 1 must be panda3d.core.CollisionSolid

原文地址: https://www.cveoy.top/t/topic/oj7g 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录