I'm thinking about how I should manage collisions in my game. I'm thinking about having a "Collision" class that checks for collision, and in case takes actions to resolve them.
My problem is this:
A and B moving to the other, when they collide they must bounce.
If I resolve the collisions sequentially, like calling:
- A.collideWith(B)
- B.collidesWith(A)
B will be stuck, because A has already updated his velocity and position, and when will be called B.collideWith(A) there won't be any collision anymore.
So, is it a good idea to use a double buffer pattern?
I'll write the values obtained by the collision resolver in some variables, they will be swapped to the real ones when all the collision has been resolved. In that way all should work fine.
But it is a good software design?
EDIT
I see, yes of course i check only for half. So there should be a class that checks for collisions, and should be like:
checkedpairs.clear();
for( Object a : objects )
{
for( Object b : objects )
{
if( ! a.collideWith( b ) || checkedpairs.contains( a, b ) ) return;
Object a1 = copyf( a );
Object b1 = copyOf( b );
a.collideWith( b1 );
b.collideWith( a1 );
checkedpairs.add( a, b );
}
}
Instead of putting the logic for the swap in the objects themselves...
But that way, I'm creating two objects for each collision, isn't too much overhead?
The problem is that I don't need only the position, but al lot of physics stuff like velocity, acceleration, mass... Should I pass them? Isn't it too hard-coded?