Page 1 of 2

Platformer Jump Help

Posted: Thu Apr 30, 2015 7:56 pm
by ArterianGames
Hey ;) Im working on a platformer and I need some help with jumping... How can I make it so I cant fly with it....

Code: Select all

function player.controls(dt)
	if isDown("w") and
	player.yv < 5 then
      	  player.yv = -player.ms
    end
end


function player.physics(dt)
	if player.y < groundLevel then
		player.yv = player.yv + player.grav * dt
	end
end

Re: Platformer Jump Help

Posted: Thu Apr 30, 2015 8:12 pm
by ivan
What you have there is the 'jetpack' effect where the velocity is increased over time.
Ideally you want to change the velocity once - when the player presses the jump button...
then you allow gravity to pull the player down.
However if the player releases the jump button while he is still ascending, you set his vertical velocity to 0.
That's the simplest approach and a good starting point.

I wrote a short tutorial about it:
http://2dengine.com/doc_1_3_10/gs_platformers.html

Re: Platformer Jump Help

Posted: Thu Apr 30, 2015 8:41 pm
by ArterianGames
This would be good, if I understood anything... I need a simple gravity and jump thing :D

Re: Platformer Jump Help

Posted: Thu Apr 30, 2015 11:47 pm
by Robin
The player should only be able to jump when on the ground, something like:

Code: Select all

function player.controls(dt)
	if player.canjump and isDown("w") and
	player.yv < 5 then
      	  player.yv = -player.ms
          player.canjump = false
    end
end


function player.physics(dt)
	if player.y < groundLevel then
		player.yv = player.yv + player.grav * dt
        else
                player.canjump = true
	end
end

Re: Platformer Jump Help

Posted: Fri May 01, 2015 8:36 am
by ArterianGames
Thank you Robin for the reply ;) It helped a lot! But now if I jump he's just teleports (jumpHeight amount) up, and then down... I would like to make it more smooth... Any suggestions?

Re: Platformer Jump Help

Posted: Fri May 01, 2015 9:39 am
by Shaddy
As far as I can tell from the code, you would basically need a way to slowly decrease the players velocity. So when the velocity going up after the jump is set, then you need something that every frame would reduce it by the gravity.
Eg.
love.load()
player.yv = 10
gravity = 2
end
love.update(dt)
player.yv = player.yv - gravity
end

(Im assuming you want something jumping wise similiar to this: https://drive.google.com/file/d/0B8Ox_E ... sp=sharing controls are WASD)

Re: Platformer Jump Help

Posted: Fri May 01, 2015 9:56 am
by ArterianGames
Yeah okay... I did that already, but how to make jump smoother?

Code: Select all

function player.controls(dt)
   if player.canjump and isDown("w") and
   player.yv < 5 then
           player.yv = -player.ms
          player.canjump = false
    end
end
Now it just teleport the player player.ms up...

Re: Platformer Jump Help

Posted: Fri May 01, 2015 1:47 pm
by Robin
Could you upload a .love? It looks like your code conflates player.y and player.yv somehow.

Re: Platformer Jump Help

Posted: Fri May 01, 2015 4:21 pm
by ArterianGames
Emm... I think this is it? :D

Edit: Forgot to put the image in it :S

Re: Platformer Jump Help

Posted: Fri May 01, 2015 6:32 pm
by Robin
Ehm, I can't run the .love or extract the files from it. Are you sure you put your files in a .zip archive, and didn't use some other archive format, like .rar?