• Advertisement


This topic is now archived and is closed to further replies.

Intersection of a line and a sphere

This topic is 5788 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

How can I find the intersection of a vector and a sphere? I got a formula but I don't know what to do with it: _____ ______ _____ _ _ (Q - M )( Q - M) + 2tC(Q - M ) + t^2C * C - r^2 = 0 where: M is the center of the sphere Q is a point of the line X is a point on the shere r is the radius C is a vector co-directed with the line t is time What am I supposed to solve for? t? Thanks in advance [edited by - creativeminds on April 19, 2002 2:17:42 AM] [edited by - creativeminds on April 19, 2002 2:18:41 AM]

Share this post

Link to post
Share on other sites
Solve for t. (That''s the only unknown.) To get the actual intersection points, you''ll probably use
Q + Ct
and substitute your value(s) for t.

Since it''s a quadratic equation, you''ll get either 0, 1, or 2 results. If 0, then the ray doesn''t intersect the sphere. If 1, then the ray just touches the edge of the sphere. If 2, the ray pierces the sphere, and you''ll get both the front and back intersections.

Share this post

Link to post
Share on other sites
Check www.realtimerendering.com/int for a complete list of intersections, One RaySphere it points you to is in Graphics Gems Volume 1, pg 388-389

You can also find it here: http://www.uwp.edu/academic/computer.science/morris.csci/CS.320/Week.10/Ch10.html

see figure 10.6

It also contains the pseudo code algorithm.

Share this post

Link to post
Share on other sites
Thanks m_w_noname for the link. The formula it give is a
lot easier but I can't get my code to work

VECTOR ray, sphere, intersection;
float v, radius, dsq, d;

ray.set( 5, 0, 0 );
sphere.set ( 10, 10, 10 );

radius = sphere.magnitude(); // radius of the sphere

v = sphere.dot( ray ); // the dot product of the sphere and ray

// use formulat: r^2 - sqrt( EO * EO - v^2)

// where EO is a vector

dsq = (radius * radius) - sqrt ( sphere.dot( sphere ) - (v * v) );
if ( dsq < 0 ) {
cout << "there is not intersection" << endl;
}else {
// use formula: d = sqrt( dsq )

// P = E + (v - d)V

// where: E is the ray origin and V is the ray vector

d = sqrt( dsq );
float temp = v - d;
intersection = ray * temp;

There is supposed to be an intersection at (10, 0, 0) but
i'm not getting it.
Sphere.dot( sphere ) - ( v * v) is a negative number!
I am getting the radius of the sphere as 17.3 and the dot product of the sphere and ray as 50.

What am I doing wrong?


[edited by - creativeminds on April 19, 2002 1:02:30 PM]

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Ugh. Is that a sqrt() I see? This is simply done by turning it into a right triangle problem. If you only want to detect the interesection and not the point of intersection:

bool cCollisionModel::testRaySphere(const cCollisionRay& ray, const cCollisionSphere& sphere)
bool intersect = false;

Vector rayToSphereCenter = sphere.position - ray.position;

float closestApproach = rayToSphereCenter.Dot(ray.direction);

if (closestApproach > 0 ) // the intersection is behind the ray
float lengthRTSC2 = rayToSphereCenter.Dot(rayToSphereCenter);

// halfCord2 = the distance squared from the closest approach of the ray to a perpendicular to the ray
//through the center of the sphere to the place where the ray actually intersects the sphere
float halfCord2 = (sphere.radius * sphere.radius) - lengthRTSC2 + (closestApproach * closestApproach);

intersect = (halfCord2 > 0);

return intersect;


So this code first makes a vector from the origin of the ray to the center of the sphere. It tests the dot to make sure it''s even headed near the sphere. Then it creates a right triangle of the two rays (the dot product of a vector on itself is the magnitude of the vector squared).

BTW, there''s not much more you have to do with this to get the point of intersection on the circle.

Share this post

Link to post
Share on other sites

  • Advertisement