Page 1 of 1

Physics - Platformer - Jumping

Posted: Fri Nov 18, 2011 1:22 pm
by pronoy
Hello there loved ones,

I am new to love2d and lua and it's been 3 days I began and it's pretty much been great. I've had a little experience with the likes of Polycode, Cocos2dx and Ogre3d and tons of Python. But Love2D has been the best. It is so easy to understand and work with.

The end objective is to have a platformer with Physics and for that I need to first master jumping. The jumping I've come up with is a bit irritating. The object moves into the air and then it moves left or right.
Here's my code:
http://pastie.org/2883068

Please don't spare my feelings and let me know how to go about things the right way, any advice would be appreciated. Here's some of the explanation to why which things were added:

1. X and Y tracing because I thought if I could somehow code it as per if the y co ordinate isn't 580 means it's still in mid air. But this would cause problems later, considering the platforms the Y co ordinate as to change.
2. Dynamic because I thought that if the object is in Motion it would give Dynamic is True and if not then False, I think I got that wrong
3. The one thing I want to implement is to have it stop at the screen's edge, I havent' been able to figure out how to do that. How do I stop it from going beyond an X coordinate?
4 If you press the up key twice the jump starts from the bottom. How to avoid that?

If there's any prior discussion on this problem, please lead me to the link. I'd like to read up all I can. Thank you all.

Re: Physics - Platformer - Jumping

Posted: Fri Nov 18, 2011 2:07 pm
by coffee
Not an expert in physics code sorry but just wanted to welcome you to LOVE! Have fun around and good coding! :)

Re: Physics - Platformer - Jumping

Posted: Fri Nov 18, 2011 2:38 pm
by Ellohir
It just needed a few tweaks.

Code: Select all

	objects.ball.body:setMassFromShapes()
I uncommented this as it's quite a good mass calculator. Gravity stays at 700. And movement changed as:

Code: Select all

	if love.keyboard.isDown("left") then
		objects.ball.body:applyForce(-90,0)
	elseif love.keyboard.isDown("right") then
		objects.ball.body:applyForce(90,0)
	end

Code: Select all

	if key == "up" then
		objects.ball.body:applyImpulse(0,200)
	end

Uploaded a .love so people can try it directly ;)

Re: Physics - Platformer - Jumping

Posted: Fri Nov 18, 2011 2:44 pm
by tentus
pronoy wrote: 1. X and Y tracing because I thought if I could somehow code it as per if the y co ordinate isn't 580 means it's still in mid air. But this would cause problems later, considering the platforms the Y co ordinate as to change.
Keep track of how many jumps the player has left, and subtract 1 every time the player hits the jump key. When the player collides with other stuff you could do a test to see if he's above the other object. If so, you can reset his number of jumps.
pronoy wrote: 2. Dynamic because I thought that if the object is in Motion it would give Dynamic is True and if not then False, I think I got that wrong
No that's right.
pronoy wrote: 3. The one thing I want to implement is to have it stop at the screen's edge, I havent' been able to figure out how to do that. How do I stop it from going beyond an X coordinate?
From my physics platformer:

Code: Select all

	local xVel, yVel = self.body:getLinearVelocity()
	if self.body:getX() < 32 then
		self.body:setX(32)
		self.body:setLinearVelocity(0, yVel)
	end
	if self.body:getY() < 32 then
		self.body:setY(32)
		self.body:setLinearVelocity(xVel, 0)
	end
You'll have to tweak it to make it work, but same idea.
pronoy wrote: 4 If you press the up key twice the jump starts from the bottom. How to avoid that?
Im not sure I follow what you mean here.

Re: Physics - Platformer - Jumping

Posted: Fri Nov 18, 2011 2:54 pm
by ghoulsblade
interesting but a bit advanced tutorial on jump&run physics with collision handing http://www.metanetsoftware.com/technique/tutorialA.html

Re: Physics - Platformer - Jumping

Posted: Fri Nov 18, 2011 3:17 pm
by pronoy
Ellohir wrote:It just needed a few tweaks.

Code: Select all

	objects.ball.body:setMassFromShapes()
I tried that and the problem is now it develops some sort of inertia and moves out of the frame. It doesn't stop. Is there a way to introduce friction?
Ellohir wrote: I uncommented this as it's quite a good mass calculator. Gravity stays at 700. And movement changed as:

Code: Select all

	if love.keyboard.isDown("left") then
		objects.ball.body:applyForce(-90,0)
	elseif love.keyboard.isDown("right") then
		objects.ball.body:applyForce(90,0)
	end

Code: Select all

	if key == "up" then
		objects.ball.body:applyImpulse(0,200)
	end

Uploaded a .love so people can try it directly ;)
The ball re initiates the jump on every key press. Need to figure that out. But thanks a lot.

Re: Physics - Platformer - Jumping

Posted: Fri Nov 18, 2011 3:25 pm
by pronoy
tentus wrote: Keep track of how many jumps the player has left, and subtract 1 every time the player hits the jump key. When the player collides with other stuff you could do a test to see if he's above the other object. If so, you can reset his number of jumps.
I don't quite understand this, but this might be needed later when I get to collisions.
pronoy wrote: 2. Dynamic because I thought that if the object is in Motion it would give Dynamic is True and if not then False, I think I got that wrong
tentus wrote: No that's right.
̶O̶k̶a̶y̶ ̶s̶o̶ ̶I̶ ̶t̶h̶i̶n̶k̶ ̶I̶ ̶n̶e̶e̶d̶ ̶t̶o̶ ̶p̶u̶t̶ ̶t̶h̶e̶ ̶s̶t̶a̶t̶u̶s̶ ̶i̶n̶ ̶l̶o̶v̶e̶.̶u̶p̶d̶a̶t̶e̶ ̶i̶n̶ ̶t̶h̶a̶t̶ ̶c̶a̶s̶e̶,̶ ̶p̶r̶o̶b̶a̶b̶l̶y̶ ̶t̶h̶a̶t̶'̶s̶ ̶t̶h̶e̶ ̶r̶e̶a̶s̶o̶n̶ ̶i̶t̶ ̶a̶l̶w̶a̶y̶s̶ ̶s̶h̶o̶w̶s̶ ̶y̶e̶s̶ ̶e̶v̶e̶n̶ ̶w̶h̶e̶n̶ ̶i̶t̶'̶s̶ ̶s̶t̶o̶p̶p̶e̶d̶

The status is already being updated. It's in love.update(). So I think this is ambiguous. Let me rephrase that as questions:
1. If the body is not moving the isDynamic() should return False?
2. If the body is moving the isDynamic() should return True?
tentus wrote: From my physics platformer:

Code: Select all

	local xVel, yVel = self.body:getLinearVelocity()
	if self.body:getX() < 32 then
		self.body:setX(32)
		self.body:setLinearVelocity(0, yVel)
	end
	if self.body:getY() < 32 then
		self.body:setY(32)
		self.body:setLinearVelocity(xVel, 0)
	end
You'll have to tweak it to make it work, but same idea.
YES! I think I can make it work :)
pronoy wrote: 4 If you press the up key twice the jump starts from the bottom. How to avoid that?
tentus wrote: Im not sure I follow what you mean here.
If you press the up key when the ball is in mid air, it starts from the bottom and re initiates the jump. What I want is, if the Ball is in mid air then there should be no effect of the key press. That's why I wanted to use the isDynamic(). Thanks this has been helpful.

Re: Physics - Platformer - Jumping

Posted: Fri Nov 18, 2011 3:26 pm
by pronoy
coffee wrote:Not an expert in physics code sorry but just wanted to welcome you to LOVE! Have fun around and good coding! :)
Thank you, I am thoroughly enjoying myself. I actually made things work using Love. Very nice framework.

Re: Physics - Platformer - Jumping

Posted: Fri Nov 18, 2011 3:49 pm
by Ellohir
pronoy wrote:If you press the up key when the ball is in mid air, it starts from the bottom and re initiates the jump. What I want is, if the Ball is in mid air then there should be no effect of the key press. That's why I wanted to use the isDynamic(). Thanks this has been helpful.
From: http://love2d.org/wiki/Tutorial:Physics ... nCallbacks

Set the functions on collision with:

Code: Select all

world:setCallbacks(add, _, rem, _)
And define them as it follows:

Code: Select all

function add(a, b, coll)
    -- on collision
    objects.ball.in_air = false
end

function rem(a, b, coll)
    -- leaving contact
    objects.ball.in_air = true
end
And modify the jumping code. I haven't tried it but it should work ;)

PS: If you use walls (object walls) instead of bounds, you should be able to wall-jump with this system ;)

Re: Physics - Platformer - Jumping

Posted: Fri Nov 18, 2011 4:05 pm
by bartbes
Please refrain from double posting unless it's a notable update that otherwise may have been missed (which generally isn't the case).