Platform engine using Box2D – Step 3

In this third part of the platform engine, it’s time to make our hero jump.

First, I suggest you to read parts 1 and 2 if you already didn’t, then you should know something about the magic of compound objects and how Box2D manages collisions.

Then, you’re ready to follow the tutorial :)

The hero in order to jump must be on the ground, or over some solid object.

The main idea is creating the hero as a compound object made with the hero itself (the big rectangle) and a ground sensor (the small rectangle under the big one) that will be triggered every time the hero stands over something solid, without interacting with the world.

Then, we should check the sensor to collide with objects according to the response decide if the hero can or can’t jump.

Believe me, it’s easier than it seems. This is the main script:

and I changed b2ContactListener.as as follows:

Playing a bit with impulses and bullets size, I made my hero shooting something like crates… and he can jump over them to reach higher places… needs some tweaks but it’s an interesting gameplay in my opinion…

Left/right arrow keys to move, up to jump and mouse to aim and shoot.

Download the source code, libraries included.

  • Eliott Robson

    Wow, very good. Keep up the good work, although it just feels slippery..

  • I agree, it’s great, but if you are planning on having jumping on crates be a gameplay element, you might want to boost the friction

  • Steve

    You’re not supposed to edit the b2ContactListener Class like this (hence the virtual functions); it should be extended and the methods overridden.

  • Great tutorial thanks, cant wait to mess about with this once i have finished re-installing AS3

  • Guest

    Great tutorial, Thanks….
    Can you do more tutorials about facebook applications?… Like how to make a ranking of facebook friends with your games?…. Regards

  • aseneo

    Cool! waiting for next step :)

  • Looks more like a dude on a jet or hover pack than a jumping one.

  • I did a platform engine (pretty simple) for a game I’m developing. It’s way simpler, but very effective. If you want, I can send you the .as!

  • Great tutorial, but I’ve kind of found a glitch or a trick, I can’t tell. If you jump in the air and them shoot at fast as you can, you’ll step on each one of your crates and fly! Either way, this is a good demonstration of Box2D.

  • Pingback: Creation of a Flash artillery game using Box2D : Emanuele Feronato()

  • Jean

    Hi there. Nice website you have there. Great tutorials.
    I’m using this tutorial to make a flying character. The problem is that whenever it hits an obstacle in the air, the player’s body starts to rotate like crazy.
    How do I prevent this from happening? Is it possible?

  • Hauts

    Thanks from Russia! =)

  • Esteban

    Hi Emanuele, your tutorials are amazing!!

    Can you help me with this little problem?
    I have added a sprite to the stone thrown, but when another collides, the first sprite does not follow the stone that was hit.

    Problem would be when to update the position of the sprite being hit

    I have uploaded the source code http://www.megaupload.com/?d=WCETLYMH

    thanks!!!