Page 1 of 1

Handling jumping and falling

Posted: Sat Aug 31, 2013 3:54 pm
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 ?

Re: Handling jumping and falling

Posted: Sun Sep 01, 2013 5:15 am
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.

Re: Handling jumping and falling

Posted: Sun Sep 01, 2013 8:00 am
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.

Re: Handling jumping and falling

Posted: Sun Sep 01, 2013 10:42 pm
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.