0
votes

I'm making a game using LibGDX, and with LibGDX, the Box2D wrapper that comes with it. Specifically, my game is a 2D sidescroller.

My problem is with my player sprite. I need very precise movement for the player, so I decided I would set it up so when the player presses an arrow key, it would call playerBody.setLinearVelocity(), and then when they stopped pressing the keys, it would reset their linear velocity to 0.

In my game, I have gravity. To make sure the player falls while moving left and right, I created the method run():

public void run(float x) {
    playerBody.setLinearVelocity(x, playerBody.getLinearVelocity().y);
}

This works fine when my player is freefalling. However, when my player moves against any static body (including vertical walls), they stop falling for as long as I'm holding down the arrow key that sticks them to the wall.

Does anyone know why this might be? Thanks in advance.

Also, here's my friction, density, and restitution for the playerBody:

friction = 0.1f
restitution = 0.01f
density = 0.4f
1

1 Answers

1
votes

I am considering that your player body is Dynamic and a dynamic body has a property of colliding with all the static bodies. So, if you take a look at your run method, you are passing the previous LinearVelocity().y of player in your current players LinearVelocity(). So, when your player's body collide with a static body its velocity become 0 and after collision if its Velocity().y becomes 0 and you are keep on passing that 0 in your playerBody.setLinearVelocity your body will not move. Something is happening like that.

public void run(float x) {
    playerBody.setLinearVelocity(x, 0);
}

after collision. I think if you pass the gravity at your velocity().y position.I think it will solve your problem.

public void run(float x) {
    playerBody.setLinearVelocity(x, gravity.y);
}