Have fun and help me to get to 1,000,000 downloads!

Using Box2D buoyancy controller to create a pool

Emanuele Feronato Actionscript 3, Box2D, Flash

If you watched all demos in the official Box2D AS3 page, you should know you can achieve buoyancy effect using a buoyancy controller.

I played a bit with it, and I wasn’t satisfied your controller must cover the entire world, just allowing you to limit it with an offset. It would have been way better if you could define an area in which bodies are affected by buoyancy controller. This way you could create a pool.

Look at this example:

Click with the mouse to release a random box from the top at mouseX position. You will see only bodies falling in the “pool” are affected by buoyancy controller.

The trick is made by creating a sensor with the same shape of the desired buoyancy controller, and adding bodies to the controller only if they touch the shape.

This way you can create pools

Look at the source code:

The code is not commented because I play to optimize it a bit, but if you have doubts, just ask. You can also download the source code.

From null to full HTML5 cross platform game

I will take you by hand from the bare bones of JavaScript programming through the creation of a full cross platform HTML5 game, with detailed explainations and source code.

If you don't know where to start, then From null to full HTML5 cross platform game is the book for you.

Comments 5

  1. siddharth shekar

    Great thanks… now how can I create ripples? I once tried faking it with sine and cos functions. But didnt look that great What is the standard/good way achieve a believable result? Thanks. And as usual awesome tutorials.

  2. earthwormjeff

    Thanks for this nice example. I suggest to use the contact listener between the sensor and the falling bodies (BeginContact and EndContact) to attach or detach to the buoyancyController.

  3. Pingback: Usando buoyancy controller de Box2D para crear una piscina | Todotuto.cl

Leave a Reply

Your email address will not be published. Required fields are marked *