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!

20 thoughts on “Box2D Platformer Character Controls

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

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

  3. @godslave

    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 🙂

  4. 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 😉

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


  6. 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 -_-

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

  8. 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?

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

Leave a Reply

Your email address will not be published.