Problems with collision order of resolution

I have two objects, A and B, and I need help to solve the following problem about the collision resolution order:

A and B are moving with the same speed, and they don't move both at the same time

1. A tries to move to where it should be, and I do the collision test
2. A collision is detected, and A is moved outside of collision
3. Then B moves after this, and a gap appears between then

I tried resolving the collision of A by discouting the velocity of B, but this fails if B collides on it's path, and A moves inside of it.

Are there any algorithm that can solve this problem? And also when working with multiple objects moving in a row just like A and B.

Also, my objects don't have any mass or elasticity, they are supposed to just move and get out of collision using simple AABB.

• What about moving all of your objects first, then handling the collisions that occur? Commented Sep 17, 2021 at 21:52
• I was thinking of doing something like this, I just tough of doing things like this: 1. move everything at the same time 2. check every object against the dynamic ones and then check against static ones 3. repeat this process X times until you're satisfied or until no collisions are reported anymore Commented Sep 17, 2021 at 22:28

Basically you're going in the wrong order. If it's just A and B or a few objects like that, I would first iterate through each object with no collision and skip any that have collisions, then iterate through all the ones that had collisions, hopefully they will then be resolved. But if you have a lot of these objects like a train, then you need to change the order in which this is done.

• I didn't understand the step where I skip the objects that have a collision, would I do this only one time? This may work if it's only in the situation with two objects, but if i have many, when do I know I shouldn't skip it? I'm making this collision detection/resolution thinking about cases where I do have many objects at the same time like a train. Commented Sep 17, 2021 at 22:31
• Let's say each object has a flag such as move_done. You iterate through each object, if there is no collision you set this flag to true and move it, if there is, you keep the flag as false and skip it. When you have checked each object you start again, this time you skip the ones that have already been moved and you can skip the ones that have yet again a collision. If you keep looping through this, eventually all objects will have moved. This would not be very efficient, it would be better to change the order. Commented Sep 18, 2021 at 11:27
• I was thinking about this solution right now, but after some thought, won't the collision be stuck in an infinite loop? I would happen when A would move towards B, and B move towards A, both of them would report a collision and won't move, and will be stuck like that. I'm trying to improve on your algorithm rn Commented Sep 23, 2021 at 16:43
• If it's like a train, the locomotive doesn't have a collision and should move first. Commented Sep 23, 2021 at 19:59
• You certainly need a timeout of some sort, for example only loop through the whole thing 3 times over and any objects still with collisions simply can't go anywhere. Commented Jul 24, 2022 at 14:00