Jumping?

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.
thewifitree
Prole
Posts: 32
Joined: Sat Dec 21, 2013 5:16 pm

Jumping?

Post by thewifitree »

I tried using several methods for jumping, but I can't get any of them to work. Any suggestions? :3
Attachments
pixel wizard.love
(2.54 KiB) Downloaded 186 times
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Jumping?

Post by Sheepolution »

I updated the code in your game.

What did I do?
The player needs to fall down by force. We call that velocity.

Code: Select all

velocity = 0
The velocity longer in air, the higher the velocity, so we need to add gravity to the velocity.

Code: Select all

velocity = velocity + grav * dt
When you jump, you go up instead of down. And while the gravity builds up slowly, jumping is instant speed.

Code: Select all

if jump == true then
		if love.keyboard.isDown("up") or love.keyboard.isDown("w") then
			player.velocity = -500
			jump = false
		end 
	end
And then we need to know if you're standing on the ground or not.

Code: Select all

if player.y + player.h < 325 then  -- We add player.h because that's his feet.
	player.velocity = player.velocity + player.gravity * dt --Add gravity to the velocity
else
	player.y = 325 - player.h
	jump = true
	velocity = 0 --Forgot to add this line in the file.
end
See, it's not that hard!
Attachments
pixel wizard.love
You can actually remove the onGround variable.
(3.23 KiB) Downloaded 192 times
thewifitree
Prole
Posts: 32
Joined: Sat Dec 21, 2013 5:16 pm

Re: Jumping?

Post by thewifitree »

Thank you so much! :awesome:
User avatar
zooks97
Prole
Posts: 11
Joined: Wed Jan 01, 2014 6:22 pm
Location: United States

Re: Jumping?

Post by zooks97 »

I put this in my game, and it worked great when I was using just one "ground". However, when I tried adding a ledge it gave me a syntax error "'then' expected near '=' in line 10." I dunno what's up. The map looks like this:

Code: Select all



(Player)
       O        ____    (ledge)
       Y
_______^______________________________   (ground)


Code: Select all

if 350 < x < 456 and						--ledge
	y + heightsum < 412 then				--above ledge
	onGround = false                                      --I know I don't need this but wtvr
	velocity = velocity + gravity * dt
elseif 350 < x < 456 and					--ledge
	412 < y + heightsum < 464 then			--in between ledge and ground
	onGround = false
	velocity = velocity + gravity * dt
elseif 350 < x <456 and						--ledge
	y + heightsum = 412 then				--on ledge
	Jump = true
elseif x < 350 or x > 456 and				--ground
	y < 464 then							--above ground
	onGround = false
	velocity = velocity + gravity * dt
elseif x < 350 or x > 456 and 				--ground
	y + heightsum = 464 then				--on ground
	Jump = true
end
Last edited by zooks97 on Thu Jan 02, 2014 4:56 pm, edited 1 time in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Jumping?

Post by micha »

To check of equality put an "==" not an "=".

Single "=" is for assigning values, double "==" is for checking equality.
User avatar
zooks97
Prole
Posts: 11
Joined: Wed Jan 01, 2014 6:22 pm
Location: United States

Re: Jumping?

Post by zooks97 »

OoOoh. Thanks! Maybe I should actually read the syntax rules before trying to code.

Edit: Now it's telling me that I'm trying to compare a boolean with a number on line 1 :?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Jumping?

Post by Robin »

Also, Lua has no support for 350 < x < 456 (I think that of all the popular languages only Python supports chaining comparison operators). Replace it with 350 < x and x < 456.
Help us help you: attach a .love.
User avatar
zooks97
Prole
Posts: 11
Joined: Wed Jan 01, 2014 6:22 pm
Location: United States

Re: Jumping?

Post by zooks97 »

Code: Select all

if 350 < x and x < 456 and						--ledge
	y + heightsum < 412 then				--above ledge
	velocity = velocity + gravity * dt
elseif 350 < x and x < 456 and					--ledge
	412 < y + heightsum and y + heightsum < 464 then			--in between ledge and ground
	velocity = velocity + gravity * dt
elseif 350 < x and x < 456 --and						--ledge --only commented it out to actually get something to work, kind of works
	--y + heightsum == 412 then				--on ledge
		then y = 412 - heightsum
		jump = true
elseif x < 350 or x > 456 and				--ground
	y < 464 then							--above ground
	velocity = velocity + gravity * dt
elseif x < 350 or x > 456 --and 				--ground  --only commented it out to actually get something to work, right half of map works like the ledge
	--y + heightsum == 464 then				--on ground
		then y = 464 - heightsum
		jump = true
end
Now the code looks like that. Jumping works on the ledge and to the right of the ledge, but the landing is extremely glitchy and there is no ground to the left.
Download: https://dl.dropboxusercontent.com/u/116 ... ySPv4.love
User avatar
CRxTRDude
Prole
Posts: 41
Joined: Sun Dec 15, 2013 3:03 am
Location: Some island in the archipelago

Re: Jumping?

Post by CRxTRDude »

Well, I seen your code, nice stuff you got there, but, it seems that your code is kind of messy, you might need to make separate luas for specific functions or code initialization saving your main.lua for important stuff. (That should not hamper you from making something though.

I would suggest that you learn to write variable comparisons (x > y) uniformly, that way it won't be a little hard for others to see what you do (especially if they want to help you and stuff). Try also to use constants. Since you're just repeating some stuff, might as well do such.

Anyhow, I might suggest you to use bounding boxes, they save you a lot of effort checking your collisions. Aside from that, you could also make some area vars.

See for example:

Code: Select all


      ====000=====           ====000000=====

					[P]
			x---------------------x
			|			   			|
y--------x---------------------x--------------y
As Doc Brown in BTTF would say it, "Please excuse the crudity of this model as I didn't have time to build it to scale or paint it."

[P] is your player with an bounding box.

the x points represent a ledge area (a rectangle. X are the points of the rectangle)
the y's are ground.

You make a constant for the ground and ledge groups (eventually, you'll learn more about making functions and tables, you can make something like that with just typing 'areaMake(x1,y1,x2,y2)' or something)

Then let the player check it using a collision check. BoundingBox.lua in the wiki can be your bet. (A good search can also help too.)

That's all what my brain cells can churn for now. Others can still help you though. Good luck on what you're making!

Don't worry, I envy your char art though and - like you - I'm learning the language as well.
~ CRxTRDude || Will be out of touch for a wee longer than expected. Will keep in touch with soon enough. Sorry bout that.
User avatar
zooks97
Prole
Posts: 11
Joined: Wed Jan 01, 2014 6:22 pm
Location: United States

Re: Jumping?

Post by zooks97 »

Thanks! That really helps a lot, I'll give that a go. Don't envy the character art, I stole it from an mmo I used to play, but I either won't actually release the game or will swap out textures by then if I want to. I just wanted a good looking baseline :P.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests