Collision detection formula math

Started by
2 comments, last by dgmul 15 years, 9 months ago
Hey there! What's up gamedev.net? I kept finding links to your forums in google searches, so I thought I'd join and ask my question here. Seems like a pretty helpful place. Anyways, I'm working on the physics engine for what hopefully will be my first official game (if I don't get exasperated and just give up :| ). Right now I'm working on collision detection and reaction code. I've got circle-line collision detection down, and right now I'm trying to work out circle-circle collision detection. Now here's the deal: I've got this formula worked out that assigns a value to the variable t, which represents the point in time at which the distance between the two centers of the circles is equal to the two radiuses combined (they are perfectly touching). This formula works on a moving circle and a static circle. I've isolated t with a CAS: According to my CAS, t equals plus or minus , where vx and vy are the x and y velocities of the moving ball, x0 and y0 are the current coordinates of the moving ball, r0 is the radius of the moving ball, x1 and y1 are the coordinates of the static ball, and r1 is the radius of the static ball. Now, because this formula contains a square root, it's going to either have no solutions, or two solutions (plus or minus). I'm assuming no solution means that the circles will never collide, and the two solutions are the two points on the line formed by the moving ball's path that intersect with the 2nd circle. So... how exactly would I determine whether there would be no solutions or two? And if there are two, how do I calculate the one solution that represents the FIRST point that the balls intersect? Any help would be really appreciated. :) Peace and love, dgm
Advertisement
without double-checking the equation (b/c working in a factory just sucks it out of you... )

there's 3 possible scenarios.. not 2..

a) no intersection - iirc your square root will be imaginary (so first check make sure that whats under it isn't negative...)

b) 1 intersection (square root is 0) - the moving circle becomes *tangent* to the second one at some instant..

c) 2 intersections - (square root +/-) - first collision is smallest t > 0 . negative t means movement backwards...
As you note, the solution(s) to the equation represent the time(s) at which the two circles are just touching. If there are no solutions, the circles miss each other. If there are two solutions, the first value of t (that is, the value that is left-most on the real-number line) represents the time at which the circles start touching, and the second represents the time at which they stop touching. It may also be the case that there's only one solution, in which case the circles just 'graze' each other; for practical purposes this can probably be handled the same as the 'two solutions' case (where it just so happens that the first and second values for t are the same).

If t1 is >= 0, then that is the time that you're interested in. If both t1 and t2 are < 0, then the intersection occurred 'in the past' and is presumably of no interest. If t1 is < 0 and t2 is > 0, the two circles are currently intersecting.

Does that help at all?
Hey, thanks a lot hstubbs3 and jyk! You helped me wrap my head around the problem - now I've got the collision detection working perfectly! :)

Now I just need to implement ball spin, friction, and rolling... wish me luck.

This topic is closed to new replies.

Advertisement