# Finding out if I'm aiming at a sphere

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

## Recommended Posts

I wondered if it's possible to find out if I'm aiming at a sphere. With aiming, I mean like in most FPS games, where the middle of the screen/crosshair is hitting the sphere somewhere. (not like if the sphere is visible on the screen) I have XYZ and Pitch, Yaw and Roll for the camera and I have XYZ for the ball and it's radius 5.0f (it's drawn with gluSphere). Could anyone help me with this, because we haven't learnt any math similar to this in school yet (we just learnt parabolas or something I think). Thanks!

##### Share on other sites
First you get the direction vector for the camera. Then you test to see if it intersects the sphere. You can get the direction vector from the camera's rotation matrix. Look up "euler to rotation matrix conversion" to find out how to compute the rotation matrix. To find out how to test for intersection, look up "ray sphere intersection".

Thanks a lot!

##### Share on other sites
just do a dotproduct of the forward direction + the direction towards the sphere

##### Share on other sites
Or you can just find the nearset point from the sphere center to the camera's viewing vector then check to see if it is less than the sphere's radius.

zedz: what do you mean by taking the dotproduct of the forward direction + the direction towards the sphere? what will that do??

##### Share on other sites
Quote:
 Original post by FarrajOr you can just find the nearset point from the sphere center to the camera's viewing vector then check to see if it is less than the sphere's radius.zedz: what do you mean by taking the dotproduct of the forward direction + the direction towards the sphere? what will that do??

That will give you the cosine of the angle between the two directions. You could then say this:

cos(theta)=dot(sphDir,forwardDir)
d=distance from you to sphere
d*sqrt(1-cos(theta)^2) <= r (if you're intersecting it)
d^2*(1-dot(sphDir,fwdDir)^2) <= r^2 (to eliminate slow operations)

This should be a fast comparison. I'm not sure if there's a better one, though, and it will also be true if the sphere is directly behind you.

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002075
×