LibGDX Box2D Prevent object from slowing down after jumping
up vote
1
down vote
favorite
I'm coding a 2d platformer in libGDX with Box2D, and I just realized that if your object jumps while running and then lands on a surface to continue running, he slows down by like 120% for about a second. That's really annoying, and I can't really come up with a solution. I've tried making the speed higher for a second after the player hits the ground after jumping, but that doesn't really seem to do the trick. This is what I'm using to move the player:
//jumping
if(Gdx.input.isKeyJustPressed(Keys.SPACE) && player.b2body.getLinearVelocity().y == 0)
player.b2body.applyLinearImpulse(new Vector2(0, 4.2f), player.b2body.getWorldCenter(), true);
//moving right
if(Gdx.input.isKeyPressed(Keys.D) && player.b2body.getLinearVelocity().x <= 2)
player.b2body.applyLinearImpulse(new Vector2(0.17f, 0), player.b2body.getWorldCenter(), true);
//moving left
if(Gdx.input.isKeyPressed(Keys.A) && player.b2body.getLinearVelocity().x >= -2)
player.b2body.applyLinearImpulse(new Vector2(-0.17f, 0), player.b2body.getWorldCenter(), true);
Here is a video showing what's happening.
I hope I've provided the correct information, please tell me if I haven't.
EDIT: I've also tried setting the position in the x-axis with
player.b2body.setLinearVelocity(1f, 0);
but that seems to make the player sort of glide-fly to the right.
java libgdx box2d physics
|
show 4 more comments
up vote
1
down vote
favorite
I'm coding a 2d platformer in libGDX with Box2D, and I just realized that if your object jumps while running and then lands on a surface to continue running, he slows down by like 120% for about a second. That's really annoying, and I can't really come up with a solution. I've tried making the speed higher for a second after the player hits the ground after jumping, but that doesn't really seem to do the trick. This is what I'm using to move the player:
//jumping
if(Gdx.input.isKeyJustPressed(Keys.SPACE) && player.b2body.getLinearVelocity().y == 0)
player.b2body.applyLinearImpulse(new Vector2(0, 4.2f), player.b2body.getWorldCenter(), true);
//moving right
if(Gdx.input.isKeyPressed(Keys.D) && player.b2body.getLinearVelocity().x <= 2)
player.b2body.applyLinearImpulse(new Vector2(0.17f, 0), player.b2body.getWorldCenter(), true);
//moving left
if(Gdx.input.isKeyPressed(Keys.A) && player.b2body.getLinearVelocity().x >= -2)
player.b2body.applyLinearImpulse(new Vector2(-0.17f, 0), player.b2body.getWorldCenter(), true);
Here is a video showing what's happening.
I hope I've provided the correct information, please tell me if I haven't.
EDIT: I've also tried setting the position in the x-axis with
player.b2body.setLinearVelocity(1f, 0);
but that seems to make the player sort of glide-fly to the right.
java libgdx box2d physics
I suspect it happens because the body collides with the ground and the force is therefore slowed down. What you could try is to apply a force when jumping, like you do. But when moving to the left and right, instead set its position
– Squiddie
Nov 11 at 15:04
Tried this, but that seems to give another weird result. I've edited my original post, so you can see it there. Thanks!
– ILikeJava
Nov 11 at 16:50
You have to reset the velocity when the user isn't pressing the button
– Squiddie
Nov 11 at 17:06
@Squiddie Found out the problem with this methode is that I need to set both velocity X and Y, which means the Y has to be affected. I'm sure there is a way to work around that, but I can't really figure out how..
– ILikeJava
Nov 14 at 20:30
Can't you just set y velocity to 0?
– Squiddie
Nov 14 at 21:30
|
show 4 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm coding a 2d platformer in libGDX with Box2D, and I just realized that if your object jumps while running and then lands on a surface to continue running, he slows down by like 120% for about a second. That's really annoying, and I can't really come up with a solution. I've tried making the speed higher for a second after the player hits the ground after jumping, but that doesn't really seem to do the trick. This is what I'm using to move the player:
//jumping
if(Gdx.input.isKeyJustPressed(Keys.SPACE) && player.b2body.getLinearVelocity().y == 0)
player.b2body.applyLinearImpulse(new Vector2(0, 4.2f), player.b2body.getWorldCenter(), true);
//moving right
if(Gdx.input.isKeyPressed(Keys.D) && player.b2body.getLinearVelocity().x <= 2)
player.b2body.applyLinearImpulse(new Vector2(0.17f, 0), player.b2body.getWorldCenter(), true);
//moving left
if(Gdx.input.isKeyPressed(Keys.A) && player.b2body.getLinearVelocity().x >= -2)
player.b2body.applyLinearImpulse(new Vector2(-0.17f, 0), player.b2body.getWorldCenter(), true);
Here is a video showing what's happening.
I hope I've provided the correct information, please tell me if I haven't.
EDIT: I've also tried setting the position in the x-axis with
player.b2body.setLinearVelocity(1f, 0);
but that seems to make the player sort of glide-fly to the right.
java libgdx box2d physics
I'm coding a 2d platformer in libGDX with Box2D, and I just realized that if your object jumps while running and then lands on a surface to continue running, he slows down by like 120% for about a second. That's really annoying, and I can't really come up with a solution. I've tried making the speed higher for a second after the player hits the ground after jumping, but that doesn't really seem to do the trick. This is what I'm using to move the player:
//jumping
if(Gdx.input.isKeyJustPressed(Keys.SPACE) && player.b2body.getLinearVelocity().y == 0)
player.b2body.applyLinearImpulse(new Vector2(0, 4.2f), player.b2body.getWorldCenter(), true);
//moving right
if(Gdx.input.isKeyPressed(Keys.D) && player.b2body.getLinearVelocity().x <= 2)
player.b2body.applyLinearImpulse(new Vector2(0.17f, 0), player.b2body.getWorldCenter(), true);
//moving left
if(Gdx.input.isKeyPressed(Keys.A) && player.b2body.getLinearVelocity().x >= -2)
player.b2body.applyLinearImpulse(new Vector2(-0.17f, 0), player.b2body.getWorldCenter(), true);
Here is a video showing what's happening.
I hope I've provided the correct information, please tell me if I haven't.
EDIT: I've also tried setting the position in the x-axis with
player.b2body.setLinearVelocity(1f, 0);
but that seems to make the player sort of glide-fly to the right.
java libgdx box2d physics
java libgdx box2d physics
edited Nov 11 at 16:56
asked Nov 10 at 18:59
ILikeJava
124
124
I suspect it happens because the body collides with the ground and the force is therefore slowed down. What you could try is to apply a force when jumping, like you do. But when moving to the left and right, instead set its position
– Squiddie
Nov 11 at 15:04
Tried this, but that seems to give another weird result. I've edited my original post, so you can see it there. Thanks!
– ILikeJava
Nov 11 at 16:50
You have to reset the velocity when the user isn't pressing the button
– Squiddie
Nov 11 at 17:06
@Squiddie Found out the problem with this methode is that I need to set both velocity X and Y, which means the Y has to be affected. I'm sure there is a way to work around that, but I can't really figure out how..
– ILikeJava
Nov 14 at 20:30
Can't you just set y velocity to 0?
– Squiddie
Nov 14 at 21:30
|
show 4 more comments
I suspect it happens because the body collides with the ground and the force is therefore slowed down. What you could try is to apply a force when jumping, like you do. But when moving to the left and right, instead set its position
– Squiddie
Nov 11 at 15:04
Tried this, but that seems to give another weird result. I've edited my original post, so you can see it there. Thanks!
– ILikeJava
Nov 11 at 16:50
You have to reset the velocity when the user isn't pressing the button
– Squiddie
Nov 11 at 17:06
@Squiddie Found out the problem with this methode is that I need to set both velocity X and Y, which means the Y has to be affected. I'm sure there is a way to work around that, but I can't really figure out how..
– ILikeJava
Nov 14 at 20:30
Can't you just set y velocity to 0?
– Squiddie
Nov 14 at 21:30
I suspect it happens because the body collides with the ground and the force is therefore slowed down. What you could try is to apply a force when jumping, like you do. But when moving to the left and right, instead set its position
– Squiddie
Nov 11 at 15:04
I suspect it happens because the body collides with the ground and the force is therefore slowed down. What you could try is to apply a force when jumping, like you do. But when moving to the left and right, instead set its position
– Squiddie
Nov 11 at 15:04
Tried this, but that seems to give another weird result. I've edited my original post, so you can see it there. Thanks!
– ILikeJava
Nov 11 at 16:50
Tried this, but that seems to give another weird result. I've edited my original post, so you can see it there. Thanks!
– ILikeJava
Nov 11 at 16:50
You have to reset the velocity when the user isn't pressing the button
– Squiddie
Nov 11 at 17:06
You have to reset the velocity when the user isn't pressing the button
– Squiddie
Nov 11 at 17:06
@Squiddie Found out the problem with this methode is that I need to set both velocity X and Y, which means the Y has to be affected. I'm sure there is a way to work around that, but I can't really figure out how..
– ILikeJava
Nov 14 at 20:30
@Squiddie Found out the problem with this methode is that I need to set both velocity X and Y, which means the Y has to be affected. I'm sure there is a way to work around that, but I can't really figure out how..
– ILikeJava
Nov 14 at 20:30
Can't you just set y velocity to 0?
– Squiddie
Nov 14 at 21:30
Can't you just set y velocity to 0?
– Squiddie
Nov 14 at 21:30
|
show 4 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242390%2flibgdx-box2d-prevent-object-from-slowing-down-after-jumping%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I suspect it happens because the body collides with the ground and the force is therefore slowed down. What you could try is to apply a force when jumping, like you do. But when moving to the left and right, instead set its position
– Squiddie
Nov 11 at 15:04
Tried this, but that seems to give another weird result. I've edited my original post, so you can see it there. Thanks!
– ILikeJava
Nov 11 at 16:50
You have to reset the velocity when the user isn't pressing the button
– Squiddie
Nov 11 at 17:06
@Squiddie Found out the problem with this methode is that I need to set both velocity X and Y, which means the Y has to be affected. I'm sure there is a way to work around that, but I can't really figure out how..
– ILikeJava
Nov 14 at 20:30
Can't you just set y velocity to 0?
– Squiddie
Nov 14 at 21:30