• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

# Camera position in spotlight cone

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1belfegor  Crossbones+   -  Reputation: 2040

Like
0Likes
Like

Posted 18 April 2012 - 12:34 PM

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.

### #2WarAmp  Members   -  Reputation: 572

Like
1Likes
Like

Posted 18 April 2012 - 02:09 PM

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.

### #3belfegor  Crossbones+   -  Reputation: 2040

Like
0Likes
Like

Posted 18 April 2012 - 02:56 PM

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.

### #4WarAmp  Members   -  Reputation: 572

Like
1Likes
Like

Posted 18 April 2012 - 03:15 PM

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.

### #5belfegor  Crossbones+   -  Reputation: 2040

Like
0Likes
Like

Posted 18 April 2012 - 03:25 PM

Thats it !!! Thank you, thank you...