Need Explanation On 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.
VectorNormal
Prole
Posts: 8
Joined: Sun Oct 02, 2016 5:58 am

Re: Need Explanation On Jumping?

Post by VectorNormal »

I've made more than a handful of 2D platformer engines so I'll add my 2 cents.

The fundamentals of jumping in a 2D game are all quite simple when broken down into their core routines. While getting a good handle on physics and trig would be a good idea in general, I feel it's extreme overkill for a simple implementation.
Here's my take on it, at least:

In the most straightforward explanation, begin with a few variables to keep track of what the character is currently doing and where he is.
Also we should come up with some "physics" constants that generally make sense. All we're worried about here is upward acceleration and gravity.
This might look like:

Code: Select all

hero = {} -- Set up a table for our character. Just to keep things neat.
hero.x = 400
hero.y = 500
hero.vel = 0 -- Start with no velocity.

hero.can_jump = true -- This will be our boolean that we flip when he's on the ground
hero.jump_acc = -8 -- These values tend to
hero.gravity = .5 --    work for a basic test.

floor_y = 500 -- This is our bottom of the map.
Now we want to figure out what happens every step. There are many ways to do this and I'll cover just one.
In our example, we always add the hero.vel to hero.y
Normally hero.vel = 0 so this does nothing. But when hero.vel changes, the position of our hero starts to change:

Code: Select all

hero.y = hero.y + hero.vel
        
if not hero.can_jump then -- if hero.can_jump is false, then we must be jumping, so we begin to add gravity.
	hero.vel = hero.vel + hero.gravity
end
        
if hero.y > floor_y then -- if we've hit the floor...
	hero.vel = 0  -- Remove the velocity from the character.
	hero.y = floor_y  -- Make sure he's sitting squarely on the ground.
	hero.can_jump = true  -- And now he's allowed to jump again
end
When the player presses the jump button, we do a check and act accordingly. Here's where hero.vel can change, affecting the main loop and of course, causing our little guy to jump.

Code: Select all

if hero.can_jump then -- Only allow him to jump if hero.can_jump == true
	hero.vel = hero.jump_acc -- This makes hero.vel = -8
	hero.can_jump = false -- Tell our hero he can't jump another time
end
And of course, we'll draw our little dude so we can confirm that it's working

Code: Select all

function love.draw()
    love.graphics.rectangle("fill", hero.x, hero.y, 32,32)
end
I'll include the actual lua file so you can take a look at it and figure out how it works.
This is the bare bones basics of the jumping concept. After this point, you still have your work cut out for you.
I would look into these topics next as further reading:
2D collision detection:
AABB to AABB (Axis Aligned Bounding Box)
AABB to Circle
Circle to Circle (extremely easy)
2D Quadtrees (helpful for checking lots of collisions)

Edited for code corrections
Attachments
main.lua
A super Simple Jump Demo
(918 Bytes) Downloaded 67 times
Last edited by VectorNormal on Tue Oct 11, 2016 7:08 am, edited 3 times in total.
User avatar
pgimeno
Party member
Posts: 3593
Joined: Sun Oct 18, 2015 2:58 pm

Re: Need Explanation On Jumping?

Post by pgimeno »

That's a great explanation that will hopefully address the OP's concern. Just two nitpicks:

- There's a straw can_jump that should be hero.can_jump, both in your post and in the attached file (though the typo is in a different part in each)
- acc should be vel (it actually holds the vertical velocity of the character, not the acceleration).
VectorNormal
Prole
Posts: 8
Joined: Sun Oct 02, 2016 5:58 am

Re: Need Explanation On Jumping?

Post by VectorNormal »

Great eye, pgimeno. I've fixed the typo so it should work as intended. You're right about the velocity. While the impulse causing the change in velocity could be seen as momentary acceleration, the heroes value must be velocity. Edited for clarity, thanks!
(I've got vector in my name, you think I'd catch that :oops: )
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests