#### Archived

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

# Acurate Response to Sphere-Sphere Intersection

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

## Recommended Posts

Ok, nice simple collision response i can do but i have got into a little bit of a situation trying to do things properly. My Collision Detection tells me that two spheres have collided. Fair enough. But the spheres are intersecting, inorder to get an accurate collision response i need to backtrack to find the exact point of contact. so for each sphere: Point at contact = Current point - Velocity * t; the problem is isolating the value of t at which the collision occurs. There are 3 unknowns, Point of contact for each sphere and t. The above equation can be used twice (once per sphere) but i still need another equation to solve them simultaneously. Anyone know how?

##### Share on other sites
If you know the position of each sphere at the momment they intersect you can equal the two sphere-equations to get a point that satisfies both equations.

[edited by - owl on January 13, 2004 6:25:21 PM]

##### Share on other sites
Ok slightly the wrong end of the stick. Due to the time steps you step over the actual moment when the spheres touch, leaving the spheres overlapping each other, i need to find the point of initial contact.

I have drawn a diagram to illustrate my problem.
Diagram

[edited by - aleks_1661 on January 13, 2004 6:54:41 PM]

[edited by - aleks_1661 on January 13, 2004 6:55:08 PM]

##### Share on other sites
you''ve got to solve a second order equation.

First, you need to make one sphere relative to the other (compute the relative velocity of sphere2 to sphere1), and you can use a ray-sphere intersection test. If a ray that intersects a sphere of radius (r1 + r2) is equivalent to a sphere of radius r1 colliding with a sphere of raduis r2.

ray equation
D = (V2 - V1)
O = (P2 - P1)

P = O + D.t

sphere equation
r = r2+r1

P2 = r2

substitute P with (O + D.t) into the second equation and it gives

(O + D.t)2 - r2 = 0

equivalent to a second order equation
a.t2 + b.t + c = 0

where

a = D2
b = 2.(D.O)
c = O2 - r2

to solve the second order equation a.t2 + b.t + c = 0

d = b2 - 4.a.c

if (d < 0.0f)
no intersection

t0 = (-b - sqrt(d)) / 2.a
t1 = (-b + sqrt(d)) / 2.a

you find the first positive between t0 and t1, and that will give the point of collision.

if none are positive, then it means that you need to backtrack the sphere (use negative number closest to 0)

t will be the time of collision, then you can move both sphere at that time

you can use the same principle with a cylinder and a sphere using the equation

L is the direction of the cylinder
r = rcylinder + rsphere

(P x L)2 = r2

##### Share on other sites
You are a Guru Oliii, Thanks. Quadratic Equations, euuuuuu!!!!

When i finished school I didnt realise i would actually need to use them.

For this problem, i thought there would have been an easy answer. Though thinking about it that is the right approach because the spheres could touch a number of times in that period, I suppose that in most cases i wil only find one instance. Its a shame i have to use sqrt though.

##### Share on other sites
quote:
Original post by aleks_1661
For this problem, i thought there would have been an easy answer. Though thinking about it that is the right approach because the spheres could touch a number of times in that period, I suppose that in most cases i wil only find one instance. Its a shame i have to use sqrt though.

This is a pretty easy answer when it comes to collision detection... its nice and clean, no need to perform some kind of numerical technique. And what is wrong with the sqrt function?

##### Share on other sites
Yeah, this is one of the classic problems of col det. It's easy peasy

you don't need to worry about sqrt. You only perform the square root if the spheres are on a collision path (or already colliding in your case), else 'd' becomes < 0 and you exit early in this case. There is a more goemetrical approach to the problem (ray-sphere intersection), but I can't remember it. It relies on trigonometry, if it makes you more comfortable.

[edited by - oliii on January 14, 2004 4:06:27 AM]

##### Share on other sites
I was only joking, the answer is cool. and to be honest a single sqrt is probably better than the sin/cos etc needed for a trig answer.

Now i know this is probably getting advanced, but does anyone know much about what to do when objects are at rest and stacked on each other?

Currently i have two spheres and a plane, the two sphers are positioned one above the other and they land on the plane in a stack. the only problem is that once stationary the top sphere slides down into the bottom sphere. I have programmed collision detection/response (not with the piece above, im still working on that) but i dont think i know how to use it in a proper physics system. Currently I perform detection and response between teh spheres and then with the plane. I can see that if i have more objects i would need some way of recording the collisions, sorting them chronologically and then executing responses, but even then, what if the response to one collision causes another collision like when you break in pool? or a newtons cradle?

[edited by - aleks_1661 on January 14, 2004 7:10:48 AM]

##### Share on other sites
usually, you don''t bother about that. You just provide an impulse, which changes the sphere velocities, and check for collisions again. It''s far from perfect, but it''s either that, or solving a set of linear (I think differencial also) equations for solving contacts. It''s called on LCP problem, and it''s the way to solve simultaneous contacts.

if you don''t use it, the objects will jitter, but it''s the price to pay.

If you sort collisions by time, you have to be careful with systems like "2 balls on top of each other and a plane". you can potentially run into an infinite loops, as the objects will collide indefinitely, then time of collision will decay to 0 exponentially, but never hit 0. That''s the theory anyway. A couple of cheats.

- You can run a maximum number of iterations, and if reached, force the sphere to have 0 velocity.
- If the sphere velocity reaches 0, stop collision tests.
- you can check if the last 2 or 3 collision planes found previously on an object completely enclose an object, if so, set it''s velocity to 0 and stop checking for collisions
- you can detect collisions that happened simultaneously (in a small time range), calculate the constrain from the planes
- for one plane (the usual case), the ball cannot move towards the plane
- for 2 planes (like a groove), the ball cannot move towards the line defined by the intersections of the plane
- for 3 planes (like a dip), the ball should move upwards from the intersection point of the three planes,
- for more planes, reduce them to the best 3 plane candidates
- and if the planes don''t intersect, you need special cases.

this becomes quite similar to an linear system of equation, which might be good to implement, as an exercise.

note : the geometrical solutions is trig, but mostly pythagor stuff, no sines or cosines. it''s got similarities with the equation solving I propose, but probably easier to visualise.

##### Share on other sites
Ok I have a problem in the above equation,

-------------------
equivalent to a second order equation
a.t2 + b.t + c = 0

where

a = D2
b = 2.(D.O)
c = O2 - r2
--------------------

Now D and O are Vectors ( or a point and a vector). is D.O a dot product or cross product?

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633409
• Total Posts
3011702
• ### Who's Online (See full list)

There are no registered users currently online

×