# Creating a celestial simulation

## Recommended Posts

I'm trying to create a simulation in 3d space. A number of "objects"(think planets or stars) which are all spheres are given a mass, radius, initial position and initial velocity. I want to let the simulation run for a period of time and see how the objects interact. The only forces involved will be the gravitational forces the objects exert on each other. Newtons equations will do fine, I'm not planning to use any relativistic physics. So given the equation for the gravitational force between 2 objects F = (G*m1*m2)/(r^2) where F = force in Newtons G = Gravitattional Constant(6.673 E -11) m1 = Mass of 1st object m2 = Mass of 2nd object r = distance between objects and F=MA How can I calculate the postition and velocity of an object after a given time. The formula above only gives the instanteanous force on an object which will be changing since the object is moving so r will not be constant. Any help would be much appreciated. Thanks.

##### Share on other sites
The key formula you're missing is Newton's Second Law:

Sum of forces = mass * acceleration.

So, your gravitational force equation will give you a force VECTOR. For each object, compute each force vector acting on that object, then sum them together to get the resultant vector. Now, you know that object's mass, and so you can now easily compute the acceleration vector. Now, we use the standard kinematic laws:

x = (1/2)*a*t^2 + v0*t + x0, where vf = vi + a * t, t is time, a is that acceleration vector, x is your current position, and v0 is the initial velocity.

Good luck! Sounds like an interesting project!

EDIT: Error in x equation, updated once ury discovered it

[Edited by - mnansgar on October 22, 2005 8:31:27 PM]

##### Share on other sites
I was going to say if you need to find "r", then just use the Pythagoreous therom and work from there.

##### Share on other sites
pbbm2000, there are really easier ways to simulate trajectories of the planets.

It can be proven that the trajectory of a body under the influence of an "inverse square" force, is a conic section. In the case of an orbiting planet, it's an ellipse.

http://en.wikipedia.org/wiki/Orbit
http://nssdc.gsfc.nasa.gov/planetary/planetfact.html

mnansgar, what you are saying only works when the force is constant, this is hardly the case with planets.

##### Share on other sites
Quote:
 Original post by urymnansgar, what you are saying only works when the force is constant, this is hardly the case with planets.

I guess I should also clarify that with the formulas I gave, you are basically approximating a differential equation using Euler's Law. Hence, you want to iterate through the position and velocity equations with very small time steps (eg make t as small as possible), and update your v0 with each "frame" of motion, recalculating the forces each frame as well. This will give you an approximation of the correct values, more accurate with the smaller your timestep.

##### Share on other sites
Even then it would look like:
x = 1/2*a*t2 + v0*t + x0

##### Share on other sites
Quote:
 Original post by uryEven then it would look like:x = 1/2*a*t2 + v0*t + x0

That's correct -- my mistake, I wrote it rather quickly. I've updated my original quote with credit to you for the find!

##### Share on other sites
Quote:
 Original post by urypbbm2000, there are really easier ways to simulate trajectories of the planets.It can be proven that the trajectory of a body under the influence of an "inverse square" force, is a conic section. In the case of an orbiting planet, it's an ellipse.Please read the following links for more information:http://en.wikipedia.org/wiki/Orbithttp://nssdc.gsfc.nasa.gov/planetary/planetfact.htmlmnansgar, what you are saying only works when the force is constant, this is hardly the case with planets.
And what you are proposing is only true with a single source of gravity. like the sun acting on planets individually. That fine if you want to ignore the erects of gravity between planets however. If there are other sources of gravity it gets much more complex. I'd imagine you need to get in to calculus and RK4 integrators to get the best results here.

##### Share on other sites
Grain, of course you are right. All sources of gravitational forces affect the trajectory of a planet. In fact, a reason why some scientists refused to accept Pluto as a planet was that it was affected too much by the gravitational force of Neptune.

Still, using the model that I proposed is very common. It's used for almost 400 hundred year now and still gives pretty much accurate results. Because this approach is only an approximation, the ellipse constants have to be updated every once in a while, to reflect the current state of the solar system.

Altough an approximation, the given error is very small and can't be possibly seen by a naked eye. IMHO, this approach is perfect for a game.

By the way, the NASA site contains pretty much everything you need to know to set up both of the experiments.

##### Share on other sites
Quote:
 Original post by uryAltough an approximation, the given error is very small and can't be possibly seen by a naked eye. IMHO, this approach is perfect for a game.
This is true of our solar system, because the majority of the mass is in one point at the center. But I think the OP wanted to do more than that. If for example he wants to model a galaxy or a twin star system or a star system with very massive gas giants, a conic approach will be insufficient.

##### Share on other sites
Quote:
 Original post by GrainThis is true of our solar system, because the majority of the mass is in one point at the center. But I think the OP wanted to do more than that. If for example he wants to model a galaxy or a twin star system or a star system with very massive gas giants, a conic approach will be insufficient.

Hm.. I don't know. Since we are probably talking about a game and not something that NASA would use to plan their next deep space exploration missions, we can usually settle for a less "accurate" solution.

You really have to know what you are doing if you want a real dynamic simulation of a star system. The numerical integrator must be VERY accurate because momentum and energy must be conserved. Even if that wasn't enough, due to the huge differences in the planet masses, you'll get a very stiff ODE to integrate.

Try to set up a simple experiment where a single point-mass circles around the origin under the influence of an inverse square force.

[Edited by - ury on October 23, 2005 12:41:17 PM]

##### Share on other sites
The gravitational force between two objects is a force that pulls one object towards another. So you want to multiply your force by the direction(vector from one object to another normalized), and to get acceleration you divide the whole thing by the mass of the object being pulled. So what you add to the velocity (acceleration) is something like:

V1+=((X2-X1)/r*(G * m1 * m2)/(r^2)) /m1

which simplifies to :

V1+=((X2-X1)*(g* m2)/(r^3))

Do that for all combinations of objects and then add the velocity of each object to the position of each object. I think someone already said this, but small timesteps are crucial. I look forward to seeing how this turns out. I've never seen a nice G-sim in 3D.

##### Share on other sites
I think Grain is right: if you want to model similar sized bodies orbiting each other then you need a very good numerical scheme. I think there are analytical solutions for special cases of the 3 body problem. Above that it's a question of number crunching.

##### Share on other sites
Sorry It took me so long to reply. This isn't for a video game it's a simulation so I would like it as accurate as possible. However it's not for NASA or anything so if so some error is acceptable. A space shuttle isn't going to crash into to moon of its off. Finding the instantinous accerleration on any given object would be fairly easy and then using basic kenamatic equations would give a good approximation but I think there may be some problems. I want to run this simulation for long periods of time(hunderds of millions of years) and be able to quickly jump forward in time. I would also like to be able to move the simulation back in time but this is not as important. Since each iteration is only going is only going to increase the time by a small ammount the simulation will run very slowly. Also, in each iteration there will be a very small amount of error so after millions and millions of years the error will most likely be very large. Do you think it would be possbile to make the time increment large enough for the simulation to be able to run fast enough while keeping the error minimual?
Kepler's equations for orbatial motion are for a planet orbiting a single star. There could very well be another star or object in the area that could significantly effect the orbit so I don't think Kepler's equations will work here.
The Ideal solution would be two equations, both a function of time. One would give the velocity and one would give the position. I've been messing around with this for a few days and it seems the key is this relationship...

A = dV/dT

V = dX/dT

but so far I havent been able to come up with anything. Thanks for all the input and any more advice would be great. Thanks.

##### Share on other sites
What you need to remember is that each object has position and velocity and needs to have its coordinates updated - so that's 3N values to be computed at each interval. Where N is the number of objects. You'd integrate for new velocities using some technique and then find the new positions.

From the new positions you'd calculate the new forces on each object (and hence accelerations) and so on.

##### Share on other sites
Yes, each object will have an x,y and z coordinate that will need to be updated. I think that the only plausible solution would be a function of time that gives position. You would have to calculate the x, y and z positions seperatly but the same formula could be used. That way it would be possible to find the position of any object after any amount of time with only a handful of calculations.

##### Share on other sites
There are a few sites out there dealing with the 3-body problem. One interesting example being:

http://www.ifmo.ru/butikov/Projects/Collection.html
With applet examples.

Wikipedia covers the history of solving for the 3-body here:
http://en.wikipedia.org/wiki/Three-body_problem

Note the "chaos" factor in determining orbits.

Here's a site giving the research into the calculation for the solar system:
http://www.fortunecity.com/emachines/e11/86/solarsys.html

Not many sites cover the nitty-gritty of setting up the equations, sadly.

[Edited by - embpos on October 25, 2005 10:11:26 AM]

##### Share on other sites
I've been working on pretty much the exact same problem in a game I'm creating and I've run into the same problem. The problem doesn't only exist when trying to speed up or slow down the simulation. It also presents itself as a function of varying framerates (at least if you're using the frame time to scale motion). I've been working on it for several days, and it's a seemingly intractable problem because, as some of the sites listed above indicate, there just aren't any general equations governing the position/velocity of objects being affected by more than one gravitational source.

You can calculate position with respect to time for an object being affected by one gravitational source, however. Somebody correct me if my math is wrong here, but if you take the equation for acceleration in gravity:

a = (G*m)/(d^2)

Since we know that velocity is simply the application of acceleration over time we can say:

v = (G*m)t/(d^2)

We also know that velocity is simply the derivative of position with respect to time so:

dx/dt = (G*m)t/d^2

Solve for dx and take the integral and you get:

x = (G*m)t^2/(2d^2)

Again, that only works when dealing with one gravity source, but perhaps it can be a jumping off point for something more robust.

EDIT - Just realized that this equation is the same equation (pretty much) posted by mnansgar above. Should have seen that.... :P

##### Share on other sites
Quote:
 Solve for dx and take the integral and you get:x = (G*m)t^2/(2d^2)

Yeh - it's a bit more complicated than that because d varies with t i.e. d =d(t)- except in the simple case of circular motion.

##### Share on other sites
Quote:
Original post by embpos
Quote:
 Solve for dx and take the integral and you get:x = (G*m)t^2/(2d^2)

Yeh - it's a bit more complicated than that because d varies with t i.e. d =d(t)- except in the simple case of circular motion.

Unfortunately, it's even more complicated than this.
Since we are formulating our problem in 3D, the force is actually a vector.

In the case of two 3D points A and B, define:
vector: R = B - A
scalar: r = |R|

The force acting of body A is:
F = (G*Ma*Mb/r3) * R

Even if the distance is constant in time, the vector R is not. This is pretty evident since we have a circular motion.

##### Share on other sites
You need something like:

F_ijx = Sum_j [-(G*mi*mj*R_ijx)/R_ij ^3

For the x component force on body i due to all j where j != i.

R_ijx is R_ix - R_jx

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628367
• Total Posts
2982284

• 10
• 9
• 13
• 24
• 11