Physics - Platformer - Jumping
Physics - Platformer - Jumping
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.
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
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
It just needed a few tweaks.
I uncommented this as it's quite a good mass calculator. Gravity stays at 700. And movement changed as:
Uploaded a .love so people can try it directly
Code: Select all
objects.ball.body:setMassFromShapes()
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
- Attachments
-
- ball.love
- (990 Bytes) Downloaded 438 times
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Physics - Platformer - Jumping
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: 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.
No that's right.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
From my physics platformer: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?
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
Im not sure I follow what you mean here.pronoy wrote: 4 If you press the up key twice the jump starts from the bottom. How to avoid that?
Kurosuke needs beta testers
- ghoulsblade
- Party member
- Posts: 111
- Joined: Sun Oct 31, 2010 6:11 pm
Re: Physics - Platformer - Jumping
interesting but a bit advanced tutorial on jump&run physics with collision handing http://www.metanetsoftware.com/technique/tutorialA.html
love-android - gamejams
Re: Physics - Platformer - Jumping
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:It just needed a few tweaks.
Code: Select all
objects.ball.body:setMassFromShapes()
The ball re initiates the jump on every key press. Need to figure that out. But thanks a lot.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
Re: Physics - Platformer - Jumping
I don't quite understand this, but this might be needed later when I get to collisions.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.
̶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̶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.
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?
YES! I think I can make it worktentus wrote: From my physics platformer:You'll have to tweak it to make it work, but same idea.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
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.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.
Last edited by pronoy on Fri Nov 18, 2011 3:37 pm, edited 1 time in total.
Re: Physics - Platformer - Jumping
Thank you, I am thoroughly enjoying myself. I actually made things work using Love. Very nice framework.coffee wrote: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
From: http://love2d.org/wiki/Tutorial:Physics ... nCallbackspronoy 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.
Set the functions on collision with:
Code: Select all
world:setCallbacks(add, _, rem, _)
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
PS: If you use walls (object walls) instead of bounds, you should be able to wall-jump with this system
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Physics - Platformer - Jumping
Please refrain from double posting unless it's a notable update that otherwise may have been missed (which generally isn't the case).
Who is online
Users browsing this forum: Google [Bot] and 3 guests