コード例 #1
0
ファイル: step1.py プロジェクト: nikemr/PandaProject
avatarRay = avatar.attachNewNode(CollisionNode('avatarRay'))
avatarRay.node().addSolid(raygeometry)
# this is how we tell the collision system that this ray would collide just with the floor acting as a FROM collider.
avatarRay.node().setFromCollideMask(FLOOR_MASK)
# we then exclude the ray from acting as an INTO collider
avatarRay.node().setIntoCollideMask(BitMask32.allOff())

#** This is the terrain - the egg model we load contains also the collider geometry as child
terrain = loader.loadModel("scene1")
terrain.reparentTo(render)
terrain.setCollideMask(BitMask32.allOff())
terrain.setScale(10)
# here how we tell the collision system that the terrain collider geometry is allowed to collide with the avatar ray as INTO collider
floorcollider = terrain.find("**/floor_collide")
floorcollider.node().setIntoCollideMask(FLOOR_MASK)

#** We set here the collison handler to use our avatar ray as into collider to detect the contact point along the Z axis and also which is the avatar nodepath to take control - this means to say that while we steer horizontally our avatar, changing its X and Y position from here to there, the handler will take care to set its Z position automatically, eventually simulating when falling down if we drop the avatar in middle air.
floorHandler.addCollider(avatarRay, avatar)
# the following setup will shift the floor contact hit position detection a little up. This is cause the CollisionHandlerFloor uses the avatar origin as reference to drive it and don't care to detect when it touch the floor surface with its  outer geometry. It's up to you to settle things to keep the avatar staying up the floor relative to the avatar's origin. Note though that this is not the recommended way to proceed in real applications, cause if we got different models with different shapes and origins roaming around, we'd see them not touching the floor correctly but or half-submerged into the terrain or not toching it at all, looking floating into the air. The correct way to go will be to interpose another nodepath to the avatar, shifting off this one instead. We'll see this better in step2.py snippet.
floorHandler.setOffset(1.0)

#** Now we're ready to start the collisions using the floorHandler and the ray to fire collisions
base.cTrav.addCollider(avatarRay, floorHandler)

#** Activating avatar steering function - now we're ready to go
steering = snipstuff.avatar_steer(avatar)
steering.start()

splash.destroy()
run()