Handling jumping and falling

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
Solitaire
Prole
Posts: 12
Joined: Mon Jul 29, 2013 1:50 am

Handling jumping and falling

Post by Solitaire »

Hola,

I'm currently meeting a rather embracing problem with a "game". I can't handle properly jumping and falling. It seems like the character is shivering when he walks on the ground. Here's the code :

Code: Select all

--Global Variables
TILE = 64
HEIGHT = TILE * 7
WIDTH = TILE * 10
GRAVITE = 0

function love.keypressed(key)
	if key == " " then
		GRAVITE = 500
		giorgio.v_y = -200
	end
end

function love.load()
	--Creation of the level
	local f = io.open("level.lvl", "r")
	level = f:read("*all")
	f:close()

	--Window settings
	love.graphics.setMode(WIDTH, HEIGHT)
	love.graphics.setCaption("Italiano, si ?")
	
	--Importing images
	giorgioImg = love.graphics.newImage("Giorgio.png")
	terreImg = love.graphics.newImage("Terre.png")
	
	giorgio = {x = 0,
			y = HEIGHT-TILE-giorgioImg:getHeight()-1,
			v_x = 200,
			v_y = 0,
			w = giorgioImg:getWidth(),
			h = giorgioImg:getHeight()
			}
	decor = {}
end

function love.update(dt)

	if love.keyboard.isDown("right") then
		giorgio.x = giorgio.x + giorgio.v_x*dt
		
	elseif love.keyboard.isDown("left") then
		giorgio.x = giorgio.x - giorgio.v_x*dt
	end
	
	for i, v in ipairs(decor) do
		if (giorgio.x + giorgio.w < v.x or giorgio.x > v.x + TILE or giorgio.y + giorgio.h < v.y or giorgio.y > v.y+1 + TILE) then
			GRAVITE = 500
		else
			giorgio.y = giorgio.y - giorgio.v_y*dt
			GRAVITE = 0
			giorgio.v_y = 0
		end
	end
	
	giorgio.y = giorgio.y + giorgio.v_y*dt
	giorgio.v_y = giorgio.v_y + GRAVITE*dt
end

function love.draw()
	love.graphics.draw(giorgioImg, giorgio.x, giorgio.y)
	
	--Foreground elements
	i = 0; j = 0
	for c in level:gmatch(".") do
		if c == '1' then
			terre = {x = TILE*i, y =TILE*j}
			table.insert(decor, terre)
			love.graphics.draw(terreImg, TILE*i, TILE*j)
			i = i + 1
		elseif c == '0' then
			i = i + 1
		elseif c == '\n' then
			j = j + 1
			i = 0
		end
	end
end
The code is a pure mess, I conceed it. Le character trembles and sometimes sinks on the ground when I press the space bar. I have and idea about why he's trembling, but no idea about how stoping it.

Does anyone know how to handle jumping and falling properly ?
User avatar
bdjnk
Citizen
Posts: 81
Joined: Wed Jul 03, 2013 11:44 pm

Re: Handling jumping and falling

Post by bdjnk »

I'm willing and able to help, but I need working code to see the current behavior as well as the results of my tinkering. Consult recommendation three of the "Posting Rules" for this forum section.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Handling jumping and falling

Post by Robin »

We really would like to see a .love, otherwise we can only guess.

My guess is:

Code: Select all

   giorgio.y = giorgio.y + giorgio.v_y*dt
   giorgio.v_y = giorgio.v_y + GRAVITE*dt
should be before you check the collisions, not after.

If that didn't work, your best course of action is to upload the .love of your game, so we don't waste your time and you don't waste ours.
Help us help you: attach a .love.
Solitaire
Prole
Posts: 12
Joined: Mon Jul 29, 2013 1:50 am

Re: Handling jumping and falling

Post by Solitaire »

Sorry, I've been to prompt to post that I forgot to read the rules. Anyway, I managed to get myself out of this problem, so, thread closed.
Post Reply

Who is online

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