Search code examples
c#unity-game-enginephysics

Why does this orbiting object not perfectly repeat its orbit?


Clip of wonky orbit
I made a rudimentary (I'm still extremely proud of it) orbital sim. The satellite does not go around the planet on the second pass the same way it went around on the first pass. Its periapsis is constantly moving, not unlike orbital precession. I would think it actually is orbital precession, but there are no other orbital bodies in the simulation. The satellite exerts no force back on the Earth. Is it a problem with my code or is this a real phenomenon? The code below are the only lines that manage the orbital physics.

public static float Gravity(Transform m2Loc, Rigidbody m1Mass, Rigidbody m2Mass, float gravityConstant)
{

    float force = (m1Mass.mass * m2Mass.mass) / MathF.Pow(m2Loc.position.magnitude, 2) * gravityConstant;

    return force;
}

i.mass.AddForce(i.pos.position * -1 * Equations.Gravity(i.pos, planetMass, i.mass, gravity), ForceMode.Force);

The pink is the satellite’s trail. The gravitational equation is used F=(G*M1*M2)/R^2, except G is 1. The Earth has a mass of 1000, the ship with a mass of 10. Force is applied to the ship using Rigidbody.AddForce every frame under FixedUpdate(). The entire script.

EDIT:

I let the simulation run for a bit and I see that the orbits are NOT random, because A: Side Orbits The orbits never exceed or go below a certain altitude and B: Side Orbits The orbits NEVER deviate from their inclination.

Still need an answer, though.


Solution

  • It seems to be because two variables for the gravity equation that I used (G = 1, M1 = 1000) were not best for the scale of my simulation.

    I'm no orbital physicist, so I can't explain WHY this fixed it exactly, but I changed the Gravitational Constant G to 5 * 10^-3 and the mass of the Earth to 1 * 10^9. I guess this setup more closely resembled real life scales, so gravity needed less time to act correctly? This change led to objects' perigees and apogees staying closer to their original position for longer; less ridiculous orbital precession.

    There's this value called the Standard Gravitational Parameter (SGP). It's defined as G(M1 + M2). Plugging in real life values, this number comes out to 3.986 * 10^14. Using my original values, it came out to 1000. I then thought that manipulating my G and M1 so that it is closer to the real SGP would result in basically zero orbital precession but deviating from my fixed SGP of 5 * 10^6 in either direction led to more orbital precession.

    So, that answers my question, but I do not understand the mathematical reasoning.