Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Orbital Mechanics URGENT Help Needed

Discussion in 'Physics' started by FredOld, Sep 16, 2018 at 7:58 PM.

  1. FredOld

    FredOld

    Joined:
    Mar 26, 2017
    Posts:
    3
    Hello

    I have been working on this project for a long time and now I am doing this part where it predicts the orbit of a rocket which you can control and apply thrust to in any direction. I am using the trajectory equation to this and it mostly works. Here's my code. If you know about this stuff you should be able to see what I am doing and hopefully what I am doing wrong as well.

    It can predict the orbit if I don-t apply any thrust to the ship and just leave it with it's initial velocity but when I apply thrust it gets really weird and shows orbits which the ship isn't even on. This is really important as I have to give this in along with a dissertation soon, and I just can't figure this out. I don't know who to ask except for you...

    This is the equation I am using, see https://en.wikipedia.org/wiki/Orbital_mechanics for more info.

    This is what it looks like with it's initial velocity, this orbit matches the actual movement of the ship:


    But then this is what it looks like when I have applied thrust in the retrograde direction:


    Another problem is that the in theory constant vector A is not actually constant, it seems to change as time passes.

    I anyone thinks they can help, please ask questions if you need more details.

    Thank you.

    This is the script attached to the ship.
    Code (CSharp):
    1. public class Ship2 : MonoBehaviour {
    2.  
    3.     public Vector3 initialVelocity;
    4.     public float lineQuality;
    5.     public float thrust = 1.0f;
    6.  
    7.     List<Vector3> points = new List<Vector3>();
    8.     Vector3[] pointsArray = null;
    9.  
    10.  
    11.     LineRenderer trajectoryLine;
    12.     Rigidbody rb;
    13.     GameObject planet;
    14.  
    15.     Vector3 vecv;
    16.     Vector3 vecr;
    17.     Vector3 vech;
    18.     Vector3 veca;
    19.     Vector3 vece;
    20.     float r;
    21.  
    22.     [SerializeField]
    23.     float e;
    24.  
    25.     double mu;
    26.     double G;
    27.  
    28.     Vector3 currentPoint;
    29.  
    30.     float angle = 0f;
    31.     float endAngle;
    32.  
    33.     private void Start()
    34.     {
    35.         trajectoryLine = GetComponent<LineRenderer> ();
    36.         trajectoryLine.startWidth = 1.0f;
    37.         trajectoryLine.endWidth = 1.0f;
    38.  
    39.         rb = GetComponent<Rigidbody>();
    40.         planet = GameObject.FindGameObjectWithTag("Attractor");
    41.  
    42.         rb.velocity = initialVelocity / 100000;
    43.  
    44.         G = planet.GetComponent<Planet>().gconst;
    45.         mu = planet.GetComponent<Planet>().mass * G;
    46.  
    47.         //CalculateR();
    48.  
    49.         angle = 0f;
    50.  
    51.     }
    52.  
    53.     private void Update()
    54.     {
    55.         vecv = transform.position + rb.velocity * 100000;
    56.         vecr = (transform.position - planet.transform.position) * 100000;
    57.         vech = transform.position +  Vector3.Cross(vecr, vecv);
    58.         veca = /*transform.position + */ Vector3.Cross(vecv, vech) - (float)mu * vecr.normalized; ///THE PROBOLEM IS HERE, THE A VECTOR SUDDENLY SHIFTs
    59.  
    60.         Debug.DrawLine(Vector3.zero, vecr, Color.white);
    61.         Debug.DrawLine(transform.position, vech, Color.blue);
    62.         Debug.DrawLine(transform.position, vecv, Color.red);
    63.         Debug.DrawLine(Vector3.zero /*transform.position*/, veca, Color.yellow);
    64.  
    65.         e = veca.magnitude / (float)mu;
    66.  
    67.         //Force Recalculate trajectory
    68.         if (Input.GetKeyDown(KeyCode.Return))
    69.             CalculateR();
    70.  
    71.         //Aplicar força a la nau
    72.         if (Input.GetKey(KeyCode.Space))
    73.             ApplyThrust();
    74.  
    75.         if (Input.GetKeyDown(KeyCode.W))
    76.             rb.velocity *= 1.1f;
    77.         if (Input.GetKeyDown(KeyCode.S))
    78.             rb.velocity *= 0.9f;
    79.     }
    80.  
    81.     void CalculateR ()
    82.     {
    83.         angle = Mathf.Atan2(veca.x, veca.z) * Mathf.Rad2Deg;
    84.         endAngle = angle + 360f;
    85.         Debug.Log("Calculating...");
    86.         while (angle <= endAngle)
    87.         {
    88.             r = (vech.sqrMagnitude) / (veca.magnitude * Mathf.Cos(angle * Mathf.Deg2Rad) + (float)mu) / 100000;
    89.  
    90.             //Debug.Log(currentPoint.magnitude);
    91.             currentPoint.x = Mathf.Cos(angle * Mathf.Deg2Rad);
    92.             currentPoint.z = Mathf.Sin(angle * Mathf.Deg2Rad);
    93.             currentPoint.y = 0f;
    94.  
    95.             currentPoint = currentPoint.normalized * r;
    96.             Debug.DrawLine(Vector3.zero,currentPoint, Color.cyan);
    97.  
    98.             //Debug.Log(r);
    99.             points.Add(currentPoint);
    100.             angle += 1.0f;      
    101.         }
    102.         DrawLine();
    103.         points.Clear();
    104.     }
    105.  
    106.     public void DrawLine()
    107.     {
    108.         trajectoryLine.positionCount = 361 /*/ (int)lineQuality*/;
    109.         foreach (Vector3 point in points)
    110.         {
    111.             trajectoryLine.SetPosition(points.IndexOf(point), point);
    112.         }
    113.         Debug.Log("Done");
    114.     }
    115.  
    116.     void ApplyThrust ()
    117.     {
    118.         rb.AddForce(transform.up * thrust, ForceMode.Impulse);
    119.         Debug.Log("Thrust applied");
    120.  
    121.         // Un cop s-ha aplicat la força, s'haurà de recalcular la trajectòria.
    122.         CalculateR();
    123.     }
    124. }

    And this is attached to the planet:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class Planet : MonoBehaviour
    6. {
    7.     public double gconst = 0.000000000067408; // Gravitational constant adapted proportionally to distances and scale (normal value = 6.674*10^-11 = 0.0000000000674)
    8.     public double mass;
    9.  
    10.     GameObject ship;
    11.     Vector3 force;
    12.     Vector3 direction;
    13.     float sqrDistance;
    14.     [SerializeField]
    15.     float time;
    16.     Vector3 earthRotation;
    17.     LineRenderer trajectoryLine;
    18.  
    19.     private void Start()
    20.     {
    21.         ship = GameObject.FindGameObjectWithTag("Ship");
    22.  
    23.         earthRotation = new Vector3(0f, -0.004178f, 0f);
    24.  
    25.         trajectoryLine = GetComponent<LineRenderer>();
    26.     }
    27.  
    28.     void FixedUpdate()
    29.     {
    30.         if(ship != null)
    31.             Attract(ship);
    32.         Rotate();
    33.         time += Time.deltaTime;
    34.         //Debug.Log(time);
    35.     }
    36.  
    37.  
    38.  
    39.     void Attract(GameObject bodyToAttract)
    40.     {
    41.         Rigidbody rbToAttract = bodyToAttract.GetComponent<Rigidbody>();
    42.  
    43.         direction = (transform.position - rbToAttract.position) * 100000;
    44.         sqrDistance = direction.sqrMagnitude;
    45.  
    46.         //Debug.Log(distance);
    47.  
    48.         if (sqrDistance == 0)
    49.             return;
    50.  
    51.         double forceMagnitude = gconst * (mass / sqrDistance);
    52.  
    53.         //Debug.Log(forceMagnitude);
    54.  
    55.         force = direction.normalized * (float)forceMagnitude / 100000;
    56.         rbToAttract.AddForce(force, ForceMode.Force);
    57.     }
    58.  
    59.     void Rotate()
    60.     {
    61.         transform.Rotate(earthRotation * Time.deltaTime);
    62.     }
    63. }
    64.  
     
    Last edited: Sep 17, 2018 at 6:45 PM
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    1,426
    Anything urgent affecting your personal life, should be really thought before hand.
    If you doing academic stuff, that I would really consider 3D space, for best results.

    Possibly you taking into calculations wrong axis, or direction.
    Check also time steps, when calculating predicted path.