简体   繁体   中英

How to make sure a Dynamic body has no gravity applied to it in libgdx box2d

I have a body following my player in a game I made and the player is of type Actor (scene2D). In his overriding act method he uses libgdx input to move. For example,(pseudo) if(gdxInput.keys(keys.up)){ applyForceToCenter(VElocity) xCoor = body.x yCoor = body.y

How do I make sure the body doesn't slide all over the place? The world has a gravity of 0,0 so if i click the up arrow the player will never stop so in my huge if else if block statement of input i put else linearVelocity = 0 this works however if the player is holding the right arrow key then holds the up arrow key the player moves right more then up as if he is sliding on ice. Please tell me how to turn all gravity off of the player in general. I can't just set the position of the body because im using the bodies as a way of collision and to set the position of the body is to turn off collision.

Have you got any damping set on your player body?

playerBody.SetLinearDamping(0.2f);

This will reduce positional forces to 0 over time if you're not applying anything to your playerBody.

As soon as you want to stop you will set the velocity to 0 for example

    if(!canFall){
        velocity.y = 0;
    }else
        velocity.y += MainScreen.GRAVITY.y;

    if(Gdx.input.isKeyPressed(Keys.A)){
        velocity.x -= 10 * Gdx.graphics.getDeltaTime();
    }
    else if(Gdx.input.isKeyPressed(Keys.D)){
        velocity.x += 10 * Gdx.graphics.getDeltaTime();
    }else
        velocity.x = 0;

So once you stop ur velocity will be equal to 0!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM