Be a Supporter!

Math behind a jump function?

  • 271 Views
  • 6 Replies
New Topic Respond to this Topic
Splyth
Splyth
  • Member since: Sep. 13, 2008
  • Offline.
Forum Stats
Member
Level 08
Programmer
Math behind a jump function? 2011-08-30 13:59:19 Reply

Hi guys, I was wondering about what mathmatical formula is used when making a person jump in a platformer. I'm asking this because I've seen plenty of code just kind of give it to you but I don't know the HOW behind it.

Thanks

Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to Math behind a jump function? 2011-08-30 14:38:07 Reply

gravity is pulling everything constantly down, when you touch the ground there is a opposite force working on you to make yourself stay at 1 place. Force makes change on the acceleration of a object:
F(res) = m * a
The forces on a object (F) = mass of the object (m) * the acceleration of the object(a)

so: a = F / m

the acceleration has an impact on the velocity of the object, according to this formula:

velocity (v) = acceleration (a) * time (t)

And the velocity in its turn has an impact on the position of the object:

s = v * t
position (s) = velocity (v) * time (t)

When we look at just the position and the time we get the following formula:

s = a * t^2
The acceleration is in a jumping object mostly a positive Constance (considering a positive value representing a lower position on the axis in flash). But when a object starts jumping the speed is already pointed upwards, over time the acceleration decreases the speed until its zero and then starts to make the velocity make the object fall down.

As you can see the determining variable is the time and its a quadratic function. These formulas will do when you have a constant acceleration, but they get more complicated if you start to do more complex stuff like the acceleration depending on the time.

4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to Math behind a jump function? 2011-08-30 17:02:56 Reply

At 8/30/11 02:38 PM, Sandremss128 wrote: gravity is pulling everything constantly down, when you touch the ground there is a opposite force working on you to make yourself stay at 1 place. Force makes change on the acceleration of a object:

That was a very well thought out explanation, and that's how gravity works in real life, but you don't *need* it to be this complicated, especially not for 2d games that most likely don't involve realistic physics.

Rather than applying real physics, there's a much simpler way to apply gravity to your platformer.

Have a gravity variable that's always increasing. Your player's way gets pushed by this variable. As simple as:

gravity++
player.y += gravity

Now whenever your character hits the ground, just set gravity to 0. And when you want your character to jump, set the gravity to a negative number, so it goes up, and since you have your gravity variable always incrementing, the character will slow down then accelerate back down.

SantoNinoDeCebu
SantoNinoDeCebu
  • Member since: Jul. 20, 2002
  • Offline.
Forum Stats
Supporter
Level 32
Programmer
Response to Math behind a jump function? 2011-08-30 17:38:57 Reply

At 8/30/11 05:02 PM, 4urentertainment wrote: Have a gravity variable that's always increasing. Your player's way gets pushed by this variable. As simple as:

gravity++
player.y += gravity

It is nice for your code to make some sense in the real world though! An incrementing gravity variable, that's just weird! (Unless its a game mechanic!)

Why not just name things for what they actually are?
In that little snippet you gave, 'gravity' is actually speed and the gravity is 1. (Time is 1 frame).

Basically the maths will go

speed += gravity;
position.y += speed;
Kibester
Kibester
  • Member since: Jul. 21, 2008
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to Math behind a jump function? 2011-08-30 17:40:37 Reply

At 8/30/11 05:02 PM, 4urentertainment wrote:
At 8/30/11 02:38 PM, Sandremss128 wrote: gravity is pulling everything constantly down, when you touch the ground there is a opposite force working on you to make yourself stay at 1 place. Force makes change on the acceleration of a object:
That was a very well thought out explanation, and that's how gravity works in real life, but you don't *need* it to be this complicated, especially not for 2d games that most likely don't involve realistic physics.

Rather than applying real physics, there's a much simpler way to apply gravity to your platformer.

Have a gravity variable that's always increasing. Your player's way gets pushed by this variable. As simple as:

gravity++
player.y += gravity

Now whenever your character hits the ground, just set gravity to 0. And when you want your character to jump, set the gravity to a negative number, so it goes up, and since you have your gravity variable always incrementing, the character will slow down then accelerate back down.

Adding onto this, you need an "If Statement" so gravity doesn't increase to infinity.

if(gravity>20){
gravity=20
}

With this should make it proper. ON a ground platform, you need to make player stop falling when he hits that with a "Hit Test"

if(_root.player(hitTest.this)){
gravity=0;
}

BBS Signature
Splyth
Splyth
  • Member since: Sep. 13, 2008
  • Offline.
Forum Stats
Member
Level 08
Programmer
Response to Math behind a jump function? 2011-08-30 20:01:53 Reply

Thank you all.

I now know a little more than I did before.

JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to Math behind a jump function? 2011-09-01 16:06:28 Reply

At 8/30/11 08:01 PM, Splyth wrote: Thank you all.

I now know a little more than I did before.

But not *much* more! I shall SWFDECOMPILE YOU