standard newtonian gravity help

I’m making a top down space shooter that involves orbiting around planets at high velocities. I need to make it so that the amount of gravitational pull a planet has is translated directly to the mass of the planet and the mass of the object that it is pulling. I also need to make it so the character won’t hit the planets unless they are traveling directly at them above a certain velocity. If they’re below this velocity, gravitational pull will deflect their trajectory into the orbit of that planet or another planet.

I know the standard equation for this is Fg = G (m1 * m2 ) / r^2…but as an artist I’m not sure how to get that into scripting reliably.

Any suggestions?

Your avatar is rather unsettling :wink:

If your question is simply how to convert the equation:

Fg = G (m1 * m2 ) / r^2

To code and to apply the resultant force, then here’s some completely untested C# code showing how it might be done:

void applyGravitationalForces(Rigidbody body1, Rigidbody body2, float G)
{
    Vector3 diff = body2.transform.position - body1.transform.position;
    float r = diff.magnitude;
    diff /= r;
    float F = (G * body1.mass * body2.mass) / (r * r);
    body1.AddForce(diff * F);
    body2.AddForce(-diff * F);
}

Not sure if that’s what you’re looking for though.