Page 1 of 2

Jumping?

Posted: Mon Dec 30, 2013 7:14 pm
by thewifitree
I tried using several methods for jumping, but I can't get any of them to work. Any suggestions? :3

Re: Jumping?

Posted: Mon Dec 30, 2013 8:10 pm
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!

Re: Jumping?

Posted: Mon Dec 30, 2013 8:22 pm
by thewifitree
Thank you so much! :awesome:

Re: Jumping?

Posted: Thu Jan 02, 2014 4:47 pm
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

Re: Jumping?

Posted: Thu Jan 02, 2014 4:56 pm
by micha
To check of equality put an "==" not an "=".

Single "=" is for assigning values, double "==" is for checking equality.

Re: Jumping?

Posted: Thu Jan 02, 2014 4:58 pm
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 :?

Re: Jumping?

Posted: Thu Jan 02, 2014 5:06 pm
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.

Re: Jumping?

Posted: Thu Jan 02, 2014 5:28 pm
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

Re: Jumping?

Posted: Fri Jan 03, 2014 3:16 pm
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.

Re: Jumping?

Posted: Fri Jan 03, 2014 4:47 pm
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.