1
votes

I'm trying to program a simple 2D platformer, and I want the player to go over some platforms. Unfortunately, my player stumbles after some platforms for no reason:

enter image description here

And that's clearly not what I want. The only thing I can imagine is that the boxes aren't the same height for some reason... But how to fix that? Here's some of my code:

createPlatform...

public static Body createPlatform(World world, int x, int y) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(new Vector2(x, y));
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(1, 1);
    Body body = world.createBody(bodyDef);
    body.createFixture(shape, Constants.GROUND_DENSITY);
    body.resetMassData();
    PlatformUserData userData = new PlatformUserData(1, 1, Constants.GROUND_IMAGE_PATH);
    body.setUserData(userData);
    shape.dispose();
    return body;
}

Runner.java - act()

@Override
public void act(float delta) {
    super.act(delta);
    body.setLinearVelocity(new Vector2(10f,0));
}
2

2 Answers

1
votes

When you have the ground made of multiple sections, the collision detection framework might detect the edge of the next block, like it was a little bit highrt than the previous. This is a computation error which happens with every sliding object sometimes. This is why they often set the player's shape to be an ellipse, and freeze the rotation of the object.

0
votes

The problem is that the player fictions against the ground so velocity is greater than the friction support so the body stumbs.

You have to put:

body.setFixedRotation(true);

Or go down the value of the friction.