Sign in to follow this  
CNU

Finding out if I'm aiming at a sphere

Recommended Posts

CNU    118
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 this post


Link to post
Share on other sites
JohnBolton    1372
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".

Share this post


Link to post
Share on other sites
Farraj    152
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 this post


Link to post
Share on other sites
nagromo    676
Quote:
Original post by Farraj
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??


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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this