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

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
Pigzo
Prole
Posts: 17
Joined: Mon Apr 09, 2012 8:49 pm

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

Post 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
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

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

Post by tentus »

This tutorial might get you started in the right direction, without handing you the answer on a silver platter.
Kurosuke needs beta testers
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

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

Post 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.
Pigzo
Prole
Posts: 17
Joined: Mon Apr 09, 2012 8:49 pm

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

Post by Pigzo »

Umm :huh: I Didn't Understand Anything :/
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

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

Post 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.
Pigzo
Prole
Posts: 17
Joined: Mon Apr 09, 2012 8:49 pm

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

Post by Pigzo »

Okay
counterman2026
Prole
Posts: 2
Joined: Mon Sep 22, 2014 1:15 am

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

Post by counterman2026 »

help!, it says that there is an error to perform on global 'gravity'(a nil value)
User avatar
AlexCalv
Prole
Posts: 49
Joined: Fri Aug 08, 2014 7:12 pm
Contact:

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

Post 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.
counterman2026
Prole
Posts: 2
Joined: Mon Sep 22, 2014 1:15 am

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

Post by counterman2026 »

how do i do that?

would i do something like this?

gravity = {}

gravity = 10
User avatar
AlexCalv
Prole
Posts: 49
Joined: Fri Aug 08, 2014 7:12 pm
Contact:

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

Post 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
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest