Advertisement
JontePonte

trajectory line bad

Mar 30th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. using Unity.Collections;
  2. using Unity.Jobs;
  3. using Unity.Mathematics;
  4. using UnityEngine;
  5.  
  6. [RequireComponent(typeof(LineRenderer))]
  7. public class TrajectoryLine : MonoBehaviour
  8. {
  9. public int iterations = 100;
  10. public float lineThickness = 5f;
  11. public int iterationsToAddPoint;
  12.  
  13. public bool scaleWithCamera;
  14.  
  15. public Rigidbody2D target;
  16. public Camera scalingCamera;
  17. public Rigidbody2D planet;
  18.  
  19. public Vector2 periapsisPosition { get; private set; }
  20. public Vector2 apoapsisPosition { get; private set; }
  21. public float periapsisAltitude { get; private set; }
  22. public float apoapsisAltitude { get; private set; }
  23.  
  24. LineRenderer lr;
  25.  
  26. JobHandle handle;
  27. NativeList<TrajectoryPoint> result;
  28.  
  29. NativeArray<float2> periapsisPos = new NativeArray<float2>(1, Allocator.Persistent);
  30. NativeArray<float2> apoapsisPos = new NativeArray<float2>(1, Allocator.Persistent);
  31. NativeArray<float> periapsisAlt = new NativeArray<float>(1, Allocator.Persistent);
  32. NativeArray<float> apoapsisAlt = new NativeArray<float>(1, Allocator.Persistent);
  33.  
  34. void Awake()
  35. {
  36. lr = GetComponent<LineRenderer>();
  37.  
  38. result = new NativeList<TrajectoryPoint>(Allocator.Persistent);
  39. }
  40.  
  41. void FixedUpdate()
  42. {
  43. if (handle != null)
  44. {
  45. handle.Complete();
  46.  
  47. apoapsisPosition = apoapsisPos[0];
  48. periapsisPosition = periapsisPos[0];
  49. apoapsisAltitude = apoapsisAlt[0];
  50. periapsisAltitude = periapsisAlt[0];
  51.  
  52. periapsisAlt[0] = float.MaxValue;
  53. apoapsisAlt[0] = float.MinValue;
  54.  
  55. UpdateLineRenderer();
  56. }
  57.  
  58. ScheduleJob();
  59. }
  60.  
  61. void ScheduleJob()
  62. {
  63. result.Clear();
  64.  
  65. TrajectoryLineJob job = new TrajectoryLineJob
  66. {
  67. result = result,
  68.  
  69. position = target.position,
  70. velocity = target.velocity,
  71. mass = target.mass,
  72.  
  73. periapsisPos = periapsisPos,
  74. apoapsisPos = apoapsisPos,
  75. periapsisAlt = periapsisAlt,
  76. apoapsisAlt = apoapsisAlt,
  77.  
  78. iterations = iterations,
  79. iterationsToAddPoint = iterationsToAddPoint,
  80.  
  81. planetPosition = planet.position,
  82. planetRadius = planet.transform.localScale.x / 2,
  83. planetMass = planet.mass,
  84.  
  85. gravitationalConstant = (float)GravitationalForce.GravitationalConstant,
  86.  
  87. deltaTime = Time.fixedDeltaTime
  88. };
  89.  
  90. handle = job.Schedule();
  91. }
  92.  
  93. void UpdateLineRenderer()
  94. {
  95. lr.positionCount = 0;
  96. lr.startWidth = scaleWithCamera ? lineThickness * scalingCamera.orthographicSize : lineThickness;
  97. lr.endWidth = scaleWithCamera ? lineThickness * scalingCamera.orthographicSize : lineThickness;
  98.  
  99. TrajectoryPoint periapsis = new TrajectoryPoint
  100. {
  101. altitude = float.MaxValue
  102. };
  103. TrajectoryPoint apoapsis = new TrajectoryPoint
  104. {
  105. altitude = float.MinValue
  106. };
  107.  
  108. for (int i = 0; i < result.Length; i++)
  109. {
  110. lr.positionCount++;
  111. lr.SetPosition(lr.positionCount - 1, new Vector2(result[i].position.x, result[i].position.y));
  112.  
  113. if (result[i].altitude < periapsis.altitude)
  114. {
  115. periapsis = result[i];
  116. }
  117.  
  118. if (result[i].altitude > apoapsis.altitude)
  119. {
  120. apoapsis = result[i];
  121. }
  122. }
  123. }
  124.  
  125. void OnDestroy()
  126. {
  127. if (handle != null)
  128. {
  129. handle.Complete();
  130. }
  131.  
  132. result.Dispose();
  133.  
  134. periapsisPos.Dispose();
  135. apoapsisPos.Dispose();
  136. periapsisAlt.Dispose();
  137. apoapsisAlt.Dispose();
  138. }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement