# AABB Sweeping, algorithm to solve "stacking box" problem

I'm currently working on a simple AABB collision system and after some fiddling the sweeping of a single box vs. another and the calculation of the response velocity needed to push them apart works flawlessly.

Now on to the new problem, imagine I'm having a stack of boxes which are falling towards a ground box which isn't moving:

Each of these boxes has a vertical velocity for the "gravity" value, let's say this velocity is 5.

Now, the result is that they all fall into each other:

The reason is obvious, since all the boxes have a downward velocity of 5, this results in no collisions when calculating the relative velocity between the boxes during sweeping.

Note: The red ground box here is static (always 0 velocity, can utilize spatial partitioning ), and all dynamic > static collisions are resolved first, thus the fact that the boxes stop correctly at this ground box.

So, this seems to be simply an issue with the order the boxes are sweept against each other.

I imagine that sorting the boxes based on their x and y velocities and then sweeping these groups correctly against each other may resolve this issues.

So, I'm looking for algorithms / examples on how to implement such a system.

The code can be found here: https://github.com/BonsaiDen/aabb

The two files which are of interest are box/Dynamic.lua and box/Manager.lua.

The project is using Love2D in case you want to run it.

• why don't you resolve static collisions first? and then you need to decrease your time steps. Jun 30 '12 at 16:03
• Sorting from lowest to highest is important to get good stacked box behavior in traditional discrete collision/physics systems. Unsure about swept volumes Jun 30 '12 at 19:06
• @Gajet I'm already doing the static ones first (that's why they don't fall through the red box which is the ground in this case) but the white boxes need to stack on top of each other. Jun 30 '12 at 19:46
• @seanmiddleditch That might work out as a quick fix, now the question is how to make it work in cases where the gravity is inverted. Jun 30 '12 at 19:47
• @Ivo Wetzel again in discrete systems, that effect is resolved by running multiple iterations of the contact resolver every physics update. It does not eliminate the innacuracies, but greatly reduces them. A great great number of tweaks and special hacks are required to get good stacking behavior. The only way to do it perfectly would be to use elastic bodies (no real objects are perfectly inelastic), infinitely small time steps, and resolve all collisions simultaneously. :) Jul 2 '12 at 1:03