November 28, 2020, 01:34:46 PM

Author Topic: [bb] Obscurer example - With viewangle by Jeppe Nielsen [ 1+ years ago ]  (Read 1145 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Obscurer example - With viewangle
Author : Jeppe Nielsen
Posted : 1+ years ago

Description : An example showing how to calculate if entities can see each other

Code :
Code: BlitzBasic
  1. ;Obscurer example by Jeppe Nielsen 2004
  2. ;nielsen_jeppe@hotmail.com
  3.  
  4. Graphics3D 800,600,16,2
  5.  
  6. viewangle#=45
  7.  
  8. CreateLight(2)
  9.  
  10. cam=CreateCamera()
  11. PositionEntity cam,0,40,0
  12. RotateEntity cam,90,0,0
  13. CameraZoom cam,2
  14.  
  15. obj1=CreateCone()
  16. RotateMesh obj1,-90,0,0
  17.  
  18. view=CreateCone(16,1,obj1)
  19.  
  20. RotateMesh view,-90,0,0
  21. PositionMesh view,0,0,1
  22. ScaleMesh view,0.5,0.5,0.5
  23. EntityColor view,128,128,0
  24. EntityAlpha view,0.8
  25. UpdateViewCone(view,10,viewangle#)
  26.  
  27. obj2=CreateCube()
  28.  
  29. cube1=CreateCube()
  30. EntityColor cube1,255,255,0
  31. EntityPickMode cube1,3
  32.  
  33. cube2=CreateCube()
  34. EntityColor cube2,255,255,0
  35. EntityPickMode cube2,3
  36.  
  37. cube3=CreateCube()
  38. EntityColor cube3,255,255,0
  39. EntityPickMode cube3,3
  40.  
  41.  
  42. Repeat
  43.  
  44. If KeyDown(203)
  45.  
  46.         TurnEntity obj1,0,2,0
  47.  
  48. EndIf
  49.  
  50. If KeyDown(205)
  51.  
  52.         TurnEntity obj1,0,-2,0
  53.  
  54. EndIf
  55.  
  56. If KeyDown(78)
  57.  
  58.         viewangle#=viewangle#+1
  59.         If viewangle#>179
  60.                 viewangle#=179
  61.         EndIf
  62.         UpdateViewCone(view,10,viewangle#)     
  63.  
  64. EndIf
  65.  
  66. If KeyDown(74)
  67.  
  68.         viewangle#=viewangle#-1
  69.         If viewangle#<15
  70.                 viewangle#=15
  71.         EndIf
  72.         UpdateViewCone(view,10,viewangle#)     
  73.  
  74. EndIf
  75.  
  76. an#=MilliSecs()/20
  77. PositionEntity obj2,Sin(an#)*10,0,Cos(an#)*10
  78.  
  79. an#=MilliSecs()/50
  80. PositionEntity cube1,Sin(an#)*6,0,Cos(an#)*6
  81.  
  82. PositionEntity cube2,Sin(an#+120)*6,0,Cos(an#+120)*6
  83. PositionEntity cube3,Sin(an#+240)*6,0,Cos(an#+240)*6
  84.  
  85. RenderWorld
  86.  
  87. If CanSeeObject(obj1,obj2,viewangle)
  88.  
  89.         Color 255,255,255
  90.         Text 400,200,"I see it :)",1
  91.         CameraProject cam,EntityX(obj1),EntityY(obj1),EntityZ(obj1)
  92.         x1=ProjectedX()
  93.         y1=ProjectedY()
  94.         CameraProject cam,EntityX(obj2),EntityY(obj2),EntityZ(obj2)
  95.         x2=ProjectedX()
  96.         y2=ProjectedY()
  97.         Color Rnd(255),Rnd(255),Rnd(255)
  98.         Line x1,y1,x2,y2
  99.        
  100.  
  101. EndIf
  102.  
  103. Color 255,255,255
  104. Text 400,10,"Left/right to rotate observer",1
  105. Text 400,30,"+ / - to change view angle",1
  106. Text 400,50,"Yellow boxes obscurers the view",1
  107. Text 400,70,"View angle : "+viewangle,1
  108.  
  109. Flip
  110.  
  111. Until KeyDown(1)
  112. End
  113.  
  114. Function UpdateViewCone(cone,depth#,angle#)
  115.        
  116.         sc#=Tan(angle/2)*depth*2
  117.        
  118.         ScaleEntity cone,sc,sc,depth#
  119.        
  120. End Function
  121.  
  122. Function CanSeeObject(obj1,obj2,angle#=90)
  123.  
  124. dist#=EntityDistance(obj1,obj2)
  125. dx#=(EntityX(obj2,1)-EntityX(obj1,1)) / dist
  126. dy#=(EntityY(obj2,1)-EntityY(obj1,1)) / dist
  127. dz#=(EntityZ(obj2,1)-EntityZ(obj1,1)) / dist
  128.  
  129. TFormVector 0,0,1,obj1,0
  130.  
  131. ;dot product:
  132. dot#=dx*TFormedX()+dy*TFormedY()+dz*TFormedZ()
  133.  
  134. If ACos(dot#)<angle/2
  135.        
  136.         Return EntityVisible(obj1,obj2)
  137.        
  138. EndIf
  139.  
  140. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal