#### Archived

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

# Sphere / Line Intersection (Sweep?)

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

## Recommended Posts

I''m looking for an example of sphere/line intersection for collision. I have code for ray/sphere intersection, the problem is I can''t determine the correct point on the edge of the poly to start the ray to trace back towards the sphere. So, I''m thinking I need to have the sphere check for an intersection with the line (edge of poly). I searched the web for quite a while and came up with nothing. Can someone point me in the right direction?

##### Share on other sites

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

##### Share on other sites
While they have some similar aspects (A LINE ... A SPHERE) -- they ask totally different questions.

One asks about problems people might have experienced with line (ray) -> sphere intersections. It was really kind of vague and I probably just shouldn''t have posted it. But I know there''s a lot of bright people on this board that may have encountered similar problems.

The other (this one) asks about sphere sweeping(?) and colliding with a line and where I might find a little information about it. This is simply not the same question and (I felt) would have been lost had I added a response to the first (vague) question.

Please excuse me for my ignorance...

##### Share on other sites
Unger,

That''s fine, its okay to have a separate post for a new, but related question. I would appreciate it if you would make your subject title more specific. That will help avoid some confusion.

Thanks,

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

##### Share on other sites
I'm a little confused by what you are asking. Simply finding the intersection is relatively simple. You can calculate a vector from a point on the line, any point, to the center of the sphere. If you also calculate a unit vector in the direction of the line then the dot product of those two vectors is the distance along the line where it is closest to the center of the sphere. You can find the distance from that point to the center of the sphere. If that distance is greater than the radius of the sphere then they don't intersect. If it is equal then the line is tangent and that is the only point of intersection. If it is less than the radius then that is one side of a right triangle. Another side is the radius. The radius is the hypotheneous (sp?). That allows you to calculate the third side. The intersections are that distance forwards and backwards along the line from the closest point. That is not a collision detection though.

Edited by - LilBudyWizer on December 29, 2001 6:08:30 PM

##### Share on other sites
Guess I didn''t describe my problem real well.

What you''ve described is exactly what I''m doing ... determining the closest point on the line to the center of the sphere. But that''s not what needs to happen to determine where on the line the sphere would hit, moving at an arbitrary velocity (say, 5 degrees off of parallel to the line).

A specific case I''m stumped by, has the center of the sphere positioned above the line, and moving so that the bottom portion will collide with the line. I''m just trying to figure out how to detect that collision, and determine where on the line and the sphere they would hit each other.

BTW, I''m using Paul Nettle''s collision code as a reference to my collision routines. I found (and fixed) some minor problems, and everything works fine except _some_ cases where the sphere is slightly above the poly moving down/over the top, and the bottom portion (bottom 1/8th?) of the sphere hits the top edge of the poly (ie: the top line).

Well, this probably doesn''t describe what''s happening much better than before. I may just need to look at another way of handling spherical collisions.

##### Share on other sites
Ok, if you have two points on a line Q and R and a third point P then you can define two vectors by a=R-Q and b=P-Q. The distance from the point P to the line QR is d=|a x b|/|a|. If a is a unit vector then |a| is one and it reduces to |a x b|. If you recall |a x b| is the |a||b|sin(theta) where theta is the angle between the two vectors. Now P is the center of your sphere. You want the time when the distance of the center from the line is equal to the radius or r=|a x b|. A bit simplier is r^2=|a x b|^2 since it eliminates the square root. If the sphere moves in a straight line at a constant velocity then P at time t is given by P(t)=p0+v*t. That gives you a quadradic equation in t which you can use the quadradic formula to solve. Assuming the spheres path is not parallel to the line and moves close enough to collide with the line then you get two times. One time will be when the sphere first touchs the line and the other will be after the sphere passes through the line. I'm afraid I don't feel up to solving that equation right now.

Ok, I played with it a bit and found a fairly simple representation of the quadradic. Given that P(t)=p0+v*t and the vector b is P(t)-Q then a x b = (a x v)*t + (a x (p0-Q)). Unless the direction/velocity of the sphere changes or the line moves (a x v) and (a x (p0 - Q)) are both constant vectors. That allows you to define two new vectors c=(a x v) and d=(a x (p0 - Q)). So now an equivalent expression is a x b = c * t + d. Now |a x b|^2 = (a x b).(a x b) = (c * t + d).(c * t + d) = c.c * t^2 + 2 * c.d * t + d.d = r^2. So you plug A=c.c, B=2*c.d and C=d.d-r^2 into the quadradic formula (-B+/-sqrt(B^2-4*A*C))/(2*A) to find the values of t.

Edited by - LilBudyWizer on December 30, 2001 8:15:01 PM

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013600
×