Page 1 of 1

Jumping physics

Posted: Wed Sep 26, 2012 8:24 pm
by luaz
I can't manage to make the jump physics to work as I want: I want the character to jump depending on how long the key is being held down.

Here is my code:

Code: Select all

-- jump speed is equal to jump speed minus jump acceleration
if (love.keyboard.isDown(' ')) then this.jspeed = this.jspeed - this.jspeedacc
P.S. I hate the "x = x - y" syntax, rather than "x -= y!" :halloween:

Re: Jumping physics

Posted: Wed Sep 26, 2012 8:48 pm
by micha
This article http://higherorderfun.com/blog/2012/05/ ... atformers/ has some thoughts on jump physics. Scroll down all the way to "jump control".

Re: Jumping physics

Posted: Wed Sep 26, 2012 9:00 pm
by luaz
micha wrote:This article http://higherorderfun.com/blog/2012/05/ ... atformers/ has some thoughts on jump physics. Scroll down all the way to "jump control".
Thanks, but I already get the concept. What I don't get is how to write it down in code. I have it gradually slowing down when going up, but it jumps as far as I have defined instead of gradually jumping in regards to how long the key is held down, which is what I want.

Re: Jumping physics

Posted: Wed Sep 26, 2012 9:14 pm
by Roland_Yonaba
Just search in the support and development threads, this have been covered a lot.
You can also give a look at this tutorial series, there's a handy solution provided there, in Lua, with Love2D.

Re: Jumping physics

Posted: Wed Sep 26, 2012 9:33 pm
by Lafolie
luaz wrote:P.S. I hate the "x = x - y" syntax, rather than "x -= y!" :halloween:
Try this: https://github.com/bartbes/Meta

Re: Jumping physics

Posted: Wed Sep 26, 2012 9:47 pm
by luaz
Roland_Yonaba wrote:Just search in the support and development threads, this have been covered a lot.
You can also give a look at this tutorial series, there's a handy solution provided there, in Lua, with Love2D.
Unfortunately the tutorial series seem to have the same jumping mechanism as I do. And I've tried searching the thread already, before posting. :)
Lafolie wrote:
luaz wrote:P.S. I hate the "x = x - y" syntax, rather than "x -= y!" :halloween:
Try this: https://github.com/bartbes/Meta
That's nice! Thank you!

Re: Jumping physics

Posted: Thu Sep 27, 2012 7:40 am
by micha
Try the following: Introduce a jump timer, that counts the time (or the number of frames) since you pressed the jump key.

Code: Select all

jumpTimer = 0
In the callback function love.keypressed( ) you put something like

Code: Select all

if canJump then
  camJump = false
  player.vecolityY = jumpVelocity
  jumpTimer = maxJumpTime
end
In the love.update() you put

Code: Select all

jumpTimer = math.max(jumpTimer - dt,0) 
if love.keyboard.isDown(jumpKey) and jumpTimer>0 then
  player.vecolityY = player.velocityY - playerJumpAcceleration * dt
end
If you are not using dt, replace it by 1.

So whenever a jump is initiated, the timer counts down from maxJumpTime (in this example) to 0. If the jumpKey is pressed and the timer has not yet counted down to 0, additional acceleration is added to the jump.

The math.max function ensure that the value never is negative.

Try experimenting with the parameters maxJumpTime and playerJumpAcceleration. Note: playerJumpAcceleration should be smaller than gravity. If it is not, the player will jump like a starting rocket.

Re: Jumping physics

Posted: Fri Sep 28, 2012 8:11 pm
by luaz
micha wrote:The math.max function ensure that the value never is negative.

Try experimenting with the parameters maxJumpTime and playerJumpAcceleration. Note: playerJumpAcceleration should be smaller than gravity. If it is not, the player will jump like a starting rocket.
Thanks, I will definitely try it out! I've implemented the same idea, but it wasn't that well done, no dt and all. Right now I'm rewriting the game code from scratch, and at the moment I'm struggling at collision, as mentioned in another thread.

As my sig says - I know the theory, but I struggle with implementation. ;)

Will let you know my results! :ultraglee:

Re: Jumping physics

Posted: Tue Nov 27, 2012 12:24 pm
by entozoon
How did you get on with this? I'm a beginner trying to make a platformer and at the minute the jumping and running has infinite acceleration (either full speed or none)