Advertisement
Guest User

Untitled

a guest
Dec 26th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. // calculating orbit forces
  2. foreach(GameObject body1 in bodies)
  3.         {
  4.             foreach (GameObject body2 in bodies) {
  5.                 if (body1.transform.position != body2.transform.position)
  6.                 {
  7.                     float m1 = body1.GetComponent<Rigidbody2D>().mass;
  8.                     float m2 = body2.GetComponent<Rigidbody2D>().mass;
  9.                     decimal g = (decimal)6.674 * (decimal)Mathf.Pow(10, -11);
  10.                     float distance = Vector2.Distance(body1.transform.position, body2.transform.position) / 3000;
  11.                     Vector2 direction = body2.transform.position - body1.transform.position;
  12.                     direction.Normalize();
  13.                     decimal force = g * (decimal)((m1 * m2) / Mathf.Pow(distance, 2));
  14.                     body1.GetComponent<Rigidbody2D>().AddForce(direction * (float)force);
  15.                 }
  16.             }
  17.             body1.GetComponent<Body>().Calculate();
  18.         }
  19.  
  20. // calculating energy
  21. float velSquared = GetComponent<Rigidbody2D>().velocity.sqrMagnitude;
  22.             float mu = (float)g * (GetComponent<Rigidbody2D>().mass + centre.GetComponent<Rigidbody2D>().mass);
  23.             float distance = Vector2.Distance(transform.GetComponent<Rigidbody2D>().position, centre.GetComponent<Rigidbody2D>().position) / 3000;
  24.             float energy = (velSquared / 2) - ((mu) / distance);
  25.             Debug.Log(energy);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement