Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Jumping Formulas  (Read 2838 times)

0 Members and 1 Guest are viewing this topic.

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Jumping Formulas
« on: May 16, 2012, 06:23:25 pm »
If you are developing a platformer game, you typically need some gravity on your character and the start velocity on jumping. I think people often test some values out until they have a jump (duration and height) they like. But I've worked on a more efficient way:
You decide the jump duration and maximum height the character can reach and calculate the needed gravity and initial velocity.
I've worked on some formulas you can easily use. Tell me whether you find it useful or not, or if there are any mistakes or confusion.

And I hope this toptic fits here.


EDIT:
Here are the formulas without the need of any download.

For a jump'n run alias platformer you might want the protagonist jumping a very certain height and time, but you don't know which gravity and jump velocity to set. Here are some useful formulas with that you can calculate the needed parameters for your wished jump.

T0 is the duration from jump start till maximum height, where v (vertical velocity) is 0 and changes sign. Example: 10
T0 = k / g

hmax is the max height the player jumps. Example: 55
hmax = (k + k * T0) / 2

k is the jump velocity (should be > 0). That is the initial velocity on jump. Example: 10 in the attachment it's -10. that's wrong!
k = (2 * h) / (1 + T0)

g is the gravity (should be > 0). That is the value, that is subtracted from your current velocity. Example: 0.5
g = k / T0

[attachment deleted by admin]
« Last Edit: May 16, 2012, 08:03:41 pm by Shy Guy »
Please note that my previous display name was "Shy Guy".

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Jumping Formulas
« Reply #1 on: May 16, 2012, 07:32:37 pm »
Most important, you should declare what the variables mean. And if I were you, I'd only provide the linearly independent equalities, i.e. not both T0=k/g and g=k/T0. People should be able to rearrange the terms. Once, you write k = 2h / (1 + T0), the units in the denominator are inconsinstent. Generally, I think your formulas are incorrect...


Let's find the formulas. First, we define some variables:
h(t) = height of the object at time t
T = total jump time
H = maximal height

When we assume h(t) to be a parabola, it has a quadradic equation:
h(t) = a*t2 + b*t + c

Some relations at begin, middle and end of the jump are known:
h(0) = 0
h(T/2) = H
h(T) = 0

By inserting them in the equation, we can compute a, b and c:
a = -4H/T2
b = 4H/T
c = 0

Therefore, we have:
h(t) = -4H/T2 * t2 + 4H/T * t

A constantly accelerated object with acceleration g and initial velocity v0 travels the following distance during time t:
s(t) = g/2 * t2 + v0 * t

Thus, comparing the coefficients of h(t) and s(t) yields:
g = -8H/T2
v0 = 4H/T

Note however that I'm talking about the continuous case. In a game, you have to consider a certain time step, to which your gravity and velocity need to be adapted.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Re: Jumping Formulas
« Reply #2 on: May 16, 2012, 08:00:25 pm »
Most important, you should declare what the variables mean. And if I were you, I'd only provide the linearly independent equalities, i.e. not both T0=k/g and g=k/T0. People should be able to rearrange the terms. Once, you write k = 2h / (1 + T0), the units in the denominator are inconsinstent. Generally, I think your formulas are incorrect...


Let's find the formulas. First, we define some variables:
h(t) = height of the object at time t
T = total jump time
H = maximal height

When we assume h(t) to be a parabola, it has a quadradic equation:
h(t) = a*t2 + b*t + c

Some relations at begin, middle and end of the jump are known:
h(0) = 0
h(T/2) = H
h(T) = 0

By inserting them in the equation, we can compute a, b and c:
a = -4H/T2
b = 4H/T
c = 0

Therefore, we have:
h(t) = -4H/T2 * t2 + 4H/T * t

A constantly accelerated object with acceleration g and initial velocity v0 travels the following distance during time t:
s(t) = g/2 * t2 + v0 * t

Thus, comparing the coefficients of h(t) and s(t) yields:
g = -8H/T2
v0 = 4H/T

Note however that I'm talking about the continuous case. In a game, you have to consider a certain time step, to which your gravity and velocity need to be adapted.

I've checked my formulas and they fully worked.
But it might depend on how people interpret the variables, about like you said I think.

For me: T0 (it's about like your T/2) is the time the object is at the highest point.
k (your v0, yeah it's a better name for it) is always positive and means the velocity to the upwards.
g is always positive as well but means the acceleration downwards.

If I for example want the object reaching a height of 6 pixels (H=6) in 3 frames (T0=3), I get v0=3 and g=1, and what's calculated then is:
3 + 2 + 1
Result is 6 (height) and there are 3 summands (T0=3).


You are taking the total time being in air, which might be easier to use. I think our both formulas are correct, but we are interpreting the variables differently. Is it like that?
Please note that my previous display name was "Shy Guy".

 

anything