1

Edit: Partial question: The object now moves in an ellipse like it's supposed to, but it orbits around a different point than I intend. How can I make it orbit around a particular object at a point?

New Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Orrery2 : MonoBehaviour
{
    public Transform Center;
    public Transform Other;
    float x;
    float y;
    float z;
    float Begining = 1;
    float Elapsed;
    float dE = 0;

// Update is called once per frame
void FixedUpdate()
{
    Elapsed = Begining++;
    PositionFromDate(100f, 0.8f, 1f, 2f, 2f, 2f);
    Debug.Log(Elapsed);
    Debug.Log("x:" + x + ", " + "y:" + y + ", " + "z:" + z);
    Other.transform.position = new Vector3(x, y, z);
}
void PositionFromDate(float a, float e, float i, float W, float w, float M)
{
    M = M * Elapsed;

    //Newton's Method
    float E = M;
    while (Mathf.Abs(dE) < 1e-3)
    {
        dE = (E - e * Mathf.Sin(E) - M) / (1 - e * Mathf.Cos(E));
        E -= dE;
        Debug.Log("E:" + E + ", " + "e:" + e + ", " + "M:" + M + ", " + "dE:" + dE);
    }

    //Determine P and Q, 2d coordinate system in plane of orbit
    float P = a * (Mathf.Cos(E) - e);
    float Q = a * Mathf.Sin(E) * Mathf.Sqrt(Mathf.Abs(1 - Mathf.Pow(e, 2)));

    Debug.Log("Line 46 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

    // rotate by argument of periapsis
    x = Mathf.Cos(w) * P - Mathf.Sin(w) * Q;
    y = Mathf.Sin(w) * P + Mathf.Cos(w) * Q;

    Debug.Log("Line 52 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

    // rotate by inclination
    z = Mathf.Sin(i) * x;
    x = Mathf.Cos(i) * x;

    Debug.Log("Line 58 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

    // rotate by longitude of ascending node
    float xTemp = x;
    x = Mathf.Cos(W) * xTemp - Mathf.Sin(W) * y;
    y = Mathf.Sin(W) * xTemp + Mathf.Cos(W) * y;

    Debug.Log("Line 65 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
    }
}

The question below was answered

This is in c# in Unity. The intent of the script is to move a sprite around a 2-body orbit. Instead it pops around erratically and rapidly speeds off into the distance. I based most of this off of this other question, but I removed a lot of the conversions from the jpl database in the beginning of the script because I intend to use generated numbers on a larger scale. I don't know much about these maths, so I can't tell what is wrong. The Erratic "orbit"

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Orrery2 : MonoBehaviour
{
    public Transform Center;
    public Transform Other;
    float x;
    float y;
    float z;
    float Begining = 1;
    float Elapsed;
    float dE = 0;
    //a: Semi-Major Axis
    //e: Eccentricity
    //i: Inclination
    //W: Longitude of the Ascending Node
    //w: Argument of Periapsis
    //M: Mean Anomaly
    //E: Eccentric Anomaly



    void FixedUpdate()
    {
        Elapsed = Begining++;
        PositionFromDate(1f, 0.8f, 10f, 2f, 2f, 2f);
        Debug.Log(Elapsed);
        Debug.Log("x:" + x + ", " + "y:" + y + ", " + "z:" + z);
        Other.transform.position = new Vector3(x, y, z);
    }
    void PositionFromDate(float a, float e, float i, float W, float w, float M)
    {
        a = a * Elapsed; //Semi-Major Axis
        e = e * Elapsed; //Eccentricity
        i = i * Elapsed; //Inclination
        W = W * Elapsed; //Longitude of the Ascending Node

        //Newton's Method
      float E = M;
        while (Mathf.Abs(dE) < 1e-3)
        {
            dE = (E - e * Mathf.Sin(E) - M) / (1 - e * Mathf.Cos(E));
            E -= dE;
            Debug.Log("E:" + E + ", " + "e:" + e + ", " + "M:" + M + ", " + "dE:" + dE);
        }

        //Determine P and Q, 2d coordinate system in plane of orbit
        float P = a * (Mathf.Cos(E) - e);
        float Q = a * Mathf.Sin(E) * Mathf.Sqrt(Mathf.Abs(1 - Mathf.Pow(e, 2)));

        Debug.Log("Line 46 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

        // rotate by argument of periapsis
        x = Mathf.Cos(w) * P - Mathf.Sin(w) * Q;
        y = Mathf.Sin(w) * P + Mathf.Cos(w) * Q;

        Debug.Log("Line 52 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

        // rotate by inclination
        z = Mathf.Sin(i) * x;
        x = Mathf.Cos(i) * x;

        Debug.Log("Line 58 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);


        // rotate by longitude of ascending node
        float xTemp = x;
        x = Mathf.Cos(W) * xTemp - Mathf.Sin(W) * y;
        y = Mathf.Sin(W) * xTemp + Mathf.Cos(W) * y;

        Debug.Log("Line 65 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
    }
}
user29010
  • 11
  • 2
  • 3
    One: you're specifying angles in degrees (inclination = 10, for example) but Mathf trig functions work in radians. Two: SMA, eccentricity, inclination, and LAN should be invariant, but you're multiplying them by elapsed time. – Russell Borogove Jan 20 '19 at 19:13
  • 2
    And Three: The one thing that should be changing with time, mean anomaly, is not. – David Hammen Jan 20 '19 at 19:20
  • I also recommend using real-world, SI units with appropriately large magnitudes throughout all your space!math and converting them to unity-convenient units only when setting the new transform. – Russell Borogove Jan 20 '19 at 19:31
  • 1
    Thank you, your first three points worked. I will work on the last one too. The object does move in an ellipse now, but the ellipse is not centered on the right place. How Should I make sure that it centers on the object it is orbiting? This is what it looks like now. – user29010 Jan 20 '19 at 19:36
  • oh that looks like your time steps are much too large. Try making them 10 or 100 times smaller without adding more steps, and see if it makes more sense. That will make the orbits look more realistic at least. – uhoh Jan 20 '19 at 23:06
  • 1
    making the time value any smaller than one makes the object not move at all. Instead I will make the distances bigger to smooth it out. – user29010 Jan 21 '19 at 00:08
  • sounds good. If you have solved some issues you can consider updating your question to the new status, some readers may not carefully read through all comments for updates. Also if you have a partial solution, you could instead post that as a partial answer to your own question, that's okay in Stack Exchange and may help future readers as well. The more detailed information you share, the easier it is for people to help. – uhoh Jan 21 '19 at 01:10

0 Answers0