Blitz3D+ Command Reference

CameraProject camera,x#,y#,z#

Parameters

camera - camera handle

x# - world x coordinate
y# - world y coordinate
z# - world z coordinate

Description

Projects a point in 3D world space onto the 2D screen, as seen by a camera.

This is the bridge from the 3D world to 2D drawing: project an entity's position, then use ProjectedX and ProjectedY as viewport coordinates to draw a name tag over a player's head, a health bar over an enemy, or a waypoint marker with the 2D Text and Image commands after RenderWorld.

The results are read with ProjectedX/Y/Z. ProjectedZ tells you whether the point was actually in front of the camera: it is 0 if the point was behind the camera or beyond the far range (in which case ProjectedX/Y are meaningless), and positive when the projection is valid - always check it before drawing, or off-screen markers will appear in mirrored positions.

The projected coordinates are relative to the camera's viewport, which for a default camera covers the whole screen. CameraPick is the reverse operation - from a screen point back into the world.

See also: ProjectedX, ProjectedY, ProjectedZ, CameraPick.

Example

; CameraProject Example
; ---------------------

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

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

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

; Textured ground plane
plane=CreatePlane()
ground_tex=LoadTexture("media/MossyGround.BMP")
ScaleTexture ground_tex,4,4
EntityTexture plane,ground_tex

; A drone that circles the scene - we will pin a HUD tag on it
drone=CreateSphere()
EntityColor drone,100,200,255

angle#=0

While Not KeyDown(1)

    ; Fly the drone in a bobbing circle
    angle=angle+1
    PositionEntity drone,Cos(angle)*6,2+Sin(angle*3),Sin(angle)*6

    ; 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

    ; Project the drone's 3D position into 2D screen coordinates
    CameraProject camera,EntityX(drone),EntityY(drone),EntityZ(drone)

    RenderWorld

    ; Draw a HUD tag at the projected screen position while the drone is on screen
    If EntityInView(drone,camera) Then
        Rect ProjectedX()-20,ProjectedY()-20,40,40,False
        Text ProjectedX()-16,ProjectedY()-36,"DRONE"
    EndIf

    Text 0,0,"Arrow keys: move camera   Esc: exit"
    Text 0,20,"CameraProject camera,x,y,z fills in the Projected values:"
    Text 0,40,"ProjectedX: "+ProjectedX#()
    Text 0,60,"ProjectedY: "+ProjectedY#()
    Text 0,80,"ProjectedZ: "+ProjectedZ#()

    Flip

Wend

End

Index