Original Post
I currently have a demo of 16 boxes bouncing against 4 walls also represented as 4 single pixel thin collision boxes. It works fine, but now I want to have the collision boxes bounce off each other I'm trying to figure the best way to go about it. Currently my movement is frame based and basically works like this: while all movement in this frame hasn't finished { find earliest collision if collision happened within remaining frame time { move all boxes up to time of collision handle the box that collided with a wall add time it took to get to the next collision to the total frame time } else { move all boxes to end of frame position break loop condition } } and if you want to see the actual source code: Handling two objects bouncing against each other is easy, all my boxes are assumed to have the same mass so they just exchange linear velocities to conserve momentum. But what do I do if all of my boxes hit each other as one clump at the same time? How would I handle if half hit each other in one clump and the other half in another clump? And how would I handle a clump of boxes colliding at a wall?
/*Logic*/
float frameTime = 0;
//While the frame hasn't finished
while( roundf( frameTime, 5 ) < 1 )
{
//Next collision time and collision intervals
ColEvent nextEvent;
//Check box collision
for( int c = 0; c < NUM_BOXES; c++ )
{
//Against walls
for( int w = 0; w < NUM_WALLS; w++ )
{
//Test for collision
ColIntervals colI = calc_intervals( boxes[ c ], walls[ w ] );
float colT = col_time( colI );
//If time is earlier
if( colT < nextEvent.time )
{
//Replace collision event
nextEvent.time = colT;
nextEvent.intervals = colI;
nextEvent.a = &boxes[ c ];
nextEvent.b = &walls[ w ];
}
}
}
//If collision happens this frame
if( roundf( nextEvent.time + frameTime, 5 ) < 1 )
{
//Move to collision point
for( int b = 0; b < NUM_BOXES; b++ )
{
boxes[ b ].move( nextEvent.time );
}
//Handle collision
nextEvent.a->handle_collision( nextEvent.intervals );
//Move time forward
frameTime += nextEvent.time;
}
else
{
//Do movement for remaining frame
nextEvent.time = 1 - frameTime;
//Move to end of frame time
for( int b = 0; b < NUM_BOXES; b++ )
{
boxes[ b ].move( nextEvent.time );
}
//Break loop
frameTime = 2;
}
}
}