예제 #1
0
 // Start is called before the first frame update
 void Start()
 {
     _orbitMotion           = GetComponent <OrbitMotion>();
     _trajectoriesContainer = transform.parent.Find("Trajectories");
     _ellipse         = Instantiate(TrajectoryGameObject, _trajectoriesContainer).GetComponent <EllipseRenderer>();
     _ellipse.ellipse = _orbitMotion.orbitPath;
     _ellipse.CalculateEllipse();
 }
예제 #2
0
    private void OrbitParameters()
    {
        if (!moving)
        {
            return;
        }

        Vector3 r = orbitingBody.transform.position - transform.position;

        radius = Vector3.Distance(orbitingBody.transform.position, transform.position);
        float mass1 = rb.mass;
        float mass2 = orbitingBody.GetComponent <Rigidbody>().mass;
        float micro = g * mass2;

        keneticEnergy    = mass1 * velocity.magnitude * velocity.magnitude / 2;
        potenetialEnergy = g * mass2 * mass1 / r.magnitude;
        Energy           = mass1 * velocity.magnitude * velocity.magnitude / 2 - g * mass2 * mass1 / r.magnitude;
        float l = Vector3.Cross(velocity, r).magnitude;

        init_energy = Energy;
        float a = -g * mass2 * mass1 / (2 * Energy);
        float T = (float)Math.Sqrt(4 * Math.PI * Math.PI * a * a * a / (g * mass2));
        float e = (float)Math.Sqrt(1 + 2 * Energy * l * l / (mass1 * mass1 * mass1 * micro * micro));

        ecentricity = e;
        float per = (l * l / (mass1 * micro)) * (1 / (1 + e * (float)Math.Cos(0)));

        apoapsis  = (l * l / (mass1 * micro)) * (1 / (1 + e * (float)Math.Cos(Math.PI)));
        periapsis = per;
        //Does work if sat mass is not 1
        orbital_period = T;
        semiMajor      = (apoapsis + periapsis) / 2;
        semiMinor      = Mathf.Sqrt(semiMajor * semiMajor * (1 - e * e));
        float radi = radius;

        theta = Mathf.Acos(l * l / (radi * mass1 * mass1 * micro * e) - 1 / e);
        beta  = Mathf.Acos(Vector3.Dot(new Vector3(1, 0, 0), r) / r.magnitude);
        //theta = Mathf.Acos(l * l / (apoapsis * mass1 * mass1 * micro * e) - 1 / e);
        print(Mathf.Rad2Deg * theta);
        print(theta);
        Vector3 p2 = new Vector3(radius * Mathf.Cos(theta), 0, radius * Mathf.Sin(Mathf.PI + theta));

        //true if ccw (negative ccw)
        renderer.ellipse.orbitSide = -1f;
        print(Vector3.Dot(Vector3.Cross(r, velocity), new Vector3(0, 1, 0)));
        float orbitSide = 1;
        float clockWise = 1;

        ccw = Vector3.Dot(Vector3.Cross(r, velocity), new Vector3(0, 1, 0)) > 0;
        if (ccw)
        {
            clockWise *= -1;
            //renderer.ellipse.orbitSide = 1f;
            //p2 = new Vector3(radius * Mathf.Cos(theta), 0, radius * Mathf.Sin(theta));
            //renderer.ellipse.orbitSide *= -1f;
            //true if descending
            if (Vector3.Dot(r, velocity) > 0)
            {
                orbitSide = -1;
            }
        }
        else
        {
            orbitSide = -1;
            if (Vector3.Dot(r, velocity) > 0)
            {
                orbitSide = 1;
            }
        }

        //theta *= clockWise;
        theta *= orbitSide;
        beta  *= -Mathf.Sign(r.z);
        //renderer.ellipse.offsetAngle = -beta + theta - Mathf.PI;
        renderer.ellipse.offsetAngle = -beta + Mathf.PI - theta;
        //theta = Mathf.Acos((2 * radius * radius - Vector3.Distance(transform.position, p2)*Vector3.Distance(transform.position, p2)) /
        //                   (2 * radius * radius));
        //theta = Mathf.Acos((2 * radius * radius - Vector3.Distance(transform.position, p2)*Vector3.Distance(transform.position, p2)) / (2 * radius * radius));
        print(Mathf.Rad2Deg * theta);
        print(theta);
        //print(Mathf.Rad2Deg* theta);
        renderer.ellipse.offsetPoint = orbitingBody.transform.position;
        renderer.ellipse.xAxis       = semiMajor;
        renderer.ellipse.yAxis       = semiMinor;
        renderer.ellipse.offset.x    = orbitingBody.transform.position.x - semiMajor + periapsis;
        renderer.ellipse.offset.y    = orbitingBody.transform.position.z;
        renderer.CalculateEllipse();
        theta = Mathf.Rad2Deg * theta;
        beta  = Mathf.Rad2Deg * beta;
        //What position in the orbit am I in? How do I return a future x,y cord?
    }