1

so I am making a simple simulation of different planets with individual velocity flying around space and orbiting each other.

I plan to simulate their pull on each other by considering each planet as projecting their own "gravity vector field." Each time step I'm going to add the vectors outputted from each planets individual vector field equation (V = -xj + (-yj) or some notation like it) except the one being effected in the calculation, and use the effected planets position as input to the equations.

However this would inaccurate, and does not consider the gravitational pull as continuous and constant. Bow do I calculate the movement of my planets if each is continuously effecting the others?

Thanks!

1
  • Google for "many body problem" Oct 27, 2011 at 17:50

2 Answers 2

2

In addition to what Blender writes about using Newton's equations, you need to consider how you will be integrating over your "acceleration field" (as you call it in the comment to his answer).

The easiest way is to use Euler's Method. The problem with that is it rapidly diverges, but it has the advantage of being easy to code and to be reasonably fast.

If you are looking for better accuracy, and are willing to sacrifice some performance, one of the Runge-Kutta methods (probably RK4) would ordinarily be a good choice. I'll caution you that if your "acceleration field" is dynamic (i.e. it changes over time ... perhaps as a result of planets moving in their orbits) RK4 will be a challenge.

Update (Based on Comment / Question Below):

If you want to calculate the force vector Fi(tn) at some time step tn applied to a specific object i, then you need to compute the force contributed by all of the other objects within your simulation using the equation Blender references. That is for each object, i, you figure out how all of the other objects pull (apply force) and those vectors when summed will be the aggregate force vector applied to i. Algorithmically this looks something like:

for each object i
    Fi(tn) = 0
    for each object ji
        Fi(tn) = Fi(tn) + G * mi * mj / |pi(tn)-pj(tn)|2

Where pi(tn) and pj(tn) are the positions of objects i and j at time tn respectively and the | | is the standard Euclidean (l2) normal ... i.e. the Euclidean distance between the two objects. Also, G is the gravitational constant.

Euler's Method breaks the simulation into discrete time slices. It looks at the current state and in the case of your example, considers all of the forces applied in aggregate to all of the objects within your simulation and then applies those forces as a constant over the period of the time slice. When using

ai(tn) = Fi(tn)/mi

(ai(tn) = acceleration vector at time tn applied to object i, Fi(tn) is the force vector applied to object i at time tn, and mi is the mass of object i), the force vector (and therefore the acceleration vector) is held constant for the duration of the time slice. In your case, if you really have another method of computing the acceleration, you won't need to compute the force, and can instead directly compute the acceleration. In either event, with the acceleration being held as constant, the position at time tn+1, p(tn+1) and velocity at time tn+1, v(tn+1), of the object will be given by:

pi(tn+1) = 0.5*ai(tn)*(tn+1-tn)2 + vi(tn)*(tn+1-tn)+pi(tn)
vi(tn+1) = ai(tn+1)*(tn+1-tn) + vi(tn)

The RK4 method fits the driver of your system to a 2nd degree polynomial which better approximates its behavior. The details are at the wikipedia site I referenced above, and there are a number of other resources you should be able to locate on the web. The basic idea is that instead of picking a single force value for a particular timeslice, you compute four force vectors at specific times and then fit the force vector to the 2nd degree polynomial. That's fine if your field of force vectors doesn't change between time slices. If you're using gravity to derive the vector field, and the objects which are the gravitational sources move, then you need to compute their positions at each of the four sub-intervals in order compute the force vectors. It can be done, but your performance is going to be quite a bit poorer than using Euler's method. On the plus side, you get more accurate motion of the objects relative to each other. So, it's a challenge in the sense that it's computationally expensive, and it's a bit of a pain to figure out where all the objects are supposed to be for your four samples during the time slice of your iteration.

2
  • Thanks andand, but by "a challenge" do you mean hard to make accurate or hard to understand/ make work in code? Also, I'm still not quite sure what to search for to learn how to learn to use Euler's method to integrate through a vector field, especially a 'global field' that's composed of multiple fields projected by each object. On top of that how do I factor in the continually changing velocity of the object due to the acceleration caused by the 'global field'.
    – Griffin
    Oct 28, 2011 at 3:49
  • Wow, really appreciate the explanation! And yes, the field of vectors is changing, so does this mean the only way to increase accuracy is through more iterations/ estimations how the field will change during the time slice? I'm trying to avoid holding the force vector as a constant during the time slice.
    – Griffin
    Oct 28, 2011 at 21:42
1

There is no such thing as "continuous" when dealing with computers, so you'll have to approximate continuity with very small intervals of time.

That being said, why are you using a vector field? What's wrong with Newton?

enter image description here

enter image description here

And the sum of the forces on an object is that above equation. Equate the two and solve for a

So you'll just have to loop over all the objects one by one and find the acceleration on it.

5
  • Well I find vector fields more flexible and easier to visualize. I plan to eventually use the gravity coded in this simulation for games where I might want to "bend or warp" gravity a bit, through editing the vector field equation. Also, I know computers aren't "continuous" but I've recently learned about the natural exponential 'e' which fixes the problem of calculating continuous compounding.... Which made me think that there's got to be a way do something similarly with a planet moving through gravity fields... What do you think?
    – Griffin
    Oct 23, 2011 at 8:31
  • You seem to be a bit mixed up with gravity. That above equation is how the force on an object due to gravity is calculated. If you were to use anything else, then you wouldn't be calculating the force of gravity, but instead some other force. There is no e in Newtonian physics, as compound interest is only derived with a limit, which is of little use in this context.
    – Blender
    Oct 24, 2011 at 17:18
  • Other than that, how are you planning on using the vector field? You'll be integrating over it, which will be absolute hell in 3D and with constantly changing fields (as you'll have to add all of the fields together).
    – Blender
    Oct 24, 2011 at 17:20
  • Ok, I guess I shouldn't really be calling it gravity because really it's more of just an acceleration field, defining how much an object accelerates in which direction depending on it's position. I plan to use the vector fields to form interesting and warping attraction between different objects, liquid/wind dynamics, really everything that has to do with non-contact and some contact forces. And this problem of constantly changing fields is exactly what I aim to fix in the future, but for now how do I find what an objects exact movement through mixed, but static acceleration fields?
    – Griffin
    Oct 25, 2011 at 6:50
  • There is no exact solution, so everything will be an approximation. You'll still have to loop over all your objects. For each object looped over, loop over all other objects, find their attraction force, and then accelerate your objects (after all have been looped over) over a time interval.
    – Blender
    Oct 25, 2011 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.