LibGDX Box2D Prevent object from slowing down after jumping










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 '18 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 '18 at 16:50











  • You have to reset the velocity when the user isn't pressing the button

    – Squiddie
    Nov 11 '18 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 '18 at 20:30











  • Can't you just set y velocity to 0?

    – Squiddie
    Nov 14 '18 at 21:30















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 '18 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 '18 at 16:50











  • You have to reset the velocity when the user isn't pressing the button

    – Squiddie
    Nov 11 '18 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 '18 at 20:30











  • Can't you just set y velocity to 0?

    – Squiddie
    Nov 14 '18 at 21:30













1












1








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 '18 at 16:56







ILikeJava

















asked Nov 10 '18 at 18:59









ILikeJavaILikeJava

125




125












  • 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 '18 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 '18 at 16:50











  • You have to reset the velocity when the user isn't pressing the button

    – Squiddie
    Nov 11 '18 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 '18 at 20:30











  • Can't you just set y velocity to 0?

    – Squiddie
    Nov 14 '18 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 '18 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 '18 at 16:50











  • You have to reset the velocity when the user isn't pressing the button

    – Squiddie
    Nov 11 '18 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 '18 at 20:30











  • Can't you just set y velocity to 0?

    – Squiddie
    Nov 14 '18 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 '18 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 '18 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 '18 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 '18 at 16:50













You have to reset the velocity when the user isn't pressing the button

– Squiddie
Nov 11 '18 at 17:06





You have to reset the velocity when the user isn't pressing the button

– Squiddie
Nov 11 '18 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 '18 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 '18 at 20:30













Can't you just set y velocity to 0?

– Squiddie
Nov 14 '18 at 21:30





Can't you just set y velocity to 0?

– Squiddie
Nov 14 '18 at 21:30












0






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',
autoActivateHeartbeat: false,
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

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)