Blitz3D+ Command Reference

CountPhysicsContacts ( world,event_type )

Parameters

world - physics-world handle returned by CreatePhysicsWorld

event_type - which contact list to count:
1: begin - pairs of shapes that started touching this step
2: end - pairs that stopped touching
3: hit - impacts faster than about 1 metre per second

Description

Returns how many contact events the last physics step produced.

Every StepPhysics call captures fresh begin, end and hit lists, replacing the previous ones - so read the counts and details after stepping and before you step again. Loop index from 1 to this count and pull the details with PhysicsContactShapeA and PhysicsContactShapeB; hit events additionally carry a contact point, normal and approach speed.

Begin and end events fire once per change, which makes them ideal for state ("the crate is on the pressure plate") without polling. Hit events only exist for solid impacts above the speed threshold - exactly the ones that deserve a thud sound or a puff of dust, and slow grazes never appear in the hit list.

Sensor shapes never show up here; their overlaps are reported through CountPhysicsSensorEvents instead.

Requires Extended mode.

See also: PhysicsContactShapeA, PhysicsContactShapeB, PhysicsContactX, PhysicsContactNX, PhysicsContactSpeed, StepPhysics.

Example

; 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

Index