0
votes

I have an issue. I'm creating a game using libGDX and have been following this tutorial to make it (https://www.youtube.com/watch?v=-Z_N5gHcl0k). So I follow the tutorial and yeah everything went well. Now I start a new project based on what I learned in the tutorial (everything (like the camera and such) is already set up properly)!

My issue is with Box2D where I add a shape to a fixture.

Let's say I'm making a box.

public void createBox() {
    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();

    bdef.position.set(200 / PPM, 500 / PPM);
    bdef.type = BodyType.DynamicBody;
    Body body = world.createBody(bdef);

    PolygonShape shape = new PolygonShape();

    shape.setAsBox(hx/ PPM, hy/ PPM);
    fdef.shape = shape;
    fdef.filter.categoryBits = B2DVars.BIT_BOX;
    fdef.filter.maskBits = B2DVars.BIT_PLATFORMS;
    body.createFixture(fdef).setUserData("box");
}

PPM is equal to 100. It's basically used to scale down to the amount of pixels per meter in Box2D.

My issue is here:

Shape.setAsBox(hx / PPM, hy/ PPM);

hx and hy stand for half of x and half of y. so if I put 50 for hx, it's actually going to be 100.

 shape.setAsBox(100/ PPM, 100 / PPM); <- Box

 shape.setAsBox(99/ PPM, 99/ PPM); <- No Box

It's as if I never created a shape or fixture...

SO I did some testing. I tried:

setAsBox(99 * 2 / PPM, 99 * 2 / PPM).

Guess what...NOTHING!

Then I used that exact statement but with 100 as a value instead. Woah A giant box!

Then if I do:

setAsBox(99 / PPM, 100 / PPM)

I get a giant line above (on the Y axis).

Then if I do:

setAsBox(100 / PPM, 99 / PPM)

I get a giant line onfront (on the X axis).

So I ignored this issue at first. But now when I want to load a sprite (basically I created the player), the shape issue is preventing me from having proper collision with platforms.

Surprisingly though, the player sprite decides to fall halfway through the platforms I have created and stops falling (as if collision had occurred. This only happens if hx and xy are below 100 (so no box). But then if I they're 100 or above, the box appears and I have proper collision (except that due to the size of the box, the player sprite/position is in the air lol).

Now to detect collision for the player, I created a foot sensor which go's under the player. I also have an issue here. I create a box and give it a Vector2 for it's center position. The Y value for the Vector 2 MUST be -100 for the box to appear. If it's anything above or below, nothing appears. The X value MUST be 0.

shape.setAsBox(100 / PPM, 100 / PPM, new Vector2(0, -100 / PPM), 0); <- Box

shape.setAsBox(99/ PPM, 99/ PPM, new Vector2(0, -99 / PPM), 0); <- No Box

Code for player

private void createPlayer() {
    MapLayer layer = map.getLayers().get("Spawn");
    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();

    // Get X and Y coords of the playerSpawn map object
    float x = layer.getObjects().get("playerSpawn").getProperties()
            .get("x", Float.class);
    float y = layer.getObjects().get("playerSpawn").getProperties()
            .get("y", Float.class);

    // set position of body
    bdef.position.set(x / PPM, y / PPM);
    bdef.type = BodyType.DynamicBody;
    // bdef.linearVelocity.set(1f, 0);
    Body body = world.createBody(bdef);

    PolygonShape shape = new PolygonShape();

    shape.setAsBox(100 / PPM, 100 / PPM);  <- Issue code
    fdef.shape = shape;
    fdef.filter.categoryBits = B2DVars.PLAYER;
    fdef.filter.maskBits = B2DVars.BIT_PLATFORMS;
    body.createFixture(fdef).setUserData("player");

    // create foot sensor  <- Used for collision detection at players feet
    shape.setAsBox(100 / PPM, 100 / PPM, new Vector2(1, -100 / PPM), 0); <- Issue code
    fdef.shape = shape;
    fdef.isSensor = true;
    fdef.filter.categoryBits = B2DVars.PLAYER;
    fdef.filter.maskBits = B2DVars.BIT_PLATFORMS;
    body.createFixture(fdef).setUserData("foot");

    player = new Player(body);

    body.setUserData(player);

}

Now the reason why platforms are working is because I'm using a system (found here: http://bitbucket.dermetfan.net/libgdx-utils/wiki/Box2DMapObjectParser) that reads objects from the tiled map (using Tile Map Editor), and makes a body and fixture off of em. I disabled the system incase of it being responsible by causing errors, but nope, still the same issue with the box!

The game in the tutorial has different window dimensions than mine by the way. BUT, switched them to the dimensions in the tutorials and still no issue, so that isn't the problem.

What am I doing wrong? The rest of the code is fine (I even went back to the tutorial's source code just to make my code identical)!

1

1 Answers

0
votes

If your PPM is defined as 100 it has int type. Thus when you write 99 / PPM its actually zero since dividing integer by integer provides an integer which is truncated. Change your PPM to be 100.0f (so its type is float now)