# Dynamic ray-sphere collision

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

## Recommended Posts

Hi, I'm having trouble figuring out how to calculate the time a moving sphere will intersect an infinite static line. I already have a function that calculates if a line is intersecting a sphere, but it's no use for this... :( Could someone explain me how to do or post me a link toward the equation I need to use (using vector and not parametric equations if possible)? Thanks for any help, Roming22

##### Share on other sites
2 questions:
-is the sphere having constant motion?
-2D or 3D?

In the following answer, constant motion in 2D is assumed.
Let's call:
n - the normalized vector representing the line.
V - the velocity vector of the sphere.

What we will do is simple: we will compute the speed component which is perpendicular to the line.
P = V - (V.n) . n
The magnitude of P represents how fast the sphere is crossing the line. Therefore, divide this by two times the radius and you'll get the time they are intersecting.

t = |P| / 2r

##### Share on other sites
Thanks for helping.
- the sphere has a constant motion during the step I'm calculating.
- I'm working in 3D.

The algorithm you gave me wouldn't work in 3D (since the line would become plane), so I'm still trying to work out the solution.

Something tells me that I should find a way to summarize it as a 2D circle/point collision, looking at the objects in the line's direction (thus see the line only as a point).

A good start seems to change the sphere's speed and getting the point in the plane.
Line: defined by a point and a normalized vectorSphere: defined by a center, a radius, and its speedspeed2D = speed3D-speed3D.dot(line.vector)*line.vector; //speed2D isn't really in 2D,but seems to be from our point of view,since we removed the speeed's colinear component to the line direction.closestPoint = point+((center-point)*vector)*vector //closest point from the center belonging to the line

Now everything should be set up to work in 2D, although everything is still 3D (the sphere can't pass in front or behind the point).

So now I have some kind of sphere-point collision...
*thinling as typing*
and it's something I know how to do!

So I guess that would work.

Anyone can confirm what I wrote?

Thanks for taking the time,

Roming22

##### Share on other sites
You can do the calculations directly in 3D. Let the line be represented parametrically by P+s*D, where P is a point on the line and D is a unit-length direction vector. Let the sphere have radius r and center C+t*V, where V is the constant velocity vector, and t is time.

The squared distance from the center to the line is |Cross(D,(t*V+C-P))|^2. Find t for when this is r^2. The resulting equation is quadratic, a0 + 2*a1*t + a2*t^2 = 0, where a0 = |Cross(D,C-P)|^2 - r^2, a1 = Dot(Cross(D,V),Cross(D,C-P)), and a2 = |Cross(D,V)|^2. If the equation has no real-valued roots, the sphere will never intersect the line. If the equation has one (repeated) real-valued root, the sphere will intersect the line tangentially. If the equation has two distinct real-valued roots, the smallest root tells you when the sphere first intersects the line. The largest root tells you when the sphere *last* intersects the line. Beware of degeneracies, such as a2 = 0 (sphere traveling parallel to the line), and others.

##### Share on other sites
Sorry for the late reply, but I've been away for some time.

I used your solution in another program, but I'm not satisfied when I have such equations in my programs, as it is more "mathematical" (or theorical) than "physical" (or practical), thus making it a little harder to for someone else to understand.

... and your solution has a square root whereas mine don't ;)

but I still don't know if it works...

##### Share on other sites
Dave's equation relates to a cylinder being hit by a sphere. It's nothing complicated.

cylinder equation

((Q - C) x D)2 = r2, for point Q on surface of the cylinder

C is point on the cylinder core, D is direction of cylinder, r is radius of cylinder.

Q = P + V.t is the equation of a point moving at velocity V

substitute Q in second equation to Q in first equation, and solve the second order equation for t. That will give you the time of impact of the two, when the point P will be at distance r from the cylinder core.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 46
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!