Planetary Gravity Physics

BlitzMax Forums/BlitzMax Programming/Planetary Gravity Physics

jondecker76(Posted 2005) [#1]
Hello,

I'm having trouble finding the formulas I need to make a solar system model (in 2d). I'm caught on the planetary gravity right now. ANybody have any helpful hints?

thanks,

Jon


Dreamora(Posted 2005) [#2]
What you need is the gravitation law ( F = (G * m1 * m2) / (Distance^2) where G is the gravitation constant ... similar formula to coulomb)

This holds for "human - earth" as well as for "earth - moon" or any other pair of masses.


jondecker76(Posted 2005) [#3]
ok, thanks for the help.. I've ran across this formula doing some searches but what is the gravitational constant?

thanks


Bot Builder(Posted 2005) [#4]
Depends on the measurements you are using - size/distances as well as mass.

http://en.wikipedia.org/wiki/Gravitational_constant


Who was John Galt?(Posted 2005) [#5]
To model a solar system, I would just move the planes in circular or elliptical paths - no need to actally model gravity. If you want to see how to do a simple 2d simulation of gravity, search these forums - the question has been answered many times before.


Diordna(Posted 2005) [#6]
Yes, but if you're making a Gravity Wars game, the gravity equation is insanely important...and you use planets. Could this be what you're doing? I've always wanted to make a Gravity Wars game but never got around to it.

(To see what Gravity Wars is, just Google "death star battles" and play the Flash knock-off.)


jondecker76(Posted 2005) [#7]
yeah, exactly what i was wanting to do. Kind of like a Scorched Earth where the two players are on different planets. To place their shots, they will have account for cravity effecting the projectile on its way.

thanks for the replies


teamonkey(Posted 2005) [#8]
Gravitational constant G = 6.67E-11 N m^2 kg^-2 (i.e. if your forces are measured in Newtons, mass in kilogrammes and distance in metres). Essentially it's a fiddle factor though - Start at G=1.0 and change it until it feels right, reducing it if the force is too strong and increasing it if it's too weak.

You'll probably want to use a = F/m to find the acceleration of each body. That is, a1=G*m2/(r*r) and a2=G*m1/(r*r).

And in 2D vector form,
r = Sqr(r_x*r_x + r_y*r_y)
a1_x = G*m2*r_x/(r*r*r)
a1_y = G*m2*r_y/(r*r*r)

(I think :) ) Where <r_x, r_y> is the distance between objects in x and y directions, m2 is the mass that's attracting your object, <a1_x,a1_y> is the acceleration at that moment in time at that point in space.

A good resource that I use: http://scienceworld.wolfram.com/physics/Gravity.html


jondecker76(Posted 2005) [#9]
thanks a lot, lots of useful help on these boards


Lazze(Posted 2005) [#10]
You might want to use Keppler for the planets and Newton for your ships as it will keep the calculations down to a minimum.

Using Newton for the whole system gets quite heavy when the amount of bodies increase as you have to calculate the gravitational effect from all the bodies on each body.


ImaginaryHuman(Posted 2005) [#11]
You could use an image which represents the gravitational field of each planet, pre-rendered from pre-calculating the gravity at each pixel. Several of these could easily be stamped down and referenced, or just looked up with an offset, when you want to know how much force to add to things that are moving around. It would be a bitmap of custom types, in a way. Quicker if you can precalc than doing it realtime.


PGF(Posted 2005) [#12]
A couple of tips:

- I'd certainly make all of the major masses like Stars, Planets and Moons affect each other and everything else. Ships, Bullets and Missiles are negligible in comparison so they need to be affected by the major masses but not each other.

- There is no real performance problem doing n-body calculations in realtime for n up to 20, 30 or maybe 50. By 100 it is definitely bogging down an average PC. Much of the fun comes from the chaotic effects of masses on each other so I'd go with proper calculations rather than pre-determined elipses for orbits.

- You can combine the gravity calculations with collision detection since you need the distance in both cases. You need objects to have an appreciable radius and to do collision detection. Point masses can get too close to each other and exert enormous force. By the next calculation step they have overstepped each other so don't 'feel' a corresponding pull back to slow them down. Objects zoom out of your system from close encounters. This does actually happen in reality as asteroids, comets and even stars can be 'ejected' by close encounters. However if you don't mask the effect with a radius it is unrealistic.

- For n bodies, a simplistic approach is to calculate each body against each other body requiring n * (n-1) calculations. A better approach is to consider them in pairs (A, B). Once you have the distance of A from B, it is the same distance from B to A and there are common terms that can be found in the equations for the force of A on B and the force of B on A. This leads to only n * (n - 1) / 2 calculations which is better. Use somthing like:

For A = 1 to n
For B = A + 1 to n
CalcForce(A, B) ' Accumulate components of force in both A and B
Next B

CalcNetEffect(A) ' All forces applied to A. Can now calculate its acceleration and new position.
Next A

This is just pseudocode. You need to define what is in the objects and how to iterate through them.

For a real challenge:

BlitzMax is quite capable of doing realtime image scaling and rotation so you can get fancy by having spinning worlds and being able to zoom in and out. Use real images. Make the view track an object to see others whizzing past. Leave object trails. Draw vectors on objects showing their velocity and acceleration.

A very rich topic.


RexRhino(Posted 2005) [#13]
Just a note from when I was working on something similiar... you are going to have to play with the equations a little bit. I created what I thought was a reasonably accurate model of the solar system and a space ship... but the ship always flew out of the system too fast, or barely moved, or always was sucked up into planets. I never quite got it to work out.


Najdorf(Posted 2005) [#14]
physical constants are useless in game developement. you just need the formula F = G * m1 * m2 / Distance^2 and you have to play with G in order to get the results you want.

so you have two bodies with masses m1,m2, velocity vx1,vx2,vy1,vy2, position x1,x2,y1,y2 then in the main loop to simulate the gravity between them you do:

distance=sqr((x1-x2)*(x1-x2)+(y1-y2)+(y1-y2))
F= G * m1 * m2 / (Distance*distance)

xcomponent=(x1-x2)/distance
ycomponent=(y1-y2)/distance

deltav1=F/m1
deltav2=F/m2

then

vx2=vx2+deltav2*xcomponent
vx1=vx1-deltav1*xcomponent
vy2=vy2+deltav2*ycomponent
vy1=vy1-deltav1*ycomponent

and the usual

x1=x1+vx1
x2=x2+vx2
y1=y1+vy1
y2=y2+vy2

that's all


ImaginaryHuman(Posted 2005) [#15]
I am thinking that while accurate physics works in the real world with real-sized objects, if you are going to use the same physics to try to simulate abnormally proportioned objects then you aren't going to see the resutls that you imagine. If you are thinking of these relatively small spheres on the screen as having the gravitational pull of a planet, surely some adjustment is going to have to be made to scale that down given the size proportions, especially if there is an unrealistic or inconsistent sense of proportions between ships and planets. I'm guessing the planets would be more like small asteroids in comparison.