Finding out if I'm aiming at a sphere

Started by
4 comments, last by nagromo 17 years, 1 month ago
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!
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".
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Thanks a lot!
just do a dotproduct of the forward direction + the direction towards the sphere
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??
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.

This topic is closed to new replies.

Advertisement