Advertisement
klassekatze

GravityCenterCalculator

Jul 20th, 2023 (edited)
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. /*
  2.  * R e a d m e
  3.  * -----------
  4.  *
  5.  * In this file you can include any instructions or other comments you want to have injected onto the
  6.  * top of your final script. You can safely delete this file if you do not want any such comments.
  7.  */
  8.  
  9. // cpa_time(): compute the time of CPA for two tracks
  10. //    Input:  two tracks Tr1 and Tr2
  11. //    Return: the time at which the two tracks are closest
  12. public static double cpa_time(Vector3D Tr1_p, Vector3D Tr1_v, Vector3D Tr2_p, Vector3D Tr2_v)
  13. {
  14.     Vector3D dv = Tr1_v - Tr2_v;
  15.  
  16.     double dv2 = Vector3D.Dot(dv, dv);
  17.     if (dv2 < 0.00000001)      // the  tracks are almost parallel
  18.         return 0.0;             // any time is ok.  Use time 0.
  19.  
  20.     Vector3D w0 = Tr1_p - Tr2_p;
  21.     double cpatime = -Vector3D.Dot(w0, dv) / dv2;
  22.  
  23.     return cpatime;             // time of CPA
  24. }
  25.  
  26. public static string Vector2GPSString(string l, Vector3D v)
  27. {
  28.     //GPS: klassekatze #2:4186.55:17490.12:19153.15:#FFB775F1:
  29.     return "GPS:" + l + ":" + v.X.ToString("0.00") + ":" + v.Y.ToString("0.00") + ":" + v.Z.ToString("0.00") + ":#FFB775F1:";
  30. }
  31.  
  32. IMyShipController c = null;
  33.  
  34. public Program()
  35. {
  36.     c = (IMyShipController)GridTerminalSystem.GetBlockWithName("Cockpit");
  37. }
  38. Vector3D p1, p2 = Vector3D.Zero;
  39. Vector3D g1, g2 = Vector3D.Zero;
  40.  
  41. public void Main(string argument, UpdateType updateSource)
  42. {
  43.     if (argument == "m")
  44.     {
  45.         if (p1 == Vector3D.Zero)
  46.         {
  47.             p1 = c.GetPosition();
  48.             g1 = Vector3D.Normalize(c.GetNaturalGravity());
  49.         }
  50.         else if (p2 == Vector3D.Zero)
  51.         {
  52.             p2 = c.GetPosition();
  53.             g2 = Vector3D.Normalize(c.GetNaturalGravity());
  54.         }
  55.  
  56.         if (p2 != Vector3D.Zero)
  57.         {
  58.             double t = cpa_time(p1, g1, p2, g2);
  59.             if (t != 0.0)
  60.             {
  61.                 Vector3D center = p1 + (g1 * t);
  62.                 string o = t + "\n" + g1.ToString()+"\n"+g2.ToString()+"\n"+
  63.                 Vector2GPSString("pcenter", center);
  64.                 Me.CustomData = o;
  65.                 Echo(o);
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement