Platformer Jumping -- how does it work?

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
User avatar
Cryogenical
Prole
Posts: 49
Joined: Mon Apr 28, 2014 5:23 pm

Platformer Jumping -- how does it work?

Post by Cryogenical » Fri May 09, 2014 8:01 pm

I'm relatively new to this whole markup. I've finished the college-level curriculum on Java, but it's a trash game development language for the projects I'm wanting to do. A friend recommended me to Love2D and I've been watching and reading up Lua and 2D game practices and the like. I've been working on a very very basic platformer to learn off of, and I have a friend who's working with me to some extent and we're both trying to learn.

Below I've attached the player.lua file that we found on a youtube tutorial and modified. The tutorial abruptly ended after implementing moving left and right and it barely touched on the physics side of things, but I did a good amount of research on the subject, yet it just doesn't click in my mind.

From what I understand, I know that the physics is being constantly updated therefore producing the gravity and the updating formuale for moving left and right, but what do I need to do for jumping for a set time/length? Do I need to have it execute a thread/timer/co-routine? I've been trying to mess around with a canJump boolean, but I'm not hitting the mark.

Code: Select all

player = {}

function player.load()
	player.x = 5
	player.y = 5
	player.width = 50
	player.height = 50
	player.xvel = 0
	player.yvel = 0
	player.friction = 7
	player.speed = 2250
	playerCanJump = true
	hitFloor = false
end
function player.draw()
	love.graphics.setColor(255,0,0)
	love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
end
function player.physics(dt)
	player.x = player.x + player.xvel * dt
	player.y = player.y + player.yvel * dt
	player.yvel = player.yvel + gravity * dt
	player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))
	--player.yvel = player.yvel * (1 - math.min(dt*player.friction, 1))
end
function player.move(dt)
	if love.keyboard.isDown('right') and
		player.xvel < player.speed then
		player.xvel = player.xvel + player.speed * dt
	end
	if love.keyboard.isDown('left') and
		player.xvel > -player.speed then
		player.xvel = player.xvel - player.speed * dt
	end
	if love.keyboard.isDown('up') and playerCanJump then
		player.yvel = player.yvel + player.speed * dt
		playerCanJump = false
	end
end
--[[function player.jump(dt)
	if love.keyboard.isDown('up')
	end
end]]
function player.boundary()
	if player.x < 0 then
		player.x = 0
		player.xvel = 0
	end
	if player.x > 750 then
		player.x = 750
		player.xvel = 0
	end
	if player.y + player.height > groundlevel then
		player.y = groundlevel - player.height
		player.yvel = 0
		playerCanJump = true
	end
end
function UPDATE_PLAYER(dt)
	player.physics(dt)
	player.move(dt)
	player.boundary()
end
function DRAW_PLAYER()
	player.draw()
end

User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Platformer Jumping -- how does it work?

Post by micha » Fri May 09, 2014 8:45 pm

Jumping is different from moving left and right in that you only need to detect the very moment, the jump key is pressed. So instead of using love.keyboard.isDown, you need to use the love.keypressed callback. Add this function to your file:

Code: Select all

function love.keypressed( key )
if love.keyboard.isDown('up') and playerCanJump then
  player.yvel = player.jumpVel
  playerCanJump = false
end
end
And define the variable player.jumpVel in the beginning. It should be a negative number.

dt is not used here, because the actual jumping action does only happen between two frames. It is not spread over several frames.
Once the player-velocity is changed, the normal physics function takes care of the rest and the player moves as expected. That also means, that you don't need a thread/timer/co-routine.

Edit: I suggest you put the player.move before the player.physics function. Otherwise player input for left- and right-movement will always be delayed by one frame (not a big deal, though).

User avatar
Cryogenical
Prole
Posts: 49
Joined: Mon Apr 28, 2014 5:23 pm

Re: Platformer Jumping -- how does it work?

Post by Cryogenical » Sat May 10, 2014 6:41 am

I never thought about setting a jump velocity, is there a reason behind why it's not a good idea to use a formula containing something consisting of x-y velocities, gravity, and speed?

I'm trying to learn all I can, and if you could help that is a major plus.

It worked btw, thank you very much.

User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Platformer Jumping -- how does it work?

Post by micha » Sat May 10, 2014 7:04 am

I am glad I could help.
Cryogenical wrote:I never thought about setting a jump velocity, is there a reason behind why it's not a good idea to use a formula containing something consisting of x-y velocities, gravity, and speed?
I think I don't fully understand your question. It is totally okay to have a formula containing velocities, gravity and speed.

User avatar
Cryogenical
Prole
Posts: 49
Joined: Mon Apr 28, 2014 5:23 pm

Re: Platformer Jumping -- how does it work?

Post by Cryogenical » Sat May 10, 2014 1:52 pm

I meant to ask why did we need to introduce a new velocity (jumpVelocity)?

Is there not a formula that we could use containing the variables we declared before?

User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Platformer Jumping -- how does it work?

Post by T-Bone » Sat May 10, 2014 1:57 pm

Cryogenical wrote:I meant to ask why did we need to introduce a new velocity (jumpVelocity)?

Is there not a formula that we could use containing the variables we declared before?
Probably just a matter of convenience. Having a variable for the velocity makes it easy to change it (you want the velocity to be more and more downwards over time). If you didn't keep track of the velocity in the y direction, it would be difficult to keep track of where in the jump you are (remember, you want to be moving quickly upward the first few frames and then start moving downward), simply keeping track of the position isn't enough. But you could implement this in different ways; for example you could keep track of the time since you last jumped. But that will probably just make things more complicated.

User avatar
Cryogenical
Prole
Posts: 49
Joined: Mon Apr 28, 2014 5:23 pm

Re: Platformer Jumping -- how does it work?

Post by Cryogenical » Sat May 10, 2014 2:25 pm

Alright, I understand more or less now; thanks for both of your help.

User avatar
Cryogenical
Prole
Posts: 49
Joined: Mon Apr 28, 2014 5:23 pm

Re: Platformer Jumping -- how does it work?

Post by Cryogenical » Mon May 12, 2014 7:13 pm

Another question; is there a way to sort of "rappel" or "scale" up a wall?

I'm using a boundary function to check if the player's x position is less/greater than 0 then sets their position back enough to keep the entire character on the screen.

Code: Select all

function player.boundary()
	if player.x < 0 then
		player.x = 0
		player.xvel = 0
	end
	if player.x > 750 then
		player.x = 750
		player.xvel = 0
	end
	if player.y < 0 then
		player.y = 0
		player.yvel = 0
	end
	if player.y + player.height > groundlevel then
		player.y = groundlevel - player.height
		player.yvel = 0
		playerCanJump = true
	end
end
Using the jump function that micha gave, is there a way to do this?

I've attempted to do this using

Code: Select all

if love.keyboard.isDown('up') and not playerCanJump and player.x < 5 then
			playerCanJump = true
		end
in the function love.keypressed(key), but it doesn't register immediately and let the player jump.

User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Platformer Jumping -- how does it work?

Post by micha » Mon May 12, 2014 8:50 pm

Before I answer your question: I realized, I did a mistake in the previous answer. This is the code I gave you:

Code: Select all

function love.keypressed( key )
  if love.keyboard.isDown('up') and playerCanJump then
    player.yvel = player.jumpVel
    playerCanJump = false
  end
end
And this is how it should be:

Code: Select all

function love.keypressed( key )
  if key == 'up' and playerCanJump then
    player.yvel = player.jumpVel
    playerCanJump = false
  end
end
Using isDown inside the love.keypressed is not as it is intended to be (unless you want one button do different things depending on another button, for example jump -> jump, jump+down->stomp). Sorry for teaching you something wrong.

That said, let's look at your current problem. What exactly do you want to accomplish? Do you want to allow the player to run up the wall? Or do you want to implement a wall jump?
The wall-running would be implemented inside the love.update together with the love.keyboard.isDown, because this would happen continuously while the button is held down. It should look something like this:

Code: Select all

if player.x == 0 and love.keyboard.isDown('up') then
  player.yvel = wallRunSpeed
end
Put some value into the wallRunSpeed variable (a negative value).
If you want to implement a wall jump, then you are on the right track. But instead of setting the playerCanJump variable to true, you should just perform the jump. Add this in the love.keypressed:

Code: Select all

if player.x == 0 and key == 'up' then
  playerCanJump = false
  player.yvel = yJumpSpeed
  player.xvel = xJumpSpeed
end
For a wall jump you would both set a velocity in x- and y-direction, to move the player away from the wall.

User avatar
Cryogenical
Prole
Posts: 49
Joined: Mon Apr 28, 2014 5:23 pm

Re: Platformer Jumping -- how does it work?

Post by Cryogenical » Tue May 13, 2014 2:04 am

Thank you very much, micha. It works almost perfectly now, and is good enough.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 55 guests