Gravity for dummies

This topic is 3621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Need help ! (sorry my post is brief but im on a rush ) I just want to create a free falling body. Example : a bowling ball dropped from the top of the empire state building. acceleration is -9.8m/s2 at each frame, I calculate velocity : velocity += velocity - 9.8 * elapsedTime; to get the position of the ball what should I do ?? position = velocity ...? And if I drop an elephant instead of a bowling ball, it will fall at the same speed ? What is the relationship with mass ? thank you !!

Share on other sites
A bowling ball does fall at the same speed as an elephant when you disregard air friction. Mass doesn't matter when dealing with a free-falling object, as it cancels out in the equations.

You can get the position like this:
position += velocity * elapsedTime;

Also, I think that your velocity equation should just be:
velocity += -9.8 * elapsedTime;

Otherwise you double your velocity every time before subtracting the acceleration.

Share on other sites

You're right for the velocity equation, it's a typo in my original post.

I tested the equation and my object was falling at a constant speed. There must be something wrong here.

Any ideas ?

Share on other sites
The distance an object falls after t seconds is equal to -4.9t^2.

Share on other sites
how are you doing your rendering? what are these units in? you probably want to scale the velocity by a "unit" variable that represents 1 meter in your game

Share on other sites
Edit your code so that the velocity is constant, to make sure that your code is executing at a uniform rate. Then, if your object is falling at a constant speed, re-check your code for typos; those equations look right to me.

Share on other sites
Okay I reviewed my code and was taking the elapsed time between 2 frames in milliseconds. Oops.

Now it falls, but it's fast !! maybe too fast.

I experimented a little and found that after 0.4 seconds, the object falls for 100 units.

I call them units because I haven't converted them in meters in my games. Suppose that 10 units is 1 meter.

If I want to be realistic to earth gravity, the object should fall 9.8 meters (approx 100 units ) for 1 second. (correct me if i'm wrong)

I will need to scale the velocity as funkymunky said, by 0.4 to keep this realist. Are you doing it this way ? All my subsequent physics calculations will have to be "scaled" this way ? Rather annoying, and i hate physics too ...

:(

Share on other sites
Quote:
 Original post by JuksosahIf I want to be realistic to earth gravity, the object should fall 9.8 meters (approx 100 units ) for 1 second. (correct me if i'm wrong)

No.
After an object has been falling for 1 second, it should have a velocity of 9.8 meters/second, and it will have fallen 4.9 meters.

After an object has been falling for 2 seconds, it should have a velocity of 19.6 meters/second, and it will have fallen 19.2 meters.

After an object has been falling for 3 seconds, it should have a velocity of 29.4 meters/second, and it will have fallen 43.2 meters.

void Update( float elapsedTimeInSecondsSinceLastUpdate ){   float delta = elapsedTimeInSecondsSinceLastUpdate;      acceleration += -9.8 * delta;//assume these are all in meters   velocity += acceleration * delta;   position += velocity * delta;   float positionInMeters = position * 0.1f; //10 units in a meter}

Share on other sites
in conventional math, there is a position function
The psuedo-code takes variables of time, initial velocity, and initial position.

Position(time, init_velocity, init_position) = -4.9*(time)^2 + (init_velocity)*(time)+ (init_position)

as already stated, the mass makes no difference, only the air resistance, which for now I assume is negligible.

Share on other sites
Have a look on my website, I have a bunch of video tutorials there that show how to implement gravity for a cannon ball in projectile motion.

Share on other sites
This took me ages to get right, but eventually i decided to store the speed in units / second, and add 9.81 * (fraction of a second) to this speed on each update, then move the object by its speed * (fraction of a second).

Share on other sites
Thanks for your input guys ! Really appreciate it, especially this nice piece of code Hodgman... After 3.8 seconds the obejct fell for 94.63 units

Gravity is simple but for some reason I have problems translating all those formulas into functional code.

thanks again.

Share on other sites
You can simplify that code by setting velocity = -4.905*delta^2 + v_initial. Since you're dropping it from a rest state, v_initial = 0.

Share on other sites
I think your problem is converting between your 'units' and meters. Basically you have your acceleration in meters, therefore when you do this calculation (Vel += -9.8 * time) the velocity is also now in meters, so finally when you use this velocity to calculate your position, it to is in meters not 'units'. An easy fix to this would be to convert your gravity in meters variable (9.8) into the units you are using. if 10 of your units is 1 meter then just use 9.8/10 = .98 as your gravity. If I understand your problem correctly, then this should work.