Page 1 of 1

I can't make a character fall down...

Posted: Sun Apr 15, 2012 10:40 pm
by NanoSpace
I searched for some Physics tutorials but no luck. All I want to do is if you release the "W" key then the character falls down until it hits the ground. I know you would need some type of ground..and collision detection..and some velocity(I think)..but sadly I don't know much about that. Does anyone know a simpler way? :cry:

Re: I can't make a character fall down...

Posted: Sun Apr 15, 2012 11:47 pm
by Puzzlem00n
Well, this is an odd question, but lucky for you, I'm an odd person.

Code: Select all

function love.load()
	player = {
		x = 380,
		y = 180,
		grav = 0 -- gravity amount
		}
end

function love.keyreleased(key)
	if key == "w" then
		player.grav = 90 -- we make it a value to go down
		print(release)
	end
end

function love.update(dt)
	player.y = player.y + player.grav * dt -- We add gravity to the player's y position
	if player.y > 400 then -- Are we on the ground? (which we'll call 400 y)
		player.y = player.y - player.grav * dt -- If we are, We undo that moving down
	end
end

function love.draw()
		love.graphics.rectangle("fill", player.x, player.y, 40, 40) -- Draw it!
end
That should do exactly what you want.