0

Hey and thanks for your help in advanced.

I've watched a few youtube videos on how to add Solar System and orbiting Gravity in Unity and ended up using the this for help for the solar system gravity part.

https://www.youtube.com/watch?v=Ouu3D_VHx9o&t=114s&ab_channel=Brackeys

But right after i decided to trying to make my planet orbit the sun i used this Wikipage for the math equation

But for some reason either my planets flies away of the sun start flying towards the planet. I've been looking around for 2 days and can't seem to make it work and tried diffrent type of possiblies.

Here is my code

public class Planets : MonoBehaviour
{
    const float G = 100F;

    public Rigidbody rb;

    public float CurrentV;

    private void FixedUpdate()
    {

        Planets[] attractors = FindObjectsOfType<Planets>();

        foreach (Planets AllPlanets in attractors)
        {
            if (AllPlanets != this)
            {
                Orbiting(AllPlanets, CurrentV);
                Attract(AllPlanets);
            }
        }

    }
    void Attract(Planets objToAttract)
    {
        Rigidbody RbTpAttract = objToAttract.rb;
        Vector3 direction = rb.position - RbTpAttract.position;
        float distance = direction.magnitude;
        float ForceMagnitude = G * (rb.mass * RbTpAttract.mass) / Mathf.Pow(distance, 2);
        Vector3 Force = direction.normalized * ForceMagnitude;
        RbTpAttract.AddForce(Force);

    }


    void Orbiting(Planets objToAttract, float CV)
    {
        Rigidbody RbTpAttract = objToAttract.rb;
        Vector3 direction = rb.position - RbTpAttract.position;
        float distance = direction.magnitude;
        float ForceMagnitude = Mathf.Sqrt((G * rb.mass) / (2 / distance - 1 / RbTpAttract.mass));
        Vector3 Force = direction.normalized * ForceMagnitude;
        RbTpAttract.velocity += Force;
    }

 }
Valon Cazimi
  • 29
  • 1
  • 9

1 Answers1

1

The problem is that the formula for orbital speed is used to derive the speed of an object in orbit, but you're using it as a form of constant thrust applied to each body towards each other. That's a bit like calculating the speed of a moving car, and then applying the same speed back to it as an impulse!

The only force experienced by objects in orbit is the one you get from Newton's law G * m * m / r*r. In order to actually orbit though, the planets will need an initial velocity - this can be calculated from the orbital speed formula. Calculate it at the given distance, and apply it on Start() in a direction perpendicular to the orbital plane and the direction to the sun (or whatever you want to orbit), you can get this from dir = Vector3.Cross(sunDir, Vector3.up).normalized

Note that gravitational systems are not numerically stable in physics engines relying on euler integration (such as PhysX). You need things like Runge-Kutta integration for that, or the planets will eventually lose their orbit if you leave the simulation running for long enough.

Kalle Halvarsson
  • 1,240
  • 1
  • 7
  • 15
  • You mention Runge-Kutta integration - Can you recommand a doc to read? – Valon Cazimi Jan 19 '22 at 21:51
  • 1
    Btw thanks for your help - it's working fine right now <3 – Valon Cazimi Jan 19 '22 at 21:52
  • Here is a great primer on the Runge-Kutta method: https://www.haroldserrano.com/blog/visualizing-the-runge-kutta-method Here is another SO thread where someone is doing N-body simulation, it has a Runge-Kutta method written in C#: https://stackoverflow.com/questions/28489789/n-body-simulation-in-c-sharp If you want to do it this way, you need to skip rigidbodies (or use kinematic ones) and write your own physics for movement. That's easier than it sounds though, since everything is a sphere and things don't bounce around too much. – Kalle Halvarsson Jan 19 '22 at 22:28