#118003 - sgeos - Fri Feb 09, 2007 1:28 am
I'm looking for a frame independent circle-circle collison/reaction routine. So far as I can tell, this is expensive. The basic flow goes something like this:
timeToCollison() seems to boild down to a quadratic equation:
When the mass of the objects are the same, reaction seems to simplify to exchanging movement along the line of action while leaving alone any movement perpendicular to line of action.
Is there a cheaper way to do all of this?
Reference:
Pool Hall Lessons: Fast, Accurate Collision Detection between Circles or Spheres
Macromedia Flash MX 2004 Demystified
-Brendan
Code: |
circle a;
circle b; float t = timeToCollison(a, b); // in frames if (t < 1.0) { scaledMove(a, t); // x += dx * scale; y += dy * scale; scaledMove(b, t); react(a, b); scaledMove(a, 1.0 - t); // or not, if you want them to touch scaledMove(b, 1.0 - t); } else { scaledMove(a, 1.0); scaledMove(b, 1.0); } |
timeToCollison() seems to boild down to a quadratic equation:
Code: |
// terms
pA; // circle A pB; // circle B r = pA.r + pB.r; // distance at collison x = pA.x - pB.x; // x distance y = pA.y - pB.y; // y distance dx = pA.dx - pB.dx; // delta x distance dy = pA.dy - pB.dy; // delta y distance t; // time SQ(v) ((v) * (v)) // squared variable // collide when distance equals sum of radii // SQ(a) + SQ(b) == SQ(c); SQ(t * dx + x) + SQ(t * dy + y) == SQ(r); // expanded SQ(t) * SQ(dx) + (t * 2 * dx * x) + SQ(x) + SQ(t) * SQ(dy) + (t * 2 * dy * y) + SQ(y) == SQ(r); // quadratic in terms of t SQ(t) * (SQ(dx) + SQ(dy)) + t * (2 * (dx * x + dy * y)) + (SQ(x) + SQ(y) - SQ(r)) == 0; // quadratic terms a = (SQ(dx) + SQ(dy)); b = (2 * (dx * x + dy * y)); c = (SQ(x) + SQ(y) - SQ(r)); // solve using quadratic equation, return smaller value t0 = (-b + sqrt(SQ(b) - 4*a*c)) / 2a; t1 = (-b - sqrt(SQ(b) - 4*a*c)) / 2a; |
When the mass of the objects are the same, reaction seems to simplify to exchanging movement along the line of action while leaving alone any movement perpendicular to line of action.
Is there a cheaper way to do all of this?
Reference:
Pool Hall Lessons: Fast, Accurate Collision Detection between Circles or Spheres
Macromedia Flash MX 2004 Demystified
-Brendan