javaandroidbox2dandengine

Moving Platform object - Android game using box2D


I have a 2D Side scrolling android game under development, using And Engine.

And Engine uses the BOX2D physics engine.

In my game I have an object called "moving platform". It's a simple platform, which is moving in right/left direction and constantly change its moving direction depending on its current X coordinate. It works fine and I'm moving it using:

    body.setLinearVelocity(-1 * 5, b.getLinearVelocity().y);

Now my problem is: obviously the player may try to jump on this moving platform to move with it, but unfortunately player will fall down if he doesn't move with this platform.

I'm trying to make it like in most platform games, where if the player is on the platform, the player moves with the platform by itself (so you are not forced to keep trying to stay on this platform to avoid falling down).

So far I have tried or have been thinking about:

Without success so far, I would be grateful for some ideas/tips. Thank you.


Solution

  • If the platform's friction coefficient is one, the player should not be able to have a relative velocity to it's velocity. So just set 1f as the friction field of the moving platform FixtureDef.

    If this doesn't work, make sure you didn't pass 0 as the density when creating the player fixture def, because kinematic friction is calculated as: friction=(friction coefficient)*(normal force) then a body without mass (density = 0), in most of the cases (Your is included in "most"), will have no friction.