EntityHit ( entity[,type][,min_speed#] )

Parameters

entity - live 3D entity

type (optional) - only report impacts with this entity type, 1 to 999; 0 reports any type (default)

min_speed# (optional) - minimum closing speed an impact must reach; 0 (default)

Description

Returns the entity hit hardest during the last UpdateWorld, or 0.

Use it to trigger impact sounds, damage and breakables. It reports the strongest qualifying physical impact of the most recent UpdateWorld, considering all native slices, including early impacts that have separated by the end. Repeated calls do not consume the result.

The world starts with a native hit threshold of 1 world unit per second; min_speed is an additional filter, so 0 does not turn resting contacts into hits. For standing, touching and sleeping contacts use EntityCollided, CountCollisions and CollisionEntity instead. EntityHit ignores ordinary support impulses and penetration correction.

The next successful update replaces the snapshot; deletion and relevant rule/type changes can invalidate affected results.

For the full story, see the entity-physics guide, or browse the compact command set.


See also: EntityCollided, CountCollisions, CollisionEntity, EntityPhysics, Physics.

Example

; EntityHit Example
; -----------------

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

; Entity physics runs on the Box3D collision backend
SetCollisionBackend 1

Const SCENERY=1
Const PROPS=2

camera=CreateCamera()
PositionEntity camera,0,3,-10
RotateEntity camera,10,0,0

light=CreateLight()
RotateEntity light,45,-25,0
AmbientLight 80,80,90

; A static floor for the crate to land on
ground=CreateCube()
ScaleEntity ground,10,0.5,10
PositionEntity ground,0,-0.5,0
EntityColor ground,65,85,100
EntityType ground,SCENERY
EntityPhysics ground,0

; A dynamic crate that falls and settles on its own
crate=CreateCube()
ScaleEntity crate,0.5,0.5,0.5
PositionEntity crate,0,4,0
RotateEntity crate,0,0,0
EntityColor crate,235,155,65
EntityType crate,PROPS
EntityPhysics crate

; Props collide physically with the scenery
Physics PROPS,SCENERY

; A bouncy crate lands several times
EntityBounce crate,0.6
min_speed#=1
impacts=0
last_hit=0

timer=CreateTimer(60)

While Not KeyDown(1)

    ; [ / ] change the minimum impact speed that counts; Space tosses the crate
    If KeyHit(26) And min_speed>0 Then min_speed=min_speed-0.5
    If KeyHit(27) Then min_speed=min_speed+0.5
    If KeyHit(57) Then EntityImpulse crate,0,5,0

    ; R drops the crate again
    If KeyHit(19)
        PositionEntity crate,0,4,0
        RotateEntity crate,0,0,0
        ResetEntity crate
    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

    UpdateWorld

    ; The strongest impact of this update, or 0
    last_hit=EntityHit(crate,SCENERY,min_speed)
    If last_hit Then impacts=impacts+1

    RenderWorld

    Text 0,0,"Arrow keys: move camera   Space: toss   [ / ] : min speed   R: drop again   Esc: exit"
    Text 0,20,"EntityHit(crate,SCENERY,"+min_speed+") hit the floor this update: "+(last_hit=ground)
    Text 0,40,"Impacts counted: "+impacts+"   (resting on the floor adds none)"

    Flip
    WaitTimer timer

Wend

End

Index