2

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!

Tobls
  • 67
  • 7
  • i wonder if your problem was solved, if yes could you please turn your question as resolved by choosing correct answer, thank you – Netero Dec 04 '15 at 10:25

2 Answers2

1

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

Netero
  • 3,761
  • 2
  • 29
  • 29
0

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.

Netero
  • 3,761
  • 2
  • 29
  • 29
  • ok it worked now but it doesnt make a difference.Do you know another method? – Tobls Oct 27 '15 at 16:11
  • what do you mean it works but doesn't make a difference ? you couldn't do a second jump ? you could you explain what woks and what not ;) – Netero Oct 27 '15 at 18:42