0

So I am making a physics engine that only uses rectangles (axis-aligned bounding boxes) as shapes. I have implemented a method from christer ericsons book that returns the collision time and normal of two moving aabbs. I also have made another method that takes two aabbs velocities, positions and a normal that responds to the collision and give the aabbs new velocities.

The actual problem now is is that I don't know how the loop, that checks the collisions between all aabbs and responds to them, should look like. Simply I don't understand how to order the collisions by the time of impact, and which collision I should respond to.

A loop written in pseudo code that shows how to order all collisions would be really helpful.

Another thing I've mentioned is that it's possible thata moving box could bounce between two static boxes hundreds of times in a single frame if it's velocity is really high, how do you handle that?

HugoRune
  • 13,157
  • 7
  • 69
  • 144

1 Answers1

0

You should let a simple priority queue take care of the right order of execution. Conceptually, this would end up looking something like this:

queue<CollisionEvent> q = new empty queue
while (!q.isEmpty) {
  nextCollision = q.dequeueMinimum
  /* run animation until nextCollision.time 
     ...
  */
  newMovingParticles = nextCollision.movingParticles
  newCollisions = computeCollisionEvents(newMovingParticles, allOtherParticles);
  for each event in newCollisions {
    q.enqueue(event, event.time);
  }
}

What about boxes that move with large velocities: I don't know. Physically, it would make sense to just accept that it can happen that there is a sequence of very frequent collision events. I can not explain why, but for some reason I do not expect any infinite loops or zeno-type-problems. I would rather expect that even frontal collisions of very heavy boxes with very light boxes, where the light box is trapped between the heavy box and the wall, end in finitely many steps. This is what makes the rigid bodies rigid, I think one should just accept this as a feature.

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
  • Since there are not many programmers on the topic collision-detection, may i just ask if you got something like skype or facebook where i can reach you more like in a chat? I write code in javascript so psuedo still makes me a bit confused. What does the enqueue function for instance mean? –  Apr 20 '15 at 15:32
  • This is not really "pseudocode" (whatever it means), it rather just "generic c++/java-esque" code. Queue is a first-in-first-out data structure. The queue you would need here is a queue that allows to insert elements by priority. The `enqueue` and `dequeue` are the basic operations supported by a `queue` data structure. I'm neither expert on physic-engines, nor on javascript. However, you might want to pose a more extended version of your question, maybe with some runnable code (maybe host it on github?), so that one actually has something to work with, maybe someone will take a closer look. – Andrey Tyukin Apr 20 '15 at 15:49