Box2D Platformer Character Controls

For QBOB we are using box2d as the driving physics engine. To integrate everything tightly we want box2d to drive the movement of our characters. I wrote a little prototype for that a while ago and it works surprisingly well. Here’s a video:

Works pretty well, and doesn’t even need pre/postSolve 😀

And here are the crown jewels:

Given that it’s a prototype it’s of course a bit of a mess. I put this out without any kind of support or explanation. It’s like with the holy grail, if you are worthy of it, it shall be yours. Bugs and corner cases that make things explode are all yours as well. No go out and write some crazy ass box2d platformers!

  • cyble

    scr*w the grail, this is sweeter 😀

  • glenn

    hey it’s not Christmas yet. I’ll be using this for sure. thank you Mario

  • http://noneyet frank

    Definitively great… I grab it too and will reuse it for sure.
    Lazy programmers in herbs 🙂

  • Doctor Funfrock

    when i run this on my htc desire s the framerate is VERY low.
    should i have expected that?

  • Mario

    yes, it uses the box2ddebugrenderer

  • David Clutter

    This is great but it no longer works with 0.9.2 is there anyway we can get an update. Thanks for all the great work.

  • Mario

    It’s in the gdx-tests project, see Box2DCharacterControllerTest.

  • godslave

    I’m not seeing the following methods in my imported classes .

    -> poly.setAsEdge(new Vector2(0, 0), new Vector2(x2 – x1, y2 – y1));

    i used setAsBox instead of setAsEdge, and try using renderer.render(world, projMatrix) instead of the first one.
    but it invoking null pointer exception.
    I’m new with Box2d. And it’s just as u said “holy grail”, pls help me

  • Lomelin

    I’ve the same problem, I’m still looking..

  • buddyvegas


    i’m using libgdx-0.9.4 and i change these lines :
    -> poly.setAsEdge(new Vector2(0, 0), new Vector2(x2 – x1, y2 – y1));

    to :
    => renderer.render(world, cam.combined);
    => poly.setAsBox(x2-x1,y2-y1);

    i hope it helps you or anyone else 🙂

  • buddyvegas

    my previous post has a bug when click with the mouse

    here a little fix
    replace this :
    PolygonShape poly = new PolygonShape();
    poly.setAsEdge(new Vector2(0, 0), new Vector2(x2 – x1, y2 – y1));

    by this :
    EdgeShape poly = new EdgeShape();
    poly.set(new Vector2(0, 0), new Vector2(x2 – x1, y2 – y1));

    sorry for the double post 😉

  • João Neto

    That’s it! I start to learn Box2D, because create a custom platform system is painful. But what i need to do for the player stay over the horizontal moving platform, like mario games, when the platform moves, it moves the player together.


  • Christopher Rowlett

    Thanks, buddyvegas.

    And of course, thanks Mario.

  • Dan

    In new Box2D player is not moving with horizontal moving platform.

  • Suvitruf

    “Dan” rly?

    It’s true only if Friction is big.

    For example:

  • Suvitruf

    Guys, I have a problem.

    dodn’t work when player on MovingPlatform =/

    Only if i set it for contact in isPlayerGrounded
    “contact.setFriction(100f);” it’s works -_-

  • Ben Cochrane

    I really love the use of &= in isPlayerGrounded() ! I’ll have to steal that for future use >:D

  • :[]

    could you expand on what is going on in your isplayerground method? i have reworked the code to work in my app but i am having inconsistent jumps… some long and tall, some short…

    hoping for some insight on the manifolds and the below variable?

  • QStorm

    Hi, whats the purpose of the transform call “player.setTransform(pos.x, pos.y + 0.01f, 0);” on jump?

  • ArcTanGentleman

    No license? With a license, maybe someone could un-messify and debug it.