Using Box2D buoyancy controller to create a pool

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.

  • 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.

  • Thanks Emanuele, great example!

  • 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.

  • Pingback: Usando buoyancy controller de Box2D para crear una piscina |

  • Joe

    Awesome tutorial. Following up on the first comment – How would one go about creating ripples when the objects hit the water?