LibGDX Box2D Prevent object from slowing down after jumping









up vote
1
down vote

favorite
1












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.










share|improve this question























  • 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














up vote
1
down vote

favorite
1












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.










share|improve this question























  • 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












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3