0
\$\begingroup\$

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.

Image explaining the problem

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.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ What about moving all of your objects first, then handling the collisions that occur? \$\endgroup\$
    – Vaillancourt
    Sep 17, 2021 at 21:52
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ Sep 17, 2021 at 22:28

1 Answer 1

0
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$ Sep 17, 2021 at 22:31
  • \$\begingroup\$ 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. \$\endgroup\$
    – pgibbons
    Sep 18, 2021 at 11:27
  • \$\begingroup\$ 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 \$\endgroup\$ Sep 23, 2021 at 16:43
  • \$\begingroup\$ If it's like a train, the locomotive doesn't have a collision and should move first. \$\endgroup\$
    – pgibbons
    Sep 23, 2021 at 19:59
  • \$\begingroup\$ 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. \$\endgroup\$
    – pgibbons
    Jul 24 at 14:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .