Blitz3D+ Command Reference

EntityPick ( entity,range# )

Parameters

entity - entity handle

range# - maximum distance of the pick, in world units

Description

Casts a ray forward from an entity and returns the nearest pickable entity 'ahead' of it, or 0 if there is none within range.

The ray starts at the entity's position and travels along its z axis - the direction the entity is facing - for up to range units. Point a camera or a gun at something and EntityPick tells you what it is looking at: ideal for crosshair targeting, 'press E to use' prompts, and checking what an NPC is facing.

An entity must have a non-zero EntityPickMode to be pickable. The source entity itself is not picked, but anything pickable in the way is - including friendly scenery.

After a successful pick, the Picked commands hold the details: PickedX/Y/Z for the point hit, PickedNX/NY/NZ for the normal, PickedEntity and friends for what was hit.

See also: CameraPick, LinePick, EntityPickMode, PickedEntity.

Example

; EntityPick Example
; ------------------

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

camera=CreateCamera()
PositionEntity camera,0,8,-14
RotateEntity camera,25,0,0

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

floor=CreatePlane()
EntityColor floor,60,90,60

; Turret in the centre - EntityPick looks straight down its z axis
turret=CreateCube()
ScaleEntity turret,0.7,0.7,2
PositionEntity turret,0,1,0
EntityColor turret,130,130,150

; Three drones orbit the turret - EntityPickMode 2 makes them pickable
drone1=CreateSphere()
EntityPickMode drone1,2

drone2=CreateSphere()
EntityPickMode drone2,2

drone3=CreateSphere()
EntityPickMode drone3,2

orbit#=0

While Not KeyDown(1)

    ; March the drones round their orbit
    orbit=orbit+1
    PositionEntity drone1,Sin(orbit)*7,1,Cos(orbit)*7
    PositionEntity drone2,Sin(orbit+120)*7,1,Cos(orbit+120)*7
    PositionEntity drone3,Sin(orbit+240)*7,1,Cos(orbit+240)*7

    ; A/D keys aim the turret
    If KeyDown(30) Then TurnEntity turret,0,2,0
    If KeyDown(32) Then TurnEntity turret,0,-2,0

    ; EntityPick: which pickable entity is directly ahead of the turret?
    target=EntityPick(turret,20)

    ; Repaint the drones, then flash the one in the turret's line of sight
    EntityColor drone1,60,160,220
    EntityColor drone2,60,160,220
    EntityColor drone3,60,160,220
    If target<>0 Then EntityColor target,255,255,60

    ; 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,"A/D: aim the turret   Arrow keys: move camera   Esc: exit"
    If target<>0
        Text 0,20,"EntityPick(turret,20) = "+target+" - target locked!"
    Else
        Text 0,20,"EntityPick(turret,20) = 0 (no drone in the line of sight)"
    EndIf

    Flip

Wend

End

Index