Jump to content
  • Advertisement
Sign in to follow this  

Dynamic ray-sphere collision

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

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

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

Link to post
Share on other sites
Thanks for helping.
To answer your questions:
- 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 vector
Sphere: defined by a center, a radius, and its speed

speed2D = 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,


Share this post

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

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

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

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!