Jump to content
  • Advertisement
Sign in to follow this  
CNU

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.

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement
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
just do a dotproduct of the forward direction + the direction towards the sphere

Share this post


Link to post
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 this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!