Obscurer demo with viewangle

ðŸ‘‰ BlitzCoder will be building a new platform and other plans to preserve and continue the Blitz legacy.

To be able to achieve this goal, we need your support by becoming a Patreon Paid Member ðŸ‘ˆ

Tweet blitz3d blitzbasic code-archives maths

A basic example showing how to calculate if entities can see each other in Blitz3D.

Author: Jeppe Nielsen

``````;Obscurer example by Jeppe Nielsen 2004
;[email protected]

Graphics3D 800,600,16,2

viewangle#=45

CreateLight(2)

cam=CreateCamera()
PositionEntity cam,0,40,0
RotateEntity cam,90,0,0
CameraZoom cam,2

obj1=CreateCone()
RotateMesh obj1,-90,0,0

view=CreateCone(16,1,obj1)

RotateMesh view,-90,0,0
PositionMesh view,0,0,1
ScaleMesh view,0.5,0.5,0.5
EntityColor view,128,128,0
EntityAlpha view,0.8
UpdateViewCone(view,10,viewangle#)

obj2=CreateCube()

cube1=CreateCube()
EntityColor cube1,255,255,0
EntityPickMode cube1,3

cube2=CreateCube()
EntityColor cube2,255,255,0
EntityPickMode cube2,3

cube3=CreateCube()
EntityColor cube3,255,255,0
EntityPickMode cube3,3

Repeat

If KeyDown(203)

&#x9;TurnEntity obj1,0,2,0

EndIf

If KeyDown(205)

&#x9;TurnEntity obj1,0,-2,0

EndIf

If KeyDown(78)

&#x9;viewangle#=viewangle#+1
&#x9;If viewangle#&#x3E;179
&#x9;&#x9;viewangle#=179
&#x9;EndIf
&#x9;UpdateViewCone(view,10,viewangle#)&#x9;

EndIf

If KeyDown(74)

&#x9;viewangle#=viewangle#-1
&#x9;If viewangle#&#x3C;15
&#x9;&#x9;viewangle#=15
&#x9;EndIf
&#x9;UpdateViewCone(view,10,viewangle#)&#x9;

EndIf

an#=MilliSecs()/20
PositionEntity obj2,Sin(an#)*10,0,Cos(an#)*10

an#=MilliSecs()/50
PositionEntity cube1,Sin(an#)*6,0,Cos(an#)*6

PositionEntity cube2,Sin(an#+120)*6,0,Cos(an#+120)*6
PositionEntity cube3,Sin(an#+240)*6,0,Cos(an#+240)*6

RenderWorld

If CanSeeObject(obj1,obj2,viewangle)

&#x9;Color 255,255,255
&#x9;Text 400,200,&#x22;I see it :)&#x22;,1
&#x9;CameraProject cam,EntityX(obj1),EntityY(obj1),EntityZ(obj1)
&#x9;x1=ProjectedX()
&#x9;y1=ProjectedY()
&#x9;CameraProject cam,EntityX(obj2),EntityY(obj2),EntityZ(obj2)
&#x9;x2=ProjectedX()
&#x9;y2=ProjectedY()&#x9;
&#x9;Color Rnd(255),Rnd(255),Rnd(255)
&#x9;Line x1,y1,x2,y2
&#x9;

EndIf

Color 255,255,255
Text 400,10,&#x22;Left/right to rotate observer&#x22;,1
Text 400,30,&#x22;+ / - to change view angle&#x22;,1
Text 400,50,&#x22;Yellow boxes obscurers the view&#x22;,1
Text 400,70,&#x22;View angle : &#x22;+viewangle,1

Flip

Until KeyDown(1)
End

Function UpdateViewCone(cone,depth#,angle#)
&#x9;
&#x9;sc#=Tan(angle/2)*depth*2
&#x9;
&#x9;ScaleEntity cone,sc,sc,depth#
&#x9;
End Function

Function CanSeeObject(obj1,obj2,angle#=90)

dist#=EntityDistance(obj1,obj2)
dx#=(EntityX(obj2,1)-EntityX(obj1,1)) / dist
dy#=(EntityY(obj2,1)-EntityY(obj1,1)) / dist
dz#=(EntityZ(obj2,1)-EntityZ(obj1,1)) / dist

TFormVector 0,0,1,obj1,0

;dot product:
dot#=dx*TFormedX()+dy*TFormedY()+dz*TFormedZ()

If ACos(dot#)&#x3C;angle/2
&#x9;
&#x9;Return EntityVisible(obj1,obj2)
&#x9;
EndIf

End Function``````