Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (11 votes, average: 4.91 out of 5)
Loading...Loading...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 5 comments

  1. siddharth shekar

    on February 25, 2013 at 7:45 am

    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. Fábio Colombini

    on February 25, 2013 at 9:46 am

    Thanks Emanuele, great example!

  3. earthwormjeff

    on February 26, 2013 at 8:51 am

    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.

  4. Usando buoyancy controller de Box2D para crear una piscina | Todotuto.cl

    on November 13, 2013 at 9:02 pm

    […] Fuente original: emanuelefenorato.com […]

  5. Joe

    on July 30, 2014 at 5:59 pm

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

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×