Troubleshooting
From Box2D Wiki
Why doesn't my code compile and/or link?
There are many reasons why a build can go bad. Here are a few that have come up:
- Using old Box2D headers with new code.
- Not linking the Box2D library with your application.
- Using old project files that don't include some new source files.
For more help, see Building.
Dealing with Jitter
Box2D is designed to provide stable simulation with stacking. You should be able to stack 8 squares at 60Hz and 10 solver iterations without trouble. Much of Box2D's stability depends on the tuning in b2Settings.h. Here are some tips for dealing with jitter.
- Make sure you are using meters and your moving objects are between 0.1 and 10 meters in length.
- Is the restitution on all your shapes equal to zero?
- Did you artificially increase gravity? Increased gravity makes the solver work harder, so if you increase gravity you may need to decrease the time step.
- Did you use b2Body::SetMassFromShapes? This will give you a stable mass distribution on your shapes.
- Do all your moving bodies have similar density? Large mass ratios can cause stability problems in the solver.
When you are setting up your application, you should look at the test bed examples. These represent a well tuned Box2D simulation. You shouldn't expect to get higher stability without using a smaller time step and/or more solver iterations.
If all this fails, you should try to reproduce your problem in a test bed example in C++. Submit this code to the bug reporting sub-forum here: http://www.box2d.org/forum/viewforum.php?f=4
Ensuring Movement
So you downloaded Box2D, punched in some code, and pressed the run button. But alas nothing is moving on screen. What's wrong?
- Make sure you have provided mass to the body somehow. This is usually done by adding shapes to the body that have non-zero density and calling b2Body::SetMassFromShapes after you have added the shapes to the body. If you implement b2DebugDraw and draw the shapes, you will see static bodies in a green color. Static bodies have zero mass and never move.
- Make sure the world AABB is much larger than the world you are simulating. If a body goes outside the world boundary it will be frozen. If you implement b2DebugDraw and tell it to draw AABBs you should see the world AABB if you zoom out. Implement b2BoundaryListener to be notified when bodies go outside the world boundary.
- Did the body fall asleep? Applying forces wakes a body, but joint motors do not wake bodies. You can either wake the body manually using b2Body::WakeUp or instruct the body never to sleep in the body definition.
- Make sure b2World::Step is being called. Set a break point and make sure you hit it when you should (e.g. once per frame).
