Camera position in spotlight cone

Started by
3 comments, last by belfegor 12 years ago
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.
Advertisement
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;
}
Waramp.Before you insult a man, walk a mile in his shoes.That way, when you do insult him, you'll be a mile away, and you'll have his shoes.
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.

Thanks for your time.
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.
Waramp.Before you insult a man, walk a mile in his shoes.That way, when you do insult him, you'll be a mile away, and you'll have his shoes.
Thats it !!! Thank you, thank you...biggrin.png

I wish i had your brain. smile.png

This topic is closed to new replies.

Advertisement