# Gravity Formulas?

Discussion in 'Mac Programming' started by Awesomeness, Dec 20, 2009.

1. ### Awesomeness macrumors member

Joined:
Feb 12, 2009
#1
Say I have an arbitrary number of bodies with x and y coordinates, each with a unique mass. How would you calculate the change in velocity for each body? How do you calculate inertia?

Mainly I just need the formulas, but if anyone is kind enough and has the time to do it, a java example would be awesome.

2. ### HiRez macrumors 603

Joined:
Jan 6, 2004
Location:
Western US
#2
I'm certainly not an expert on physics, but IIRC the mass of an object doesn't factor into how gravity affects them at all, UNLESS you're also accounting for air friction/drag. Acceleration due to gravity is constant, regardless of the object's mass (we are talking Earth gravity here, right?). I think Galileo's famous experiment of dropping 2 balls of different masses proved that.

Earth gravity accelerates objects at about 9.8 meters per second squared, so you can use that to calculate downward force (towards the earth's core). d = 1/2at^2. Or, distance traveled = 0.5 * 9.80 * time squared. So for example, after 3 seconds of freefall, d = 0.5 * 9.8 * (3.0)^2 = 44.1 meters added to the y component (the x component would be independent).

Like I said, I'm not an expert on this, so please check first, but I think that's right for simple freefalling bodies in gravity, unless you're doing something more complicated (rolling friction, air resistance) or need more accuracy (the acceleration is slightly different depending on elevation, objects accelerate faster at the North Pole than at the equator, for example).

3. ### mkrishnan Moderator emeritus

Joined:
Jan 9, 2004
Location:
Grand Rapids, MI, USA
#3
Mass in essence is a definition of an object's magnitude of gravitational interaction...

However, that assumption is fair in the scenario where numerous "small" objects are interacting with a large object at a nearly fixed distance (e.g., people walking on or near the Earth's surface -- where you use the "g" constant of 9.8 m/s^2 as an approximation of the actual gravitational force). It is, of course, incorrect for the scenario when the objects are of loosely comparable size.

For the original question, though, this is going to depend essentially on how detailed you want to get. For games in which a physics engine is not important, you have your objects accelerate down the screen at a fixed rate, specifying some terminal velocity for them if you have an atmosphere. But for games where the physics engine is a really crucial part of the enterprise, you're not going to get someone to post you a java example... this is going to be a lot of work, and you have to decide if you really want to make a physics engine (and understand how to make approximations well enough to do it), or if you would be better served by just using an open source physics engine, like this one.

4. ### TomM macrumors newbie

Joined:
Dec 6, 2007
#4
Gravity Formulas

Two days ago I found a link to a very good web site on physics from Georgia State University. They have formulas and good discussions on gravity and other topics.

The site is: http://hyperphysics.phy-astr.gsu.edu/hbase/hframe.html

TomM

5. ### MrFusion macrumors 6502a

Joined:
Jun 8, 2005
Location:
West-Europe
#5
This brings back a flashback. I once had to calculate the trajectory of an asteroid through the solar system for an astrophysics course. I still have the (crappy) java code. If you are interested, PM me. Unfortunately, the accompanying report is not in English.

There are no analytical solutions for more than 2 bodies. For three bodies, you can approximate it if the mass of the third body can be neglected. For example, the apollo on way to the moon. For a larger number of bodies, you have to compute a numerical solution.
If you are only interested in a bunch of things falling towards the ground, with no or little interaction between them, then you simply use an acceleration of 1 g for each object. This also assumes that you are close enough to the earth surfaces. The value of g also depends where you are on the planet. G, as in Newton's law of gravity, is a universal constant. g as in F=mg is not.

If you really want to get started in numerical computations of the heavens, then maybe this book is of interest to you (it is in English):
http://www.amazon.com/Astronomical-...=sr_1_1?ie=UTF8&s=books&qid=1261345244&sr=8-1
(Ah, another half-hearted project that fell through the cracks.)

You should be forewarned though. I don't think you are embarking on an easy task. Implementing a few formulas from a book is one thing (setting aside all the numerical approximations issues), trying to build a physics engine is another. If you do not know both physics and mathematics, then I recommend you find a good API. You might get some answer eventually if you do it yourself, but that doesn't mean it will be physically correct.

If you are just doing it for fun, though, then go ahead. Just don't expect too much, too early. You could get disappointed very soon.

6. ### MrFusion macrumors 6502a

Joined:
Jun 8, 2005
Location:
West-Europe
#6
This is a nice site that explains some basic physics. But if this is all you have to base yourself on to calculate the orbit of N bodies, then in (insert name of whatever deity you believe in)'name don't do it. First you do 10+* pages of hardcore* math to understand the basics of astrophysics, then you do some numerical simulations.

*depending on your prior knowledge of physics and mathematics.

7. ### mkrishnan Moderator emeritus

Joined:
Jan 9, 2004
Location:
Grand Rapids, MI, USA
#7
Yes, those who went through at least two or three years of undergraduate physics had a fear of explicit solutions to n>2 body problems well drilled into us.

8. ### MrFusion macrumors 6502a

Joined:
Jun 8, 2005
Location:
West-Europe
#8
F = m a = GmM/r^2

Also, don't confuse mass with weight. The mass of an object would still be the same in the absence of gravity. Think of it as an expression of how many atoms something has. An object can gain or lose mass, gain or lose atoms. My mass is the same on earth, as it is on the moon.

However, the weight is not the same. My weight on Earth is much higher than it is on the moon. That is because Earth's gravity is higher than the moon's, and that is because Earth's mass is higher.
The force that the earth applies is much higher because it's mass is higher. This force also depends on the mass of the object (maybe that is what you meant, mkrishnan?). That is why a bag of feathers is easier to carry than a bag of the same volume filled with stones. However, the acceleration is the same for both bags and both bags will hit the ground at the same time. If we neglect friction, that is.

9. ### MrFusion macrumors 6502a

Joined:
Jun 8, 2005
Location:
West-Europe
#9
2 or 3 years? You lucky bastard. This is my eleventh year. A few more months until my Ph.D. defense. Argh, the insanity, the horror, the low pay, the long maddening hours in the lab. Incompetent undergrads. Burning labs. (the last two are not necessarily related.) You should read phdcomics.com. Oh, the brutal reality captured in a 3 frame cartoon. And the thrill/terror of publishing.
I am now certain; the mental wings in hospitals are filled with PhD's in physics.

Why yes, I would do it all over again.

----
Edit:
This is my eleventh year. 11... Oh my... For the love of God, put me out of my misery. Please, I am begging you...

Okay, okay, I exaggerate. It is actually a lot of fun to do, with many great opportunities, but at times also stressful.

10. ### mkrishnan Moderator emeritus

Joined:
Jan 9, 2004
Location:
Grand Rapids, MI, USA
#10
I am not a physicist, but it was my understanding that it is not an overly absurd understanding of relativistic theory that energy in the form of rest mass causes an object to distort the metric of spacetime, thereby causing the effect of gravity. Or that in the running quantum theory, that the higgs field gives a particle its mass, which in turn causes other particles to gravitate towards it....

11. ### gnasher729 macrumors P6

Joined:
Nov 25, 2005
#11
Gravity is three-dimensional, so assume x, y and z coordinates.

Assuming that two bodies are both non-intersecting spheres with constant density, the force of gravity is m1 * m2 * G / r^2, where m1 is the mass of the first body, m2 is the mass of the second constant, r is the distance between the centres of both bodies, and G is the gravitational constant

G = 6.67300 × 10^-11 m^3 / (kg s^2)

The acceleration of the first body is m2 * G / r^2 in the direction of the second body, the acceleration of the second body is m1 * G / r^2 in the direction of the first body. For multiple bodies, calculate the acceleration for all pairs of bodies and add the accelerations for each body. Now all you need is a good differential equation solver and you are there

Things are considerably more complicated if the bodies in question, at least the larger ones, are not perfect spheres with constant density.

12. ### MrFusion macrumors 6502a

Joined:
Jun 8, 2005
Location:
West-Europe
#12
Indeed. No need to keep it simple for you.
Now let see if the LHC will detect the Higgs particle. It it does, it would confirm the standard theory. But from what I understand (it is not my field), that would be boring. Far more interesting for the community is apparently that the Higgs particle is not found. That way, they have to come up with a new theory.