Build 10 classic Flash games and learn game development along the way with this ultra-fast paced game development course.

If you love this blog, this is the book for you.

Buy the book

Get the source code of 12 commercial Flash games, which have been loaded more than 50 million times!

Learn from real world successful examples.

Get it now

Box2D for Flash Games teaches you how to make Flash physics games from scratch with the most advanced features.

Create the new Flash game smashing hit.

Buy the book

Understanding Box2D’s one-way platforms, aka CLOUDS

One of the new features introduced with Box2D 2.1a is the improved contact listener class which comes in hand when we want to create one-way platforms, or “clouds”.

This can be made thanks to a function called before the contact is processed… something like “hey, two bodies are about to collide, what should I do?”… so you can decide to disable the contact for every collision you want.

The function used to do this task is PreSolve, working for all awake bodies that aren’t sensors.

If you don’t know what is a Box2D sensor, check Box2D Flash game creation tutorial – part 2.

So the concept is: listen for collisions, if a collision involves the cloud wall and the player, then check if the player is higher or lower than the cloud. If it’s lower, don’t process the collision and let the player fly through the cloud.

Let’s see the script, directly taken from Box2D Flash game creation tutorial – part 2:

Line 34: with the old function introduced at Understanding Box2D applicable forces I create a box marked as “middle”.

No other changes on the main file, now let’s see the custom_contact_listener.as file

Line 22: beginning of the PreSolve function, the core of this example

Lines 24-25: getting the fixtures that generated the contact

Lines 27-28: declaring two variables to store y position of both bodies

Line 30: here I am checking if the fixtures are the one associated to the player and the one associated to the cloud

Lines 32-43: according to the fixture associated to the player and the one associated to the cloud, I am saving in the variables declared at lines 27-28 the y position of both bodies. I am multiplying directly by 30 without passing the right world_scale value declared at line 13 of the main class because it’s not the purpose of this tutorial.

Line 45: Determining the vertical distance from the player and the cloud

Lines 47-50: If the player is not at least 14.5 pixels higher than the cloud, then disable the contact. Why 14.5? It’s the sum of the ball radius (10) and half the cloud height (5)… and I am not using 15 because I found sometimes the distance when the ball falls on the cloud is 14.93, so the cloud won’t “hold” the ball, letting it fall down. With 14.5, I am sure this won’t happen.

And this is the result…

Move the ball by tapping arrow keys and watch the static object in the center of the stage act as a cloud… you can fly through it from bottom to top, but you can’t do it from top to bottom.

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 4.92 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 10 comments

  1. Monkios

    on March 3, 2010 at 4:07 pm

    This is really nice.

    How tough would it be to add some bouncing on the ball ?

  2. rishabh

    on March 3, 2010 at 4:54 pm

    @monkios
    just add some restitution (0.3 for ex)

  3. Quintus

    on March 11, 2010 at 12:43 pm

    Niceeeee :)

  4. Quintus

    on March 14, 2010 at 3:06 am

    Greetings!

    I am trying to make a box2d game with this tutorial. It’s a sort of biljart game.

    I don’t know when 2 biljart balls hittest each other. So my question is: how can i declare if a ball hits another ball?

    -Quintus

  5. encoder

    on March 22, 2010 at 3:18 pm

    lol
    @1. Monkios
    it is something like writing 6 characters.

    Emanuele Feronato
    if you read yr comments pls read this post:
    http://szeredai.wordpress.com/2010/03/22/from-design-to-physix-enabled-object-under-5-minutes/

    wanted to ask you something a long time ago:
    why are you throwing all yr code in a single class? i know it’s only 200 lines but still..

    kinda started my blog. it is for putting things out there and i tend not to advertise with it ;)

  6. artyangst

    on April 22, 2010 at 1:52 am

    Great tutorial, but my question is a little more advanced. This only works on horizontal platforms. For something like a slanted platform, the player could still have a center of mass above the platform, yet actually be “below” the platform. So I could figure this out if I knew the point of contact in worldspace. Any tips on how to figure this out? :)

  7. Heorhiy

    on May 19, 2011 at 9:16 am

    2artyangst
    Hi! have figured it out?

  8. shakespeare

    on June 6, 2011 at 5:09 pm

    instead of using the distance calculation, you could check the velocity of the character you are moving, and if it is moving “upward”, you could set the contact enabled to false.

  9. Brad Henderson

    on July 20, 2011 at 10:15 pm

    YOU ROCK!!! It took me only like 30 mins to get his implemented and I just started using BoX2d yesterday. Many many many THANKS!!!

  10. Bananni

    on January 10, 2012 at 9:23 pm

    your “SUPPORTED BY” section loaded a side that sends viruses, better take a look on it ;)

    opbvhdjehh(dot)biz/index.php?tp=89c9473171a1c848

    was it