0

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.

hakubaa
  • 305
  • 2
  • 7
  • Hard to picture what you've done there. But if you collect all the collisions for the ball e.g. current vector and delta, and then bin the duplicates, you'll get where you want to be. You can also sum the deltas as well so you can hit the corner of the game area as well and so forth. – Tony Hopkinson Jan 15 '14 at 00:29

1 Answers1

0

Maybe you should update your velocity simultaneously. When you are representing your velocity as vectors you can calculate all collision updates and then linearly combine these updates to a single update.

See this post and adopt it to your problem: Ball to Ball Collision - Detection and Handling

Community
  • 1
  • 1
Marc
  • 111
  • 2