Camera position in spotlight cone

This topic is 2405 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am using DX9 api for my hobby graphics projects. To cut down number of pixels proessed i need to find if my camera position is inside cone (spotlight volume) to be able to switch face culling to/from CW/CCW. I have used google but didn't find satisfactory answers so i came here in hope that someone will help me.Thanks for your time.

Share on other sites
First, compute the distance from the camera to the spotlight. If this distance is greater than the range of the spotlight, then you are not in the cone.

Second, take that Vector that you used for the distance, and dot it with the facing vector of the spotlight. If that value is LESS than the cosine of the angle of the cone, you are OUTSIDE the cone. Else you are in.

 bool IsCameraInSpotlightCone { Vector toCamera = Camera.Position - Spotlight.Position; float dist = toCamera.Length(); if (dist > Spotlight.Range) { return false; } toCamera /= dist; float cosAngle = dot(toCamera, Spotlight.Direction); if (cosAngle < Spotlight.cosAngle) { return false; } return true; } 

Share on other sites
Looks like i am missing spotlight cosAngle to be able to test that. I have radius for cap, position, range and direction. Could you please help me how to calculate cone angle given other info i have.

Share on other sites
By 'radius for cap' I assume you mean the width of the light at the maximum range?
If so, the angle of the cone is simply given by our old nemesis, Trigonometry.

Tan(angle) = Opposite / Adjacent. In our case, that translates to Tan(angle) = CapRadius/Range

So to get our cosAngle, we do
 float angle = atan(Spotlight.CapRadius / Spotlight.Range); Spotlight.cosAngle = cos(angle); 

I recommend just calculating that value once for each spotlight and storing it.

Share on other sites
Thats it !!! Thank you, thank you...

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 13
• 14
• 9
• Forum Statistics

• Total Topics
633365
• Total Posts
3011520
• Who's Online (See full list)

There are no registered users currently online

×