Page 1 of 1

How can i make a Player that Jump Up and Falls Down

Posted: Tue Apr 10, 2012 5:55 pm
by Pigzo
How can i make a Player that Jumps Up and Falls Down? I Haven't Any Idea :/ the Key for Jumping must be Space :s

Re: How can i make a Player that Jump Up and Falls Down

Posted: Tue Apr 10, 2012 6:57 pm
by tentus
This tutorial might get you started in the right direction, without handing you the answer on a silver platter.

Re: How can i make a Player that Jump Up and Falls Down

Posted: Tue Apr 10, 2012 7:16 pm
by veethree
I'll break it down for you.

First you need a player that has the following, An X position, A Y position and a Y velocity. I'd suggest setting up a table called player and putting the stuff into it like such:

Code: Select all

player = {
	x = 0,
	y = 0,
	yVel = 0
	}
Then you would need a gravity variable. I usually set it to something around 800 to 1200. Next step would be making the player be affected by the gravity. The way that would work is quite simple. In love.update you would increase the players yVel by the gravity value each frame, Then set the player y position to whatever it is at the time + the yVel.

Something like this:

Code: Select all

function love.update(dt)
	player.yVel = player.yVel + gravity * dt
	player.y = player.y + player.yVel * dt
end
Now with that the player would just fall off the screen, then keep falling. I'm assuming you have some form of a ground, So you would want to write a function that checks if the player is on the ground or not. I'm not gonna get into that because the function would differ depending on how your game is set up. Main thing is, If the player is on the ground the function returns true, otherwise it returns false.

So now you'd have to check if the function returns true before the whole gravity thing above. Something like this:

Code: Select all

function love.update(dt)
	if not onGround() then --if the onGround function returns false
		player.yVel = player.yVel + gravity * dt
		player.y = player.y + player.yVel * dt
	end
end
Now you would have to stop the player somehow when he does touch the ground, So you add an else statement, and in it you set the yVel to 0 which stops the player. something like this:

Code: Select all

function love.update(dt)
	if not onGround() then --if the onGround function returns false
		player.yVel = player.yVel + gravity * dt
		player.y = player.y + player.yVel * dt
	else
		player.yVel = 0
	end
end
Now you have the most basic form of physics. For the jumping all you do is set the yVel to some negative value, How high said value is depends on the strength of the gravity and how high you want the player to jump.
Something like this:

Code: Select all

function love.keypressed(key)
	if key == " " then --Checks if the space key was pressed
		player.yVel = -500
	end
end
This would still be incomplete because the player can jump while in mid air, That can be fixed with a simple boolean. Hope this helps.

Note that this code is completely untested. There may be errors in it.

Re: How can i make a Player that Jump Up and Falls Down

Posted: Tue Apr 10, 2012 7:20 pm
by Pigzo
Umm :huh: I Didn't Understand Anything :/

Re: How can i make a Player that Jump Up and Falls Down

Posted: Tue Apr 10, 2012 7:24 pm
by veethree
Pigzo wrote:Umm :huh: I Didn't Understand Anything :/
Then i'd suggest trying something simpler first ;) Check out some of the tutorials on the wiki.

Re: How can i make a Player that Jump Up and Falls Down

Posted: Tue Apr 10, 2012 7:34 pm
by Pigzo
Okay

Re: How can i make a Player that Jump Up and Falls Down

Posted: Mon Sep 22, 2014 1:21 am
by counterman2026
help!, it says that there is an error to perform on global 'gravity'(a nil value)

Re: How can i make a Player that Jump Up and Falls Down

Posted: Mon Sep 22, 2014 3:44 am
by AlexCalv
counterman2026 wrote:help!, it says that there is an error to perform on global 'gravity'(a nil value)
You need to define the variable gravity.

Re: How can i make a Player that Jump Up and Falls Down

Posted: Sat Sep 27, 2014 8:48 pm
by counterman2026
how do i do that?

would i do something like this?

gravity = {}

gravity = 10

Re: How can i make a Player that Jump Up and Falls Down

Posted: Sun Sep 28, 2014 5:42 am
by AlexCalv
The simplest way would be to just put it into love.load.

Code: Select all

function love.load()
     gravity = [[Whatever works for your game]]
end