friction of stacked boxes

Official forum for the Chipmunk2D Physics Library.
Post Reply
jzxu
Posts: 7
Joined: Fri Apr 23, 2010 1:22 pm
Contact:

friction of stacked boxes

Post by jzxu »

Hello:

I'm trying to model a stack of boxes being pushed on a flat surface in the presence of friction and gravity. If I understand correctly, chipmunk uses the Coulomb model of friction, so the velocity of a sliding object is decelerated by a constant amount opposite the direction of travel at each simulation step. That constant amount is a function of the object's mass and friction coefficient, and the y gravity constant. Is that all correct?

This gets a little more complicated when two boxes are stacked on top of each other. My expectation is that the friction between the bottom box and the ground should be a function of the mass of the entire stack, since the boxes on top are pushing down and increasing the normal force of the bottom box against the ground. Is that also correct?

Finally, when actually simulating this scenario, I see some very strange behavior. My setup is like this: I place two boxes into the world, one on top of the other. I give them some time to settle, then apply a horizontal impulse to the bottom box. As expected, it slides slightly, with the top box also moving but not as much. The strange behavior is that after a while, the top box starts to sink into the bottom box. You can see a video of it here:

http://www.youtube.com/watch?v=I5gtDsJO7Ss

I'm running the simulation with a dt of 1e-5 and my boxes are all 1 unit in size. The boxes have friction coefficients of 1.0 and the ground has a friction coefficient of 0.1. All objects have 0 elasticity. Does anyone have any suggestions to fix this behavior?

Thanks ahead of time for your help.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: friction of stacked boxes

Post by slembcke »

Yup, that is basically all correct. The simplest way to put it is that the friction force is proportional to the normal force multiplied by the friction coefficient. Static friction is not simulated.

The behavior in that video is definitely weird, and I have no idea what would be causing that... Can you replicate the issue using the demo code from the Chipmunk github repo? I could probably figure out what is going on pretty quickly then.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jzxu
Posts: 7
Joined: Fri Apr 23, 2010 1:22 pm
Contact:

Re: friction of stacked boxes

Post by jzxu »

Hi slembcke:

Thanks for replying. I made a demo like you suggested, but I couldn't reproduce the sinking behavior. However, the behavior I did produce is also strange - the top box falls off the stack after the bottom box is pushed (as expected), but when it hits the ground, one corner sticks to the ground while the entire box tips back up slowly. You can see it here:

http://www.youtube.com/watch?v=PEYtZ9gikWU

Here's the code for the demo:
Bug.c
(2.29 KiB) Downloaded 878 times
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: friction of stacked boxes

Post by slembcke »

Ah, so I think I know what is going on. Issue number one is that your box bodies' have their center of gravity at their lower left corner. Shapes are specified relative to the body's center of gravity. So by making one of the corners (0, 0)... You can use cpBoxShapeNew() as a convenience if you want. Otherwise you need to do this:

Code: Select all

cpVect box_verts[] = { {-L/2,-L/2}, {-L/2,L/2}, {L/2,L/2}, {L/2,-L/2} };
As for your first video, I'm willing to bet that you forgot to convert radians to degrees when drawing. The graphics seem to barely rotate at the end of the video, but the motion looks plausible if it was rotating correctly.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jzxu
Posts: 7
Joined: Fri Apr 23, 2010 1:22 pm
Contact:

Re: friction of stacked boxes

Post by jzxu »

You're absolutely right on both counts. Huge thanks for your help debugging my stupid mistakes!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: friction of stacked boxes

Post by slembcke »

No problem. :)
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jzxu
Posts: 7
Joined: Fri Apr 23, 2010 1:22 pm
Contact:

Re: friction of stacked boxes

Post by jzxu »

I have another quick question. If a box has two contact points with the ground, will the same frictional force be exerted twice on the box? Will this frictional force have the same magnitude as if the box had only one contact point with the ground, or will the force be spread out?

Thanks.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: friction of stacked boxes

Post by slembcke »

Short answer: The friction force will get spread out, and the amount doesn't really change with multiple contact points.

Longer answer: Each contact point has it's own normal force (how tightly the surfaces press together). For an object sliding across the ground, the total normal force will be constant (or the object would sink into the ground or float away). The total friction force will be constant too since it's a percentage of that. Depending on the weight distribution of the object, different contacts might get different amounts of normal force, and different amounts of friction. They only go out of equilibrium when an object has enough friction to be tipped over.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jzxu
Posts: 7
Joined: Fri Apr 23, 2010 1:22 pm
Contact:

Re: friction of stacked boxes

Post by jzxu »

I have yet another question. Hopefully these aren't getting annoying...

Suppose I have the following setup: Three boxes A, B, and C are arranged on a flat surface so that they're right next to each other. The three boxes have different masses, call them m1, m2, m3, and different friction constants, call them f1, f2, and f3. The ground has friction 1.0. All elasticity constants are set to 0. All boxes have infinite moments of inertia so they never rotate. This setup is shown in this drawing:
drawing.png
drawing.png (2.4 KiB) Viewed 14340 times
Now I apply a constant force F to box A. My expectation is that the three boxes will move as if they are a single body with mass M = (m1 + m2 + m3). Furthermore, I would expect the total force acting on this setup to be:

F' = F - G * (f1 * m1 + f2 * m2 + f3 * m3)

where G is the gravity constant. This is just the pushing force minus the frictional force experienced by each box. The total acceleration experienced by the system should be F' / M.

I tried to replicate this setup in chipmunk, and the acceleration I measure in the simulation is close to my calculation, but still significantly different (by about 0.0002 for the values I used). I'm not using this for a game, so the small difference matters.

My question is, is the difference due to my calculation being too naive, or imperfections in the simulation? If the latter, what parameters can I tune to get the difference to be smaller?

I'm already running the simulation with collision iterations set to 100, and collision slop and bias set to 1e-7. I'm also running with a very small step duration (1e-5). Decreasing any of these parameters doesn't seem to decrease the difference between the measured acceleration and my calculation. I've also tried putting 0-length joint constraints between the boxes, but that made the situation much worse, as it seemed to build energy as the simulation progressed.

Any suggestions would be appreciated. Thanks for your help!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: friction of stacked boxes

Post by slembcke »

A good question, but one that I don't exactly know the answer too. The thing to keep in mind is that Chipmunk uses an imprecise iterative solver. For a complex system with a lot of contacts and constraints, you could build and solve a giant linear system of equations, but it would be very expensive to do so. Instead, an iterative solver treats each contact and constraint independently and iterates over them several times. Each iteration should get you closer to the exact answer.

Chipmunk is primarily written for games, and games games really only require physically plausible physics. In other words, if the simulation is close enough that it looks correct, then it's good enough. So if the solver is 99.9% accurate and 20x faster than a non-iterative solver, that's an excellent compromise to make for a game engine. Not a good compromise for some non-game purposes though.

Anyway. While the quality should continue increasing with smaller timesteps and more iterations, it doesn't appear to be the case. At some point it doesn't help to do more of them. I'm not exactly sure why, but I would guess its just because the noise from the accumulated floating point error is too much to overcome.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests