Guest User

Untitled

a guest
Jan 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Orrery2 : MonoBehaviour
  5. {
  6. public Transform Center;
  7. public Transform Other;
  8. float x;
  9. float y;
  10. float z;
  11. float Begining = 1;
  12. float Elapsed;
  13. float dE = 0;
  14.  
  15. // Update is called once per frame
  16. void FixedUpdate()
  17. {
  18. Elapsed = Begining++;
  19. PositionFromDate(100f, 0.8f, 1f, 2f, 2f, 2f);
  20. Debug.Log(Elapsed);
  21. Debug.Log("x:" + x + ", " + "y:" + y + ", " + "z:" + z);
  22. Other.transform.position = new Vector3(x, y, z);
  23. }
  24. void PositionFromDate(float a, float e, float i, float W, float w, float M)
  25. {
  26. M = M * Elapsed;
  27.  
  28. //Newton's Method
  29. float E = M;
  30. while (Mathf.Abs(dE) < 1e-3)
  31. {
  32. dE = (E - e * Mathf.Sin(E) - M) / (1 - e * Mathf.Cos(E));
  33. E -= dE;
  34. Debug.Log("E:" + E + ", " + "e:" + e + ", " + "M:" + M + ", " + "dE:" + dE);
  35. }
  36.  
  37. //Determine P and Q, 2d coordinate system in plane of orbit
  38. float P = a * (Mathf.Cos(E) - e);
  39. float Q = a * Mathf.Sin(E) * Mathf.Sqrt(Mathf.Abs(1 - Mathf.Pow(e, 2)));
  40.  
  41. Debug.Log("Line 46 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
  42.  
  43. // rotate by argument of periapsis
  44. x = Mathf.Cos(w) * P - Mathf.Sin(w) * Q;
  45. y = Mathf.Sin(w) * P + Mathf.Cos(w) * Q;
  46.  
  47. Debug.Log("Line 52 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
  48.  
  49. // rotate by inclination
  50. z = Mathf.Sin(i) * x;
  51. x = Mathf.Cos(i) * x;
  52.  
  53. Debug.Log("Line 58 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
  54.  
  55. // rotate by longitude of ascending node
  56. float xTemp = x;
  57. x = Mathf.Cos(W) * xTemp - Mathf.Sin(W) * y;
  58. y = Mathf.Sin(W) * xTemp + Mathf.Cos(W) * y;
  59.  
  60. Debug.Log("Line 65 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
  61. }
  62. }
  63.  
  64. using System.Collections;
  65. using System.Collections.Generic;
  66. using UnityEngine;
  67.  
  68. public class Orrery2 : MonoBehaviour
  69. {
  70. public Transform Center;
  71. public Transform Other;
  72. float x;
  73. float y;
  74. float z;
  75. float Begining = 1;
  76. float Elapsed;
  77. float dE = 0;
  78. //a: Semi-Major Axis
  79. //e: Eccentricity
  80. //i: Inclination
  81. //W: Longitude of the Ascending Node
  82. //w: Argument of Periapsis
  83. //M: Mean Anomaly
  84. //E: Eccentric Anomaly
  85.  
  86.  
  87.  
  88. void FixedUpdate()
  89. {
  90. Elapsed = Begining++;
  91. PositionFromDate(1f, 0.8f, 10f, 2f, 2f, 2f);
  92. Debug.Log(Elapsed);
  93. Debug.Log("x:" + x + ", " + "y:" + y + ", " + "z:" + z);
  94. Other.transform.position = new Vector3(x, y, z);
  95. }
  96. void PositionFromDate(float a, float e, float i, float W, float w, float M)
  97. {
  98. a = a * Elapsed; //Semi-Major Axis
  99. e = e * Elapsed; //Eccentricity
  100. i = i * Elapsed; //Inclination
  101. W = W * Elapsed; //Longitude of the Ascending Node
  102.  
  103. //Newton's Method
  104. float E = M;
  105. while (Mathf.Abs(dE) < 1e-3)
  106. {
  107. dE = (E - e * Mathf.Sin(E) - M) / (1 - e * Mathf.Cos(E));
  108. E -= dE;
  109. Debug.Log("E:" + E + ", " + "e:" + e + ", " + "M:" + M + ", " + "dE:" + dE);
  110. }
  111.  
  112. //Determine P and Q, 2d coordinate system in plane of orbit
  113. float P = a * (Mathf.Cos(E) - e);
  114. float Q = a * Mathf.Sin(E) * Mathf.Sqrt(Mathf.Abs(1 - Mathf.Pow(e, 2)));
  115.  
  116. Debug.Log("Line 46 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
  117.  
  118. // rotate by argument of periapsis
  119. x = Mathf.Cos(w) * P - Mathf.Sin(w) * Q;
  120. y = Mathf.Sin(w) * P + Mathf.Cos(w) * Q;
  121.  
  122. Debug.Log("Line 52 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
  123.  
  124. // rotate by inclination
  125. z = Mathf.Sin(i) * x;
  126. x = Mathf.Cos(i) * x;
  127.  
  128. Debug.Log("Line 58 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
  129.  
  130.  
  131. // rotate by longitude of ascending node
  132. float xTemp = x;
  133. x = Mathf.Cos(W) * xTemp - Mathf.Sin(W) * y;
  134. y = Mathf.Sin(W) * xTemp + Mathf.Cos(W) * y;
  135.  
  136. Debug.Log("Line 65 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
  137. }
  138. }
Add Comment
Please, Sign In to add comment