; CountPhysicsContacts Example ; ---------------------------- ; Requires Extended mode. Graphics3D 640,480,0,2 SetBuffer BackBuffer() Const PHYSICS_STATIC=0 Const PHYSICS_DYNAMIC=2 Const PHYSICS_CONTACT_BEGIN=1 Const PHYSICS_CONTACT_END=2 Const PHYSICS_CONTACT_HIT=3 camera=CreateCamera() PositionEntity camera,0,3,-9 light=CreateLight() RotateEntity light,45,-30,0 AmbientLight 50,50,50 ; Independent Box3D world with Earth-like gravity world=CreatePhysicsWorld() PhysicsGravity world,0,-9.81,0 ; Blue static ground slab for the ball to hit ground=CreateCube() ScaleEntity ground,4,0.5,4 PositionEntity ground,0,-0.5,0,True EntityColor ground,80,160,255 ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC) ground_shape=PhysicsBodyBox(ground_body,8,1,8,0) ; Bouncy orange ball dropped from above ball=CreateSphere() ScaleEntity ball,0.5,0.5,0.5 PositionEntity ball,0,4,0,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 begin_total=0 end_total=0 hit_total=0 While Not KeyDown(1) ; Space throws the ball back up for more impacts If KeyHit(57) Then PhysicsBodyVelocity ball_body,0,7,0 ; Advance the Box3D simulation by one 60 Hz step StepPhysics world,1.0/60.0,4 ; Count this step's contact snapshots of each type begin_count=CountPhysicsContacts(world,PHYSICS_CONTACT_BEGIN) end_count=CountPhysicsContacts(world,PHYSICS_CONTACT_END) hit_count=CountPhysicsContacts(world,PHYSICS_CONTACT_HIT) begin_total=begin_total+begin_count end_total=end_total+end_count hit_total=hit_total+hit_count ; 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: throw ball up Esc: exit" Text 0,20,"This step - begin: "+begin_count+" end: "+end_count+" hit: "+hit_count Text 0,40,"Totals - begin: "+begin_total+" end: "+end_total+" hit: "+hit_total Flip Wend FreePhysicsWorld world FreeEntity ball FreeEntity ground End