Loading...
93
2 years 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

My Pokémon Baking Book arrived today

#MyPokemonBakingBook #InsightEditions #Giveaway

It's just rain

Made in Blender using Dynamic Paint and particles.

#Blender #3D #3DArt #Animation #Rain #Storm #Window #DynamicPaint

What's Your Most Feared Minecraft Mob?

My first thought was this green guy who appears out of nowhere and erases all your efforts in one second

#MostFearedMob #ArtWeeklies #Blender #Animation #Minecraft #Cave #Creeper #3D #3DArt

Madeline is the main protagonist of the mountain climbing platformer, Celeste. The game has been praised for its visuals, gameplay, and introspective narrative. The game's creator Maddy Thorson confirmed that Madeline is trans in 2020.

Comparison of view transforms in Blender using Christmas candles as an example

#Blender #3D #3DArt #ViewTransform #Christmas #Candles

LGBTQ+ Game Dev Icons:

Rebecca Heineman is a trans woman, award-winning Space Invaders player, and game dev who worked as a dev on The Bard’s Tale, Myth III, and more.

She was inducted into the International Video Game Hall of Fame in 2017.

Chibi drawing by Aikeji, modeled and animated in 3D.

Made in Blender.

#Blender #3D #3DArt #Modeling #Animation #Chibi #Aikeji

Beware of the armored frog

Kikkerstein's tank made in Blender for the War Thunder quest.

#Blender #3D #3DArt #Modeling #Animation #Kikkerstein #Kikkertank #Tank #WarThunder

Congratulations to @Freddy9207 , our #LTCOKills winner!

They posted a Mission Accomplished screen showing that they got 1,814 kills!

Check out his original Post: https://gamejolt.com/p/ltcokills-it-was-alright-vkccpcc2

What's Your Favorite Video Game Console Of All Time?

PC - I've never had a console.

#GJAsks #Blender #3DArt #3D #PC #Room #Darkness