Jumping physics

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
luaz
Citizen
Posts: 83
Joined: Sun Sep 16, 2012 2:55 pm

Jumping physics

Post 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:
If you're going to reply to my post, consider posting an (preferably working) example - 99.7% of time time, I already know how to implement the feature theoretically! ;) I don't learn very well from references, etc....
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Jumping physics

Post 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".
luaz
Citizen
Posts: 83
Joined: Sun Sep 16, 2012 2:55 pm

Re: Jumping physics

Post 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.
If you're going to reply to my post, consider posting an (preferably working) example - 99.7% of time time, I already know how to implement the feature theoretically! ;) I don't learn very well from references, etc....
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Jumping physics

Post 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.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Jumping physics

Post 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
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
luaz
Citizen
Posts: 83
Joined: Sun Sep 16, 2012 2:55 pm

Re: Jumping physics

Post 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!
If you're going to reply to my post, consider posting an (preferably working) example - 99.7% of time time, I already know how to implement the feature theoretically! ;) I don't learn very well from references, etc....
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Jumping physics

Post 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.
luaz
Citizen
Posts: 83
Joined: Sun Sep 16, 2012 2:55 pm

Re: Jumping physics

Post 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:
If you're going to reply to my post, consider posting an (preferably working) example - 99.7% of time time, I already know how to implement the feature theoretically! ;) I don't learn very well from references, etc....
User avatar
entozoon
Prole
Posts: 2
Joined: Tue Nov 27, 2012 12:22 pm

Re: Jumping physics

Post 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)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests