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 :D

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 :D

  • 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 :-)

  • http://www.squar.info Doctor Funfrock

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

  • http://badlogicgames.com 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.

  • http://badlogicgames.com Mario

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

  • http://www.rhymesnetwork.com godslave

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

    ->renderer.renderBodies(world);
    -> 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

    @godslave

    i’m using libgdx-0.9.4 and i change these lines :
    ->renderer.renderBodies(world);
    -> 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 ;)

  • http://www.deploy.net.br 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.

    Thanks!

  • Christopher Rowlett

    Thanks, buddyvegas.

    And of course, thanks Mario.

  • Dan

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

  • http://suvitruf.ru Suvitruf

    “Dan” rly?

    It’s true only if Friction is big.

    For example:
    playerSensorFixture.setFriction(100);

  • http://suvitruf.ru Suvitruf

    Guys, I have a problem.

    “playerPhysicsFixture.setFriction(100f);
    playerSensorFixture.setFriction(100f);”
    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?