I am making a small simulation in which a ball collides with other objects (e.g. rectangles). My main loop looks like this:
while (true) {
entities.update(); // update all entities (e.g. ball, rectangles), move them
updateCollision(); // check for collision and inform colliding objects
}
Everything works perfectly in case of collision involving the ball and only one object. The ball bounces in accordance with implemented physics. However it dosen't work when the collision system detects that the ball collides with more then one rectangles simultaneously. Why ? When the ball hits rectangle, it's change its direction and it's ok. But when the ball hits two rectangles, its change direction as a result of collision with the first one, and then it change direction once more as a result of collision with the second rectangle. So when this two rectangles are very close to each other and the ball falls down and hits them at the same time, direction of ball is changed twice, so at the end it will not changed at all. It all happens in one iteration in loop in updateCollision() function. How can I improve this mechanism to work correctly? I will appreciate any ideas.