Calculating Gravity for Jumping in a Platform Game

  • Thread starter Thread starter menzow
  • Start date Start date
  • Tags Tags
    Gravity
AI Thread Summary
The discussion revolves around calculating the jump arc for a platform game where a player must land on platforms spaced 50 cm apart, with a jump covering 2 meters. The original approach using a sine curve is challenged, as true projectile motion under gravity follows a parabolic trajectory. Participants suggest adapting the game design to accommodate a parabolic arc, emphasizing the importance of adjusting either the launch angle or gravity to achieve the desired jump distance while maintaining a constant horizontal speed of 4 m/s. The player’s jump mechanics are designed to be simple, relying on a single tap to jump, which necessitates fixed landing points on the platforms. Ultimately, understanding the physics of projectile motion is crucial for implementing effective game mechanics.
menzow
Messages
6
Reaction score
0
Hello,

I'm new to this forum, and I signed up because I have been struggling with this problem for a couple of days now.
I am making a game where the player has to land on specific platforms.

The player runs at 4 meters / second.
When the player jumps there are 4 different platforms to land on.
All platforms are 50 centimeters apart, each.
1 jump covers a distance of 2 meters. ( Hence the 4 platforms to land on each jump. )


gKWNE.png


Homework Equations



I currently have the following equation to solve the problem:

abs(sin(0.5 * PI * x))
I enter the distance at x. And it returns the correct height.

In theory, this works, but in game I have to keep applying gravity etc.
How could I possible make the player jump this curve when applying gravity?

I have 0 knowledge of physics or whatsoever.
Never had any good math lessons at school so I suck at this topic.

I hope I was clear enough.
 
Physics news on Phys.org
so are you assuming the jumping arc is a sin curve? If so that's an interesting solution to get an arc but in reality a jumping curve in a constant gravity setting with no air resistance (like the Earth's surface) is a parabola.

Perhaps that is the source of your problem. The wki article below describes these kinds of curves.

http://en.wikipedia.org/wiki/Ballistic_trajectory
 
Like I said, I have 0 knowledge of physics.
Thats why I took a Sin curve. Because it is always accurate to what I need.

I need help converting it back to gravity. But I have no Idea how much upforce I have t apply and how much downforce I have to apply.
That is why I posted my question here.

Thanks for the link; reading the page as we speak. :)
 
Last edited:
Well, the simple answer is you can't make the player follow this curve by following gravity since, as jedishrfu said, projectile motion follows parabolas not sine curves. If you're willing to adapt the game design so that the platforms are along the correct type of curve, we can help you—but if you're sticking with a sine curve, we can't help you with "converting it back to gravity" because that's not what gravity does.
 
Your sin curve and parabola share a lot of common features so that switching shouldn't be a problem.

The way most game physics works is that they numerically integrate a simple formula to get the parabola. Starting at a given position with a given initial velocity

vx = vx0 * t
vy = vy0 - g * t
x = x0 + vx*t
y= y0 + vy * t
 
LastOneStanding said:
Well, the simple answer is you can't make the player follow this curve by following gravity since, as jedishrfu said, projectile motion follows parabolas not sine curves. If you're willing to adapt the game design so that the platforms are along the correct type of curve, we can help you—but if you're sticking with a sine curve, we can't help you with "converting it back to gravity" because that's not what gravity does.

I could change the curve, no problem.

Tho the platforms have to stay 50 cm away from each other, in height it doesn't matter.

Thanks for all the help I'm getting. Reading through the Wiki, but I barely know how to read those formulas..

Mind confirming if I have done this right?
eb3b67e6645cdcf8b61cd1385387df54.png


Velocity = 20.
Angle = 45.
Gravity = 9.81.

d = ((20^2) * sin(2 * 45)) / 9.81.

If I solve it like that, am I doing this correct?
 
jedishrfu said:
Your sin curve and parabola share a lot of common features so that switching shouldn't be a problem.

The way most game physics works is that they numerically integrate a simple formula to get the parabola. Starting at a given position with a given initial velocity

vx = vx0 * t
vy = vy0 - g * t
x = x0 + vx*t
y= y0 + vy * t

I'm sorry but I do not know how to read that.
Are there any books on how you solve equations like that?

Like I said in the first post, I have 0 knowledge on how to read formulas.
 
Here's a simple processing script (processing.org) to try out:

Please note that 0,0 is the top left corner and that I used a reverse sense to the vy0 and the g (gravity) values. The float values can be considered to be in meters:

Code:
float x,y,vx,vy;
float x0,y0,vx0,vy0;
float g,t,dt,r;

void setup() {
  size(640,480);  // setting screen size

  x0=0.0;
  y0=200.0;

  vx0=50.0; // 50 meters / sec
  vy0=-50.0; // 50 meters/sec

  g=9.8;  // g in meters/sec/sec

  dt=0.1;

  r=2.0;
}

void draw() {
  t = t+dt;

  vx=vx0;
  vy=vy0 + g * t;

  x = x0 + vx * t;
  y = y0 + vy * t;

  ellipse(x,y,r,r);
}
 
Last edited:
menzow said:
I could change the curve, no problem.

Tho the platforms have to stay 50 cm away from each other, in height it doesn't matter.

Thanks for all the help I'm getting. Reading through the Wiki, but I barely know how to read those formulas..

Mind confirming if I have done this right?
eb3b67e6645cdcf8b61cd1385387df54.png


Velocity = 20.
Angle = 45.
Gravity = 9.81.

d = ((20^2) * sin(2 * 45)) / 9.81.

If I solve it like that, am I doing this correct?
(Don't leave out your units like m or m/s when substituting things into equations, they're important!)

That formula gives you the maximum range of a projectile launched with initial speed v at angle 45 due to gravitational acceleration g. Since you know the maximum range you desire (2m), you've picked 45 degrees as the launch angle, and you seem to be sticking with the acceleration of real gravity that means you have to solve for initial launch speed, ##v_0## (which you wrote as ##v##, but ##v_0## is better since it's just the initial speed).

Now having determined all your projectile parameters, what you want is the equation for the trajectory given here:
##y = \tan(\theta)\cdot x - \frac{g}{2v_0^2\cos^2(\theta)} \cdot x^2##
where y is the height as a function of distance x, ##g=9.8m/s^2##, θ is whatever you chose for the launch angle (45 degrees in your example), and ##v_0## is what you calculated above. By calculating this at x=0.5m, 1m, 1.5m, and 2m, this will tell you how high to put your platforms.

Now I'm assuming your game has a time parameter and so you will want the avatar's position to be a function of time. For this, you want what are called the parametric equations for projectile motion (given right above the trajectory equation in that same link), and they are:
##x(t) = v_0\cos(\theta)t##
##y(t) = v_0\sin(\theta)t - \frac{1}{2}gt^2##
(or you can just calculate x(t) and then plug that into the equation above for y as a function of x).

And that's it! If run into trouble doing this, just ask—but do be specific in your questions so we can help.
 
  • #10
jedishrfu said:
Here's a simple processing script (processing.org) to try out
"Give a man a fish..." jedishrfu. This isn't helpful in the long run (and is against forum rules).
 
  • #11
All this said, it's worth pointing out that someone following this trajectory would, depending on your exact horizontal placement of the platforms, always: (i) miss them all and just land two metres ahead, (ii) hit their head on the first platform, or (iii) land on the second or third platforms. The same thing will always happen, as they would have no control over where they land if they always jump along this fixed trajectory. Projectile motion is deterministic.

This is why avatars generally don't follow projectile motion in games. If you think of a game like Super Mario, you are able to control the character while they're in the air. This let's you decide where you want to land. In true projectile motion, you don't have a choice once your feet have left the ground.
 
  • #12
LastOneStanding said:
All this said, it's worth pointing out that someone following this trajectory would, depending on your exact horizontal placement of the platforms, always: (i) miss them all and just land two metres ahead, (ii) hit their head on the first platform, or (iii) land on the second or third platforms. The same thing will always happen, as they would have no control over where they land if they always jump along this fixed trajectory. Projectile motion is deterministic.

This is why avatars generally don't follow projectile motion in games. If you think of a game like Super Mario, you are able to control the character while they're in the air. This let's you decide where you want to land. In true projectile motion, you don't have a choice once your feet have left the ground.

Thanks for the description above! I will try to put it in my game.

And yes, I know that the character will have no control, but that's the whole point.
The game I'm making is a one button game, the player can only tap the screen of the phone to make his player jump.
Thats why the jump has to be fixed.

Also the collision only works from the top side of a platform, so this way the player can't bumb his head (Think about mario), but can jump again from the platform he just went through.
 
  • #13
I see, so the character will stop on each of the platforms along the way? That changes your scenario quite a bit. If the avatar jumps and lands on the first platform and then makes the exact same jump again (i.e. same initial velocity) it will not continue along the same trajectory as before. It follow the same shape trajectory but from the new starting point.
 
  • #14
LastOneStanding said:
I see, so the character will stop on each of the platforms along the way? That changes your scenario quite a bit. If the avatar jumps and lands on the first platform and then makes the exact same jump again (i.e. same initial velocity) it will not continue along the same trajectory as before. It follow the same shape trajectory but from the new starting point.


Here is how the game works:
AuWbp.png


The character has to jump on these platforms.
He can only tap the phone screen, and the character jumps.
Each platform will play a beat.

Because we want to be able to move the platforms closer to each other, we have to also increase their position on the Y axis.
This way, we are able to catch the player mid air, and the player is able to jump again from that platform.

He is constantly moving at a speed of 4 m/s so you need a pretty high reaction speed to play the game.
But that is what the game is about.

Hope that explains the situation.
 
  • #15
I see, that makes sense—so in your original drawing, those were just possible locations of the platforms.

Note, though, if you are requiring a constant horizontal speed of 4m/s then you no longer have the freedom to choose the horizontal velocity in the above equations. The only parameter you would be free to adjust is the strength of gravity (g in those equations) to get the 2m range you want.
 
  • #16
Cant you instead adjust the vertical velocity?
 
  • #17
As long as he's fine with giving up the 45 degree jumping angle, which their might be aesthetic reasons for. But yes, either the angle or the gravitational acceleration will need to be adjusted if the horizontal speed is to remain fixed.
 
Back
Top