Using a Box2D Controller
Box2D lets you easily apply a wide range of force, gravity, impulse to a b2body. It’s actually very simple as you can see in this following example. Simply click on any object to make it jump:
The code for this is actually very straight forward. I simply get a reference of the b2Body referenced by the graphic I click on and then in my click event handler:
body.ApplyImpulse(new b2Vec2(0 , - 7), body.GetWorldCenter());
and this is really it.
Controllers let you apply a wide range of different forces too but on as many b2Body as you want. Let’s say you create a platform game and on one level there is a area where the gravity is different, or why not an area filled with water or again an area very windy. Instead of trying to apply different values to all b2Body, you simply register them with a controller and let the magic happen.
We won’t try all Controller here but only show a couple just so we know how to use them. A popular and spectacular one if the b2BuoyancyController which let’s you simulate fluids. Let’s try this. As usual click first to see the objects start falling. In the center, there’s a simple graphic representing a sink full of water. If the falling objects enter in that area, they are added to the b2BuoyancyController which simulates a fluid. For that purpose I set the basketball to be a bit less dense while the little crate were set to be heavier. That way you can really see the difference between the two when they enter the fluid area. If a graphic floating in the sink leaves the sink area for some reason, its referenced b2Body is removed from the controller. Go ahead and try it.
Some relevant code:
fuildcontroller.offset = - 280 / SCALE;
fuildcontroller.density = 3;
fuildcontroller.angularDrag = 2;
fuildcontroller.linearDrag = 5;
We set the normal (which is a b2Vec2) to opposite on the y axis. We then set an offset to position the start of that fluid (280 pixels from the top). We also set a density for the fluid, the higher the value the thicker will be the fluid simulated. Water should probably be 1 or 2 but here I set it to 3. The angularDrag and linearDrag value influences the reaction and movements of the objects so feel free to experiment with them. Finally we set our Controller:
And that’s it. Now all you have to do is add b2Body instances to it simply like that:
And of course you can remove them as easily:
Important: Do not add the same b2Body twice! Weird results will occur.
Now for our last example we will look into the b2ConstantAccelController. This simple Controller applies an acceleration to your b2Body (via a b2Vec2). This is the perfect Controller for simulating wind. Let’s see it in action. When objects pass through the orange stripe, they are added to the b2ConstantAccelController and of course once they are out of that zone they are removed. To really show the effect I set a very strong wind. Click anywhere:
windcontroler.A = new b2Vec2(15, 0);
Setting a wind in Box2D with 3 lines of code. That can’t get better than that. Now it’s time to look into Box2D joints.