Advertisement
FocusedWolf

IsSphereInFrustum

Jul 3rd, 2011
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. local nearD = 0 //the real distance of nearD bounces around 3e-7. Can use 0 here
  2. local farD = 100000000000 //the real distance of farD is like 1e12. Can use a preferred distance.
  3. //a radius of 0.63 will create a sphere that is approximately a marines waist
  4. function IsSphereInFrustum(center,radius)
  5.     //http://www.ijvr.org/issues/issue3-2008/7.pdf
  6.     //http://www.lighthouse3d.com/tutorials/view-frustum-culling/radar-approach-testing-points/
  7.     //http://www.lighthouse3d.com/tutorials/view-frustum-culling/radar-approach-implementation/
  8.     //game programming gems 5 under the title "Improved Frustum Culling"
  9.    
  10.     local player = Client.GetLocalPlayer()
  11.  
  12.  //todo: can be moved out of function so it's not re-calculated for each point tested
  13.     //local cameraCoords = player:GetCameraViewCoords() //more precise...more costly
  14.     //local cameraOrigin = cameraCoords.origin
  15.     local cameraCoords = player:GetViewAngles():GetCoords()    
  16.     local cameraOrigin = player:GetEyePos()
  17.      
  18.  //todo: these only needs to be calcuated during resolution change
  19.     local rFactor = math.tan(player:GetRenderFov() / 2)
  20.     local uFactor = rFactor / GetAspectRatio() //this approach came from game programming gems 5
  21.  
  22.     local v = center - cameraOrigin
  23.  
  24.     // compute and test the Z coordinate
  25.     local P_z = Math.DotProduct(v, cameraCoords.zAxis)
  26.     if P_z < (nearD - radius) or (farD + radius) < P_z then
  27.         return false //behind near-plane or in front of far-plane
  28.     end
  29.  
  30.     // compute and test the X coordinate
  31.     local P_x = Math.DotProduct(v, cameraCoords.xAxis)
  32.     local limit = (rFactor * P_z) + radius //half width of frustum at distance P_z from user
  33.     if P_x < -limit or limit < P_x then
  34.         return false //left of left-plane or right of right-plane
  35.     end
  36.  
  37.     // compute and test the Y coordinate
  38.     local P_y = Math.DotProduct(v, cameraCoords.yAxis)
  39.     limit = (uFactor * P_z) + radius
  40.     if P_y < -limit or limit < P_y then
  41.         return false //above top-plane or below bottom-plane
  42.     end
  43.  
  44.     return true //is visible
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement