Jump to content
  • Advertisement
Sign in to follow this  

Box stacking

This topic is 1218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,


I'm trying to get stable box stacking with my physics engine. I use an accumulated impulse model but I dont pre-apply last accumulated impulse at the beginning of each timestep, basically I don't have a preStep function as in box2d lite. Actually, I tried to implement it but it made the simulation totally unstable. I create persistent contact manifolds by checking contact proximity at each step, I use something like;

if (contact.position.distanceToSquared(contacts[i].position) <= 0.004) {
      // Use previous contact impulses

My allowed penetration is 0.01 and bias is 0.1, iteration 10.


At each iteration I calculate impulses like this;

// Normal Impulse
deltaNormalImpulse = c.massNormal * (-vn + c.bias)
oldImpulse = c.normalImpulse;
c.normalImpulse = Math.max(oldImpulse + deltaNormalImpulse, 0.0);
deltaNormalImpulse = c.normalImpulse - oldImpulse;

// I use the same approach for calculating the tangent and binormal impulses
deltaTangentImpulse = c.massTangent * (-vt);
maxTangentImpulse = 0.6 * c.normalImpulse;
oldTangentImpulse = c.tangentImpulse;
c.tangentImpulse = clamp(oldTangentImpulse + deltaTangentImpulse, -maxTangentImpulse, +maxTangentImpulse);
deltaTangentImpulse = c.tangentImpulse - oldTangentImpulse;

deltaBinormalImpulse = c.massBinormal * (-vb);
maxBinormalImpulse = 0.6 * c.normalImpulse;
oldBinormalImpulse = c.binormalImpulse;
c.binormalImpulse = clamp(oldBinormalImpulse + deltaBinormalImpulse, -maxBinormalImpulse, +maxBinormalImpulse);
deltaBinormalImpulse = c.binormalImpulse - oldBinormalImpulse;

 and here is the result;


https://vimeo.com/135466972 (Watch on vimeo for HD version)



Box stacking is not stable, they fall down in the long run. By the way, sleeping is not enabled in this demo, but I think it should be stable without sleeping, isn't it?


Waiting for your tips.


Thanks, Uza

Edited by uza

Share this post

Link to post
Share on other sites
Try this:

A vertical stack of 5 boxes of 0.05km/^3 using 5 iterations, without contact caching, nor sleeping, nor any damping. The boxes should slide of the top of each other approx. after 6 seconds.

Later try only with contact caching enable. The vertical stack should be stable. Otherwise you have bug somewhere.

Also make sure that the ground inertia tensor is a zero matrix (not the identity).

Share this post

Link to post
Share on other sites

I said about the Inertia just for precaution. 


Like Dirk said, old friction solutions wouldn't solve your problem.


Seeing your video I remember this behaviour occurred when NOT using contact caching. Therefore, you may check how you're computing the tangential directions and velocity.

Share this post

Link to post
Share on other sites

I think I'm doing it wrong. Do I need to copy tangents along with impulses when caching contact data?

Share this post

Link to post
Share on other sites

I just tried stacking spheres and it was extremely stable. No problem there, so is this an indication that the source of the problem is in the contact manifold generation for boxes?

Share this post

Link to post
Share on other sites
Each contact point holds the normal and tangential impulses;
Before a manifold generation, make a copy of the old manifold;
If there is a match between the manifold contact points then copy the normal impulse and project the old manifold tangential impulses on the new tangential directions;
Apply the old impulses one time (that is, warm-start the solver);
Apply new impulses for how many iterations you want updating the accumulated impulses during the iterations;

Basically, thats all needed.

Test your colllision detection routine separately for a box pair drawing the contact points and normal persistently.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!