Calculating Gravity for Jumping in a Platform Game

  • Thread starter menzow
  • Start date
  • Tags
    Gravity
In summary, the conversation discusses a problem the speaker is having in creating a game where the player must land on specific platforms. They have been using a sine curve to calculate the height of the jump, but have been struggling to incorporate gravity into the equation. The other person in the conversation suggests using a parabola instead and provides a simple processing script to try out. The speaker also mentions their lack of knowledge in physics and asks for confirmation on their calculations for determining the initial speed needed for the jump. They are then given an equation for the trajectory of the jump and are advised to use it to calculate the height of the platforms at different distances.
  • #1
menzow
6
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
  • #2
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
 
  • #3
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:
  • #4
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.
 
  • #5
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
 
  • #6
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?
 
  • #7
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.
 
  • #8
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:
  • #9
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.
 
  • #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.
 

What is gravity?

Gravity is a natural force that pulls objects with mass towards each other. It is responsible for keeping planets in orbit around the sun and objects on Earth from floating off into space.

How is gravity measured?

Gravity is measured in units of acceleration, such as meters per second squared (m/s²) or feet per second squared (ft/s²). Scientists use instruments like gravimeters to measure the strength of gravity at different locations on Earth.

What factors affect the amount of gravity?

The amount of gravity depends on the masses of the objects involved and the distance between them. The greater the mass of an object, the stronger its gravitational pull. The further apart two objects are, the weaker the gravitational force between them.

How do we find the amount of gravity on Earth?

The amount of gravity on Earth is approximately 9.8 m/s² or 32 ft/s². This can be calculated using Newton's law of universal gravitation, which states that the force of gravity between two objects is directly proportional to the product of their masses and inversely proportional to the square of the distance between them.

Can gravity be canceled out?

No, gravity cannot be canceled out completely. However, the effects of gravity can be counteracted by other forces, such as the upward force of air resistance or the lift force of an airplane wing. In space, astronauts experience microgravity because they are constantly falling towards Earth but also moving sideways at a fast enough speed to stay in orbit.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
2K
  • Introductory Physics Homework Help
Replies
7
Views
2K
  • Introductory Physics Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
968
  • Introductory Physics Homework Help
Replies
5
Views
993
Replies
10
Views
1K
  • Introductory Physics Homework Help
Replies
13
Views
3K
  • Mechanics
Replies
12
Views
1K
  • Introductory Physics Homework Help
Replies
4
Views
1K
Back
Top