Blitz3D+ Command Reference

CountPhysicsSensorEvents ( world,event_type )

Parameters

world - physics-world handle returned by CreatePhysicsWorld

event_type - which sensor list to count:
1: begin - a shape entered a sensor this step
2: end - a shape left a sensor

Description

Returns how many sensor overlap events the last physics step produced.

Sensors are shapes created with the sensor flag of PhysicsBodyBox and friends: invisible trigger volumes that never push or stop anything, they just notice who wanders in and out. Checkpoints, pickup zones, pressure plates, kill volumes and door openers are all one sensor each.

Loop index from 1 to this count and read each event with PhysicsSensorShape (the trigger) and PhysicsSensorVisitor (the shape that entered or left). Events fire once per change, not continuously - if you need to know who is currently inside, flip your own flag on begin and clear it on end.

Like contact events, sensor events are a snapshot captured by each StepPhysics call and replaced by the next one.

Requires Extended mode.

See also: PhysicsSensorShape, PhysicsSensorVisitor, PhysicsBodyBox, StepPhysics.

Example

; CountPhysicsSensorEvents Example
; --------------------------------
; Requires Extended mode.

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

Const PHYSICS_STATIC=0
Const PHYSICS_KINEMATIC=1
Const PHYSICS_SENSOR_BEGIN=1
Const PHYSICS_SENSOR_END=2

camera=CreateCamera()
PositionEntity camera,0,2,-9

light=CreateLight()
RotateEntity light,45,-30,0
AmbientLight 50,50,50

; Zero gravity so the patrolling visitor is not pulled down
world=CreatePhysicsWorld()
PhysicsGravity world,0,0,0

; Transparent blue trigger zone - the final True makes the box a sensor
zone=CreateCube()
ScaleEntity zone,1.5,1.5,1.5
PositionEntity zone,0,0,0,True
EntityColor zone,80,160,255
EntityAlpha zone,0.35
zone_body=CreatePhysicsBody(world,zone,PHYSICS_STATIC)
zone_shape=PhysicsBodyBox(zone_body,3,3,3,0,0,0,0,True)

; Kinematic visitor sphere driven by PositionEntity each frame
visitor=CreateSphere()
ScaleEntity visitor,0.5,0.5,0.5
PositionEntity visitor,-4,0,0,True
EntityColor visitor,255,120,70
visitor_body=CreatePhysicsBody(world,visitor,PHYSICS_KINEMATIC)
visitor_shape=PhysicsBodySphere(visitor_body,1,1)

angle#=0
paused=False
begin_total=0
end_total=0
inside=0

While Not KeyDown(1)

    ; Space pauses and resumes the patrol
    If KeyHit(57) Then paused=Not paused

    ; Sweep the visitor back and forth through the zone
    If Not paused Then angle=angle+1.2
    PositionEntity visitor,Sin(angle)*4,0,0,True

    ; Advance the Box3D simulation by one 60 Hz step
    StepPhysics world,1.0/60.0,4

    ; Count this step's sensor overlap events
    begin_count=CountPhysicsSensorEvents(world,PHYSICS_SENSOR_BEGIN)
    end_count=CountPhysicsSensorEvents(world,PHYSICS_SENSOR_END)
    begin_total=begin_total+begin_count
    end_total=end_total+end_count
    inside=inside+begin_count-end_count

    ; Light the zone up while the visitor is inside
    If inside>0 Then EntityColor zone,120,255,120 Else EntityColor zone,80,160,255

    ; 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: pause patrol   Esc: exit"
    Text 0,20,"This step - begin: "+begin_count+"   end: "+end_count
    Text 0,40,"Totals - begin: "+begin_total+"   end: "+end_total+"   Inside now: "+inside

    Flip

Wend

FreePhysicsWorld world
FreeEntity visitor
FreeEntity zone
End

Index