Blitz3D+ Command Reference

CollisionEntity ( entity,collision_index )

Parameters

entity - entity handle
collision_index - index of the collision, from 1 to CountCollisions( entity )

Description

Returns the other entity involved in one of an entity's collisions.

Collision number collision_index was recorded during the last UpdateWorld, and this tells you what the entity actually touched. Both sides remember a collision: the moving entity records what it hit, and the entity that was hit records the mover - so a stationary coin can still ask who bumped into it.

Combine it with GetEntityType to sort out the reaction: loop from 1 to CountCollisions( player ), fetch each CollisionEntity, and branch on its type - pick up coins, take damage from spikes, open doors. If you only care whether a particular type was touched at all, EntityCollided does that in one call.

In debug mode an index outside the valid range is a runtime error.

See also: CountCollisions, EntityCollided, GetEntityType, CollisionX, CollisionTime.

Example

; CollisionEntity Example
; -----------------------

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

; Collision type ids
type_player=1
type_scenery=2

; NOTE: fixed overhead camera - the arrow keys steer the PLAYER, not the camera
camera=CreateCamera()
PositionEntity camera,0,13,-13
RotateEntity camera,45,0,0

light=CreateLight()
RotateEntity light,60,30,0

floor=CreatePlane()
EntityColor floor,70,110,70

; The player ball is the collision SOURCE, so it needs an ellipsoid radius
player=CreateSphere()
EntityColor player,220,60,60
PositionEntity player,0,1,0
EntityRadius player,1
EntityType player,type_player

; Two walls meeting in a corner, plus a crate
wall1=CreateCube()
ScaleEntity wall1,7,1.5,0.5
PositionEntity wall1,0,1.5,7
EntityType wall1,type_scenery

wall2=CreateCube()
ScaleEntity wall2,0.5,1.5,7
PositionEntity wall2,7,1.5,0
EntityType wall2,type_scenery

crate=CreateCube()
ScaleEntity crate,1.2,1.2,1.2
PositionEntity crate,-4,1.2,3
EntityType crate,type_scenery

; Ellipsoid-to-polygon collisions with a sliding response
Collisions type_player,type_scenery,2,2

While Not KeyDown(1)

    ; Repaint the scenery; whatever is being touched is flashed white below
    EntityColor wall1,150,150,160
    EntityColor wall2,150,150,160
    EntityColor crate,200,160,60

    ; Arrow keys steer the player ball
    If KeyDown(200) Then MoveEntity player,0,0,0.1
    If KeyDown(208) Then MoveEntity player,0,0,-0.1
    If KeyDown(203) Then MoveEntity player,-0.1,0,0
    If KeyDown(205) Then MoveEntity player,0.1,0,0

    ; UpdateWorld performs the collision checks and the slide response
    UpdateWorld

    ; Flash every entity the ball is currently touching
    For i=1 To CountCollisions(player)
        hit=CollisionEntity(player,i)
        EntityColor hit,255,255,255
    Next

    RenderWorld

    Text 0,0,"Arrow keys: drive the ball into the walls and crate   Esc: exit"
    ; CollisionEntity returns the other entity involved in each collision
    n=CountCollisions(player)
    If n=0 Then Text 0,20,"CollisionEntity: no collisions this frame - push against something"
    For i=1 To n
        hit=CollisionEntity(player,i)
        name$="?"
        If hit=wall1 Then name$="back wall"
        If hit=wall2 Then name$="side wall"
        If hit=crate Then name$="crate"
        Text 0,20*i,"CollisionEntity(player,"+i+") = "+hit+" ("+name$+")"
    Next

    Flip

Wend

End

Index