Loading...
92
11 months ago

A planet orbiting a star in orbits of different eccentricities.

A long time ago I wrote a script in Unity to simulate the motion of bodies in elliptical orbits.

#Unity #Programming #Planet #Star #Orbit #Orbiting #Elliptical #Elliptic


This script calculates the position of the body as a function of time based on the formulas for mean, eccentric and true anomaly.

Since the equation for the eccentric anomaly cannot be solved algebraically, I wrote a simple numerical solver using the bisection method (RootFinder).

		
			public class Orbiter : MonoBehaviour
{
    public float period, distance, eccentricity;
    public float time = 0;
    private Transform Trs;
    private float M, E, T, r, tmp;
    private Vector2 pos = new Vector2();

    private float Ecc(float X)
    {
        return X - eccentricity * Mathf.Sin(X) - M;
    }

    void Start ()
    {
        Trs = GetComponent<Transform>();
    }
	
    void Update ()
    {
        //M
        M = ((time / period) * 2 * Mathf.PI) % (2 * Mathf.PI);
        //E
        try { E = RootFinder.Find(Ecc, 0, 2 * Mathf.PI, 1e-3f); } catch { Debug.Log("Range"); }
        //T
        tmp = Mathf.Cos(E);
        T = Mathf.Acos(eccentricity / (tmp * eccentricity - 1) - tmp / (tmp * eccentricity - 1));
        if (E > Mathf.PI) T = 2 * Mathf.PI - T;
        //R
        tmp = Mathf.Cos(T);
        r = distance / (1 + eccentricity * tmp);
        //Pos
        pos.x = r * Mathf.Sin(T);
        pos.y = r * tmp;
        Trs.localPosition = pos;
        //Time
        time += Time.deltaTime;
    }
}

public class RootFinder
{
    public static uint maxIter = 100;

    public delegate float Function(float x);

    public static float Find(Function F, float A, float B, float P)
    {
        float
            C = (A + B) / 2,
            Cv = F(C),
            Av = F(A),
            Bv = F(B);
        uint i = 0;
        if (Mathf.Abs(Av) <= P) return A;
        if (Mathf.Abs(Bv) <= P) return B;
        if (Mathf.Sign(Av) == Mathf.Sign(Bv)) throw new Exception("Bad Interval");
        while (Mathf.Abs(Cv) > P && i < maxIter)
        {
            if (Cv > 0)
            {
                if (Av > 0)
                {
                    A = C;
                    Av = Cv;
                }
                else
                {
                    B = C;
                    Bv = Cv;
                }
            }
            else
            {
                if (Av > 0)
                {
                    B = C;
                    Bv = Cv;
                }
                else
                {
                    A = C;
                    Av = Cv;
                }
            }
            C = (A + B) / 2;
            Cv = F(C);
            i++;
        }
        return C;
    }
}
		
	


0 comments

Loading...

Next up

Bottled water tree

#PlantPhoto

Snow globe with a city in retro style

Made in Blender for Art Weeklies - Item and City themes.

#Blender #Animation #3D #3DArt #Modeling #ArtWeeklies #City #Item #SnowGlobe #Retro #LowPoly #PerVertexLighting #PS1Graphics #Old3DGraphics #Early3DGraphics

My Pokémon Baking Book arrived today

#MyPokemonBakingBook #InsightEditions #Giveaway

Happy #WIPWednesday!

Are you working on a game? A song? Something else? Tell us in the comments!

Praise the Cat!

Blobcats worshiping their creator in an ancient temple.

Made in Blender for Art Weeklies - Monument theme.

#Blender #Animation #3D #3DArt #Modeling #ArtWeeklies #Cat #Blobcat #KniteBlargh #Golden #Monument #Temple #Pyramid #Ancient

🕹️ Enter The Highrise Game Jam Before It Ends On May 5! 🕹️

Learn the rules here: https://gamejolt.com/c/gamedev/highrisejam

Learn how to make a world on Highrise (which you MUST do to enter the jam) here: https://gamejolt.com/p/the-highrise-game-jam-is-underway-watch-t…

The jam has cash prizes! 💸💸💸

Diglett as chocolate cake

Made in Blender.

#MyPokeCake #Pokemon #Diglett #Cake #Blender #3D #3DArt #Modeling

Highrise Game Jam Reminder: In order to enter to win the Jam, your game must be created in Unity and submitted using the Highrise Studio Hub.

Learn all the deets about the jam at https://gamejolt.com/c/gamedev/highrisejam

Spirit of the snowy forest

Made in Blender.

Anime girl model made in VRoid Studio (last image).

#Blender #3D #3DArt #Modeling #Animation #Anime #Girl #VRoid #VRoidStudio #Snow #Forest #Winter #Night #Blobcat