Blitz3D+ Command Reference

EntityDistance# ( source_entity,destination_entity )

Parameters

source_entity - source entity handle

destination_entity - destination entity handle

Description

Returns the distance between two entities.

The distance is measured in world space between the two entities' positions (their origins, not the surfaces of their meshes), so two large meshes can 'touch' while their EntityDistance is still well above zero.

This is the everyday range check in a game: is the player close enough to a pickup, is an enemy within aggro range, which target is nearest? The order of the two entities makes no difference to the result.

For real contact detection use the collision system (Collisions) or picking rather than comparing distances by hand.

See also: EntityX, EntityY, EntityZ, EntityVisible.

Example

; EntityDistance Example
; ----------------------

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

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

light=CreateLight()
RotateEntity light,45,45,0

; A patrolling mine and a player ship
mine=CreateSphere(16)
ScaleEntity mine,0.5,0.5,0.5
EntityColor mine,255,0,0

player=CreateCone(16)
PositionEntity player,-4,0,0
EntityColor player,0,200,255

While Not KeyDown(1)

    ; The mine patrols up and down
    PositionEntity mine,2,Sin(MilliSecs()/10.0)*2,0

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

    ; Measure the distance between the two entities
    dist#=EntityDistance(player,mine)

    ; Proximity warning: the mine glows when the player gets close
    If dist<3
        EntityColor mine,255,255,0
    Else
        EntityColor mine,255,0,0
    EndIf

    RenderWorld

    Text 0,0,"Arrow keys: fly the ship near the mine   Esc: exit"
    Text 0,20,"EntityDistance(player,mine) = "+dist

    Flip

Wend

End

Index