0
$\begingroup$

I'm trying to simulate an object, like a ship, floating around planets. all i can find about anything like this online is the N-body problem, but I'm not trying to simulate the effects of the planets on each other, only on the "ship".

i intend to fake the orbits of the planets to save resources, and the affect of the ship on the planets is negligible, i only wanna simulate the worthwhile things.

my current method defines the force of gravity on the ship as 1/distance, then averages all the gravity vectors to get the final gravity vector for the ship. this doesn't do what i want though. the ship can't maintain a stable orbit around a planet, and ends up flying away, it also ends up gaining energy as it goes on.

$\endgroup$

2 Answers 2

2
$\begingroup$

Some points:

  • Game development stackexchange might be a better place to ask questions about faking details, there don't seem to be any physics concepts at stake here.
  • The Newtonian gravitational force on an object is proportional to one over the distance squared, not one over the distance.
  • You should use the vector sum of all the forces on a body, not the vector average. This shouldn't cause you to be unable to have stable orbits though, it would just reduce the strength of gravity by a factor of 1/N.
  • I've seen some video games just simulate the planets, even if their orbits are circular and they aren't affected by other bodies. This ensures that everything is going through the same physics engine. A straightforward implementation of N-body attraction is $O(N^2)$, but if $N$ is small then we can sweep it under the rug even if many terms are zero each frame (the attractions you don't want to account for).
  • Here is some C style code to timestep a particle with position x[0],y[0],z[0] and velocity vx[0],vy[0],vz[0], under the influence of $N-1$ other bodies. Perhaps it will be useful for you to compare to your implementation to see if you can find any bugs!
/* Pass in positions x,y,z,vx,vy,vz, all arrays of length N. */
/* ax,ay,az should be arrays of length N, not assumed to be zero. */
/* gm is an array of gravitational parameters, basically the masses. Units of (distance)^3/(time)^2. */
double * x,y,z,vx,vy,vz, gm;
double dt; 

double ax=0,ay=0,az=0;
const int i=0; //define a variable i so I can write in the style of n-body code
for(int j=i+1;j<N;j++){
    double d=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+(z[i]-z[j])*(z[i]-z[j]));
    double mult=gm[j]/(d*d*d);
    ax[i]+=(x[j]-x[i])*mult;
    ay[i]+=(y[j]-y[i])*mult;
    az[i]+=(z[j]-z[i])*mult;
}
vx[0]+=dt*ax[0];
vy[0]+=dt*ay[0];
vz[0]+=dt*az[0];
x[0]+=dt*vx[0];
y[0]+=dt*vy[0];
z[0]+=dt*vz[0];
$\endgroup$
3
  • 1
    $\begingroup$ Suggestion: move the drift operations (x += dt v) after the kick operations (v += dt a). This makes the integrator symplectic, so that it will manifestly conserve energy. $\endgroup$
    – Sten
    Dec 23, 2022 at 22:05
  • $\begingroup$ @Sten Oops I meant to include that. Of course you're right, thanks for catching it! $\endgroup$
    – David
    Dec 23, 2022 at 22:59
  • $\begingroup$ It may be easier to calculate the potential by summing $1/r$ terms, then taking the gradient. $\endgroup$
    – J.G.
    Dec 23, 2022 at 23:11
0
$\begingroup$

Forces don't average, but add up as vectors. Gravity scales as $\frac{1} {r^2}.$

You still have to calculate these forces properly and can only skip those between the planets

Thus to get the force or acceleration acting on your ship you have to calculate the acceleration as vector from each planet onto your ship. The magnitude of this vector can be taken as $a \propto \frac{M} {r^2} $ with planet mass $M$ and distance to the ship $r$. Add the vectors before applying the force on the ship.

This said, it will be unexpected to find stable orbits in a situation with more than one planet. There are hardly any stable configuration for 3 bodies, and they are very peculiar.

If you want your ship to orbit nicely one planet, you might want to introduce a (smooth) cut-off distance for the sake of your game beyond which you ignore the influence of other bodies. You might want to make this dependent on the distance to the nearest body.

$\endgroup$
2
  • $\begingroup$ the issue isn't not having a stable orbit with multiple planets, i can't even get a stable orbit with only 1 planet. $\endgroup$ Dec 23, 2022 at 5:50
  • $\begingroup$ Even when you use the correct inverse square law instead of 1/r? And adding vectors instead of averaging? $\endgroup$ Dec 23, 2022 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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