Gravity/Jumping

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
lobes
Prole
Posts: 2
Joined: Fri Jun 29, 2012 1:47 am
Location: Dirty South

Gravity/Jumping

Post by lobes »

What direction should I be looking at to achieve this?

Basically so my player/sprite will be walking on the "ground" (2d side scroller) and be able to jump and fall back down? I guess a simple question, I'm just not sure and havent seen any tutorials on this - most I have seen utilize movement all across the screen with no real "gravity"
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Gravity/Jumping

Post by Qcode »

Just think of it as you would in real life.
First we have to make you.

Code: Select all

function love.load()
   guy = {}
   guy.x = 0
   guy.y = 500
   guy.ySpeed = 0
   guy.xSpeed = 300
   guy.inAir = false
   guy.length = 50
end
function love.update(dt)
end
function love.draw()
   love.graphics.setColor(255, 255, 255)
   love.graphics.rectangle("fill", guy.x, guy.y, 50, 50)
end
Now you exist. You also have the ability to move don't you?

Code: Select all

function love.load()
   guy = {}
   guy.x = 0
   guy.y = 500
   guy.ySpeed = 0
   guy.xSpeed = 300
   guy.inAir = false
   guy.length = 20
end
function love.update(dt)
   if love.keyboard.isDown("left") then
      guy.x = guy.x - (guy.xSpeed * dt)
   end
   if love.keyboard.isDown("right") then
      guy.x = guy.x + (guy.xSpeed * dt)
   end
end
function love.draw()
   love.graphics.setColor(255, 255, 255)
   love.graphics.rectangle("fill", guy.x, guy.y, 50, 50)
end
Ok now you can move left and right. You can also jump, so we'll add that in.

Code: Select all

function love.load()
   guy = {}
   guy.x = 0
   guy.y = 500
   guy.ySpeed = 0
   guy.xSpeed = 300
   guy.inAir = false
   guy.length = 20
end
function love.update(dt)
   if love.keyboard.isDown("left") then
      guy.x = guy.x - (guy.xSpeed * dt)
   end
   if love.keyboard.isDown("right") then
      guy.x = guy.x + (guy.xSpeed * dt)
   end
   guy.y = guy.y + (guy.ySpeed * dt)
end
function love.draw()
   love.graphics.setColor(255, 255, 255)
   love.graphics.rectangle("fill", guy.x, guy.y, 50, 50)
end
function love.keypressed(key)
   if key == "up" then
      guy.ySpeed = guy.ySpeed - 200
   end
end
So now you can jump, but when you jump you can jump infinitely and you can repeatedly jump. So lets fix that.

Code: Select all

function love.load()
   guy = {}
   guy.x = 0
   guy.y = 500
   guy.ySpeed = 0
   guy.xSpeed = 300
   guy.inAir = false
   guy.length = 20
end
function love.update(dt)
   if love.keyboard.isDown("left") then
      guy.x = guy.x - (guy.xSpeed * dt)
   end
   if love.keyboard.isDown("right") then
      guy.x = guy.x + (guy.xSpeed * dt)
   end
   guy.y = guy.y + (guy.ySpeed * dt)
   if guy.inAir then
      guy.ySpeed = guy.ySpeed + (100 * dt)
   end
end
function love.draw()
   love.graphics.setColor(255, 255, 255)
   love.graphics.rectangle("fill", guy.x, guy.y, 50, 50)
end
function love.keypressed(key)
   if key == "up" and not guy.inAir then
      guy.ySpeed = guy.ySpeed - 200
      guy.inAir = true
   end
end
So now you can jump and you fall. But you also fall through the floor. So lets add it so that doesn't happen.

Code: Select all

function love.load()
   guy = {}
   guy.x = 0
   guy.y = 500
   guy.ySpeed = 0
   guy.xSpeed = 300
   guy.inAir = false
   guy.length = 20
end
function love.update(dt)
   if love.keyboard.isDown("left") then
      guy.x = guy.x - (guy.xSpeed * dt)
   end
   if love.keyboard.isDown("right") then
      guy.x = guy.x + (guy.xSpeed * dt)
   end
   guy.y = guy.y + (guy.ySpeed * dt)
   if guy.inAir then
      guy.ySpeed = guy.ySpeed + (100 * dt)
   end
   if guy.y + guy.length > 500 then
      guy.y = 500 - guy.length
      guy.inAir = false
   end
end
function love.draw()
   love.graphics.setColor(255, 255, 255)
   love.graphics.rectangle("fill", guy.x, guy.y, 50, 50)
   love.graphics.setColor(0, 255, 0)
   love.graphics.rectangle("fill", 0, 500, 800, 300)
end
function love.keypressed(key)
   if key == "up" and not guy.inAir then
      guy.ySpeed = guy.ySpeed - 200
      guy.inAir = true
   end
end
And that should be everything. Now if you copy that last script in you should have a little white guy jumping on a green ground.
Thats assuming you only have a flat ground, if you want holes and need more help with horizontal collision just post here again :awesome: .
Hope that helps.
Qcode
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Gravity/Jumping

Post by coffee »

Qcode wrote: And that should be everything. Now if you copy that last script in you should have a little white guy jumping on a green ground.
Thats assuming you only have a flat ground, if you want holes and need more help with horizontal collision just post here again :awesome: .
Hope that helps.
Qcode
Nice tutorial but you may have some unnoticed quirks since you using different sizes for height. the guy.length is 20 but the draw guy size is 50. So the guy is not jumping on a flat green ground, but stuck and jumping in the middle of green mud (because you drawing also terrain after/over player). :)

So a next evolution in code could be add width/height to guy (and replace lenght) and auto-relate it (without starting fixed position numbers) with the ground (that also is now auto-draw related to screen):

Code: Select all

function love.load()
   ground = {}
   ground.width = love.graphics.getWidth()
   ground.height = 100
   ground.x = 0
   ground.y = love.graphics.getHeight() - ground.height

   guy = {}
   guy.width = 50
   guy.height = 50
   guy.ySpeed = 0
   guy.xSpeed = 300
   guy.inAir = false
   guy.x = love.graphics.getWidth()/2 - guy.width/2 
   guy.y = ground.y - guy.height      
end
function love.update(dt)
   if love.keyboard.isDown("left") then
      guy.x = guy.x - (guy.xSpeed * dt)
   end
   if love.keyboard.isDown("right") then
      guy.x = guy.x + (guy.xSpeed * dt)
   end
   guy.y = guy.y + (guy.ySpeed * dt)
   if guy.inAir then
      guy.ySpeed = guy.ySpeed + (100 * dt)
   end
   if guy.y + guy.height > 500 then
      guy.y = 500 - guy.height
      guy.inAir = false
   end
end
function love.draw()
   love.graphics.setColor(0, 255, 0)
   love.graphics.rectangle("fill", ground.x, ground.y, ground.width, ground.height)
   love.graphics.setColor(255, 255, 255)
   love.graphics.rectangle("fill", guy.x , guy.y, guy.width, guy.height)
end
function love.keypressed(key)
   if key == "up" and not guy.inAir then
      guy.ySpeed = guy.ySpeed - 200
      guy.inAir = true
   end
end
And improvements and tutorial could go on. Let's write a platform game together Qcode? ;D
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Gravity/Jumping

Post by Qcode »

My last attempt at a platform game failed miserably :P .Whoops. I wrote all of it in the forum then just copied it in to test. I fixed that mistake, though evidently forgot to move it over. We could improve it some more, and add some acceleration on the xSpeed though I think all he wanted to know was how to jump.
Post Reply

Who is online

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