Blitz3D+ Command Reference

PhysicsContactSpeed# ( world,event_type,index )

Parameters

world - physics-world handle returned by CreatePhysicsWorld

event_type - contact list to read; only 3 (hit) carries a speed

index - 1-based position in that list, up to CountPhysicsContacts

Description

Returns the approach speed of a hit contact, in metres per second.

This is how fast the two shapes were closing on each other at the moment of impact - always positive. It is the natural volume knob for impact sounds and the right number to scale damage, camera shake or debris count, so a gentle bump and a high dive feel different.

Hit events only exist for impacts faster than about 1 metre per second, so every entry in the hit list is worth reacting to. Asking a begin or end event for a speed raises a runtime error, and the event lists are replaced by every StepPhysics call.

Requires Extended mode.

See also: CountPhysicsContacts, PhysicsContactShapeA, PhysicsContactX, PhysicsContactNX, StepPhysics.

Example

; PhysicsContactSpeed 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,"PhysicsContactSpeed(world,3,1) = "+speed
    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

Index