I'm developing a simulation in Java where objects move around in a 2D grid. Each cell in the grid can only be occupied by one cell, and the objects move by moving from one cell to another.
This is more theoretical than Java specific, but does anyone have ideas as to how I should approach collision handling with this grid? Are there any algorithms that people have used to handle collisions in a grid-like world?
Please note, I am not talking about collision detection as this is trivial since the objects move from one cell to another. I'm talking about collision handling, which can get extremely complex.
Ex: Object A wishes to move into the same location as Object B, and Object C wishes to move into Object B's current location. Since each cell can only contain one object, if Object A is able to move into its desired location, this will cause Object B to remain stationary and thus cause Object C to also remain stationary.
One can imagine that this could create an even longer chain of collisions that need handled.
Are there any algorithms or methods that people have used that can help with this? It is almost impossible to search for this problem without the search results being saturated with collision detection algorithms.
Edit: All objects move at the exact same time. I want to limit the number of objects that must remain stationary, so I prefer to handle objects with longer collision chains first (like Object B in this example).
[A]->[ ]<-[B]<-[C]
. Depending on who should move firstA
orB
providedC
moves last you may end up with two results:[ ] [A] [B] [C]
and[A] [B] [C] [ ]
. Do you have any order to process the cells? Do cells move simultaneously? – Mancini[A]->[ ]<-[B]
? Will it be[A] [ ] [B]
(they just remain where they were)? – Mancini[A]->[B]->[ ]
will resolve to[A] [ ] [B]
. – ManciniA
object in case it has very long chain, right? – Mancini