Reputation: 71
I am learning basic collision detection.
Using posteriori (discrete) method for collision detection. Assume the simplest case of 2 circles in 2D, same mass, same size, and assume elastic collision and assume they are moving on the x-axis and are moving towards each other’s.
Now advanced the simulation one time step.
Assume now the circles are now in collision where one circle has entered another. This is found by checking that the distance between their centers is smaller than 2*r where r is the radius).
Now the speeds are adjusted according to the standard equation and the simulation is advanced one time step and the positions are adjusted. For this case, the speeds will flip directions and the circles will start moving away from each other’s.
The problem is that if the simulation time step is too small or the objects are moving too slow, it is possible that the 2 circles will remain in collision by the next step because they have not moved out of each other’s completely yet.
Therefore, in the next time step, the circles are found again to be in collision, and the speeds are adjusted again, but now they will flip backwards, and hence the circles will begin to move back into each other’s.
On the next time step, collision is detected again, and the speeds adjusted, and circles will now move away from each other’s.
This process repeats, and the circles will remain in collision unable to completely leave each other’s.
I am sure this is a known issue with the posteriori method. What is the best way to resolve this scenario?
Upvotes: 1
Views: 295
Reputation: 13
You could check every direction (-1, 0), (1, 0), (0, -1), (0, 1) and see which one is the farthest to the circle then repeat until it is out of the collision. Also, I will elaborate on user1354999's idea.
I think user1354999's means that you find the atan2 of the objects and find the distance. Then you move the objects to the negative of the atan2 by half the distance. To the random person that finds this hope it helps!
EDIT: by moving the objects I mean put the atan2 in the cos for x and sin for y.
Example:
var d = dist(this.x, (this.y + this.s), t.x, t.y);
if(d < this.s) {
var r = atan2(this.y - t.y, this.x - t.x);
t.x-=cos(r) * d/2;
t.y-=sin(r) * d/2;
}
Upvotes: 0
Reputation: 163
The standard solution is to move the objects apart when collision is detected. Calculate the amount of overlap and move each object away from the other by half this distance. You can even factor in the masses of the objects if you want (heavier objects are shifted less). Hope this helps.
Upvotes: 0