Tobls
Tobls

Reputation: 67

Libgdx box2d Tiled map

I have a problem: I am programming a tiled map game with Box2D but the proble is, that if I press for example D to go forward with my character, a vector 2 makes me going faster and faster so I did this:

if (Gdx.input.isKeyJustPressed(Input.Keys.W) && player.b2body.getLinearVelocity().y == 0)
            player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true);
        if (Gdx.input.isKeyPressed(Input.Keys.D))
            player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
        if (Gdx.input.isKeyPressed(Input.Keys.A))
            player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);
if (player.b2body.getLinearVelocity().x > 2) {
        player.b2body.setLinearVelocity(2, player.b2body.getLinearVelocity().y);
    }
    else if (player.b2body.getLinearVelocity().x < -2) {
        player.b2body.setLinearVelocity(-2, player.b2body.getLinearVelocity().y);
    }

So the player has a maximum speed of two. But when he hits the ground he is slower for around a half second because he gets faste in the air. How ca I fix that?

And my second questin is: When I jump and press jump again right after I hit the goround the character doesnt jump! Why and how can I fix that?

Hope you can help me and thanks in advance!

Upvotes: 2

Views: 292

Answers (2)

Netero
Netero

Reputation: 3819

this is how you can add userData to a body

... // Define your bodyDef
Body body = world.createBody(bodyDef);
... // Define your fixtureDef   
Fixture fixture = body.createFixture(fixtureDef);
String userData ="MyBody";
fixture.setUserData(userData);

when you put the fixture definition to your body you can get the fixture just put the userdata (unique string ex : "mybody") to the fixture

PS: You can put the same userData to many bodies (a group of body) if you want.

Upvotes: 0

Netero
Netero

Reputation: 3819

But when he hits the ground he is slower for around a half second because he gets faste in the air. How ca I fix that?

I think that is because of the friction, try to set the friction to zero in the BodyDef when you create the body

When I jump and press jump again right after I hit the ground the character doesn't jump! Why and how can I fix that?

i propose to you to do this :

if (Gdx.input.isKeyJustPressed(Input.Keys.W) && isPlayerOnGround)
      player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getPosition(), true);

using world contact we detect if the player is on the ground

world.setContactListener(new () {
            @Override
            public void endContact(Contact c) {

                Fixture fa = c.getFixtureA();
                Fixture fb = c.getFixtureB();

                /* end Contact
                 * <<Player>> <---> <<Gound>>
                 */
                if(fa.getUserData() !=null && fb.getUserData() !=null && 
                           ((fa.getUserData().equals(playerUserData) && fb.getUserData().equals(groundUserData)) || 
                             fb.getUserData().equals(playerUserData) && fa.getUserData().equals(groundUserData)))
                {
                        isPlayerOnGround = false;
                }
            }
            @Override
            public void beginContact(Contact c) {

                Fixture fa = c.getFixtureA();
                Fixture fb = c.getFixtureB();

                /* Begin Contact
                 *  * <<Player>> <---> <<Gound>>
                 */
                if(fa.getUserData() !=null && fb.getUserData() !=null && 
                           ((fa.getUserData().equals(playerUserData) && fb.getUserData().equals(groundUserData)) || 
                             fb.getUserData().equals(playerUserData) && fa.getUserData().equals(groundUserData)))
                {
                        isPlayerOnGround = true;
                }
            }
        });

hope that was clear and helpful :=)

Upvotes: 1

Related Questions