; PhysicsContactZ Example ; ----------------------- ; Requires Extended mode. Graphics3D 640,480,0,2 SetBuffer BackBuffer() Const PHYSICS_STATIC=0 Const PHYSICS_DYNAMIC=2 Const PHYSICS_CONTACT_HIT=3 camera=CreateCamera() PositionEntity camera,0,3,-6 light=CreateLight() RotateEntity light,45,-30,0 AmbientLight 50,50,50 world=CreatePhysicsWorld() PhysicsGravity world,0,-9.81,0 ; Static ground slab for the ball to bounce on ground=CreateCube() ScaleEntity ground,4,0.5,4 PositionEntity ground,0,-0.5,5,True EntityColor ground,80,160,255 ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC) ground_shape=PhysicsBodyBox(ground_body,8,1,8,0) ; Bouncy dynamic ball dropped from above ball=CreateSphere() ScaleEntity ball,0.5,0.5,0.5 PositionEntity ball,0,4,5,True EntityColor ball,255,120,70 ball_body=CreatePhysicsBody(world,ball,PHYSICS_DYNAMIC) ball_shape=PhysicsBodySphere(ball_body,1,1) PhysicsShapeRestitution ball_shape,0.7 ; Yellow marker highlights the latest contact point marker=CreateSphere(8) ScaleEntity marker,0.15,0.15,0.15 EntityColor marker,255,255,0 HideEntity marker hits=0 While Not KeyDown(1) ; Space kicks the ball back up so fresh contacts keep happening If KeyHit(57) Then PhysicsBodyImpulse ball_body,0,3,0 StepPhysics world,1.0/60.0,4 ; Read the first hit-contact snapshot recorded during this step If CountPhysicsContacts(world,PHYSICS_CONTACT_HIT)>0 hit_x#=PhysicsContactX(world,PHYSICS_CONTACT_HIT,1) hit_y#=PhysicsContactY(world,PHYSICS_CONTACT_HIT,1) hit_z#=PhysicsContactZ(world,PHYSICS_CONTACT_HIT,1) normal_x#=PhysicsContactNX(world,PHYSICS_CONTACT_HIT,1) normal_y#=PhysicsContactNY(world,PHYSICS_CONTACT_HIT,1) normal_z#=PhysicsContactNZ(world,PHYSICS_CONTACT_HIT,1) speed#=PhysicsContactSpeed(world,PHYSICS_CONTACT_HIT,1) shape_a=PhysicsContactShapeA(world,PHYSICS_CONTACT_HIT,1) shape_b=PhysicsContactShapeB(world,PHYSICS_CONTACT_HIT,1) hits=hits+1 PositionEntity marker,hit_x,hit_y,hit_z,True ShowEntity marker EndIf ; Arrow keys move the camera If KeyDown(200) Then MoveEntity camera,0,0,0.1 If KeyDown(208) Then MoveEntity camera,0,0,-0.1 If KeyDown(203) Then TurnEntity camera,0,1,0 If KeyDown(205) Then TurnEntity camera,0,-1,0 RenderWorld Text 0,0,"Arrow keys: move camera Space: kick the ball Esc: exit" Text 0,20,"PhysicsContactZ(world,3,1) = "+hit_z Text 0,40,"Hit point: "+hit_x+", "+hit_y+", "+hit_z+" Normal: "+normal_x+", "+normal_y+", "+normal_z Text 0,60,"Approach speed: "+speed+" Hit contacts seen: "+hits Flip Wend FreePhysicsWorld world End