Jumping help

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
Devnaut
Prole
Posts: 2
Joined: Mon May 20, 2013 10:59 am

Jumping help

Post by Devnaut »

So I've been following sockmunkee dev's tutorial on youtube on how to make a platformer http://www.youtube.com/watch?v=Ueya18MrtaA and it's a good tutorial, but he hasnt uploaded any more videos after the second in the platforming series, and i feel like watching his older tutorials wont work with the code that he shows in these platforming videos. I have figured out how to get gravity and jumping to work, but the jumping looks stiff and you can jump multiple times, and if you hit the "ceiling" of the game window, you can't come back down. Can anyone help me?
My player.lua file:

Code: Select all

player = {}

function player.load()
	player.x = 5
	player.y = 5
	player.xvel = 0
	player.yvel = 200
	player.friction = 7
	player.speed = 2250
	player.width = 50
	player.height = 50
	player.jumpHeight = 200
	player.startJump = 0
	player.jumping = false
	player.screenheight = 900
end

function player.draw()
	love.graphics.setColor(0,255,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
	if not player.jumping then
		player.yvel = player.yvel + gravity * dt
	else 
		if player.startJump - player.y >= player.jumpHeight then
			player.jumping = false
			player.yvel = 200
		end
	end
	player.xvel = player.xvel * (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 not player.jumping then
		player.yvel = player.yvel - player.speed * dt
		player.startJump = player.y
		player.jumping = true
		if player.y + player.height + player.width > player.screenheight then
			gravity = 600
			player.y = player.screenheight
		end
	end
end

function player.boundary()
	if player.x < 0 then
		player.x = 0
		player.xvel = 0
	end
	if player.y + player.height >= groundLevel then
		player.y = groundLevel - player.height
		player.yvel = 0
	end
end

function UPDATE_PLAYER(dt)
	player.move(dt)
	player.physics(dt)
	player.boundary()	
end

function DRAW_PLAYER()
	player.draw()
end
and my main.lua file:

Code: Select all

require "player"

function love.load()

	love.graphics.getBackgroundColor(0,0,0)
	
	groundLevel = 600
	
	gravity = 900
	
	--Loading classes
	player.load()
end
function love.update(dt)
	UPDATE_PLAYER(dt)
	if player.yvel ~= 0 then
		player.y = player.y + player.yvel * dt
		player.yvel = player.yvel - gravity * dt
		if player.y < 0 then
			player.yvel = 0
			player.y = 0
		end
	end
	
end
function love.draw()
	DRAW_PLAYER()
end
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Jumping help

Post by Helvecta »

Wassahhp, Devnaut, welcome to the forums! ^^

I've attached the patched code, here's some (but not all) of the changes I made:

Code: Select all

if player.y < 0 then
		player.yvel = 0
		player.y = 0
	end
This is the code that is getting your character stuck on the ceiling; when your y is < 0 then your velocity is forced to 0 and the y position is, too. Remember, the coordinates in LOVE go from 0,0 in the topleft corner, increasing in x as you move right, and increasing in y when you move down (when you start doing transformations to things the grid gets weird, but that's an entirely different topic).

Code: Select all

	groundLevel = 600
Ground level is 600, but so is the height of the window! I just changed the 600 to love.window.getHeight(), since it's the same thing.

Code: Select all

if player.y + player.height + player.width > player.screenheight then
		 gravity = 600
		 player.y = player.screenheight
	  end
As far as I know, this code doesn't have an effect on the hopping cube! I went ahead and swept that under the rug.

Code: Select all

player.isOnGround = false
I added this variable because you can still be jumping when your y velocity is higher than 0 (like at the top of your jumping arc). With this variable, you'll know if you're touching the ground, so you can know if you're ready to jump again.

Code: Select all

		if player.y == groundLevel - player.height then
			player.isOnGround = true
		end
This is the code I popped in to tell if you're on the ground. This is a dirty, quick fix but it works! (there's another line of code that makes player.isOnGround false automtically, if your y velocity isn't 0)


I removed player.jumpstart: gravity will determine where the top of your jump would be, so you don't need that variable to know anything as far as jumping is concerned.

Code: Select all

		if player.startJump - player.y >= player.jumpHeight then
			player.jumping = false
			player.yvel = 200
		end
I changed the above to:

Code: Select all

		if player.yvel < 0 then
			player.jumping = false
		end
(the player.yvel = 200 bit didn't seem to do anything useful either, so I popped it out).

I also changed player.jumpheight to player.jumpForce -- seems more appropriate! :ultraglee:



I hope I helped you learn a bit about the language and framework. Good luck! ;)
Attachments
main.lua
(402 Bytes) Downloaded 192 times
player.lua
(1.81 KiB) Downloaded 158 times
"Bump." -CMFIend420
Devnaut
Prole
Posts: 2
Joined: Mon May 20, 2013 10:59 am

Re: Jumping help

Post by Devnaut »

Thanks so much, you've really helped a lot :awesome:
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests