How to fix circles overlap in collision response?
Asked Answered
A

1

1

Since in the digital world a real collision almost never happens, we will always have a situation where the "colliding" balls overlap.

How to put back balls in situation where they collide perfectly without overlap?

I would solve this problem with a posteriori approach (in two dimensions).

In short I have to solve this equation for t:

((x2 - t * v.x2) - (x1 - t * v.x1))^2 + ((y2 - t * v.y2) - (y1 - t * v.y1))^2 = (r1 + r2)^2

Where:

  • t is a number that answers to the question: how many frames ago did the collision happen perfectly?
  • (x1, y1) is the center of the first ball
  • (x2, y2) is the center of the second ball
  • (v.x1, v.y1) and (v.x2, v.y2) are their velocities.

but the solution from WolframAlpha is too complicated (I changed the name of the velocities but essentially does not change anything).

Astronomy answered 8/9, 2013 at 11:33 Comment(2)
If I understand correctly, you want to find the position where the circles have only one common point? Find a line that connects the centers of the circles, then move one of the circles so that the distance between their centers is equal to the sum of their radiuses?Moujik
Yes you understand correctly but your solution (that I already know and I'm using for now) is wrong because the direction of the line that connects the center is different from the direction of the velocity(especially if the ball is very intersected to the other ball).Astronomy
T
3

It looks complicated because it's the full solution, not just the simplified polynomial form of it. Multiply everything out and gather the constant, t, and t^2 terms, and you'll find that it becomes just at^2 + bt + c = 0. From there you can just use the quadratic formula.

Also, if you want to keep things simple, do them with vector math. There's no reason here to separate out the x and y coordinates; vector addition and dot products are all you need.

Finally, all that matters is the relative position and relative velocity. Pretend one circle is at the origin and stationary, and apply the difference to the other ball. That doesn't change the answer, but it does reduce the number of variables you're wrangling.

Tansey answered 8/9, 2013 at 13:53 Comment(7)
Can you explain how do that with vector math?Astronomy
Sure -- the first ball's position is X1, the second ball's position is X2, and the velocities are V1 and V2. (Those are all 2D variables). Take X=X2-X1, and V=V2-V1. Then you have (X+tV) dot (X+tV) = r^2.Tansey
Thank you I'll try your solution! r is the sum of the radii?Astronomy
yep. (yepyepyep character limit).Tansey
I realized that it is the same thing as using separate x and y but now I have the same problem: how can I extract t from the vector equation?Astronomy
After being multiplied out, it becomes (V dot V)*t^2 + 2*(X dot V)*t + (X dot X) - r^2 = 0. There you have your a, b, and c. The rest is just the quadratic formula.Tansey
It worked. A very big thank you, finally my collisions between balls are perfect!Astronomy

© 2022 - 2024 — McMap. All rights reserved.