Skip to content

Commit

Permalink
Improve performance and accuracy when planet trajectories are enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
SkutteOleg committed Nov 21, 2022
1 parent f75a234 commit 4ddeb2e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
6 changes: 5 additions & 1 deletion TrajectoryPrediction/AstroObjectTrajectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ public void UpdateTrajectory()
}
else
{
if (TrajectoryPrediction.Parallelization || TrajectoryPrediction.Multithreading && Thread.CurrentThread == TrajectoryPrediction.MainThread)
if (TrajectoryPrediction.Parallelization)
{
Busy = true;
TrajectoryPrediction.SimulateTrajectoryMultiThreaded(_body, _framePosition, _frameVelocity, Trajectory, null, false, false, () => Busy = false);
}
else
{
Busy = true;
TrajectoryPrediction.SimulateTrajectory(_body, _framePosition, _frameVelocity, Trajectory);
Busy = false;
}
}
}

Expand Down
10 changes: 7 additions & 3 deletions TrajectoryPrediction/MapMarkerTrajectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private void Start()
{
_marker = GetComponent<CanvasMapMarker>();
_body = _marker._rigidbodyTarget;

if (!_body)
return;

Expand Down Expand Up @@ -119,7 +119,7 @@ public void UpdateTrajectory()
return;

_updatedThisFrame = true;

if (_forceDetector._activeVolumes.Count == 0 || _forceDetector._activeVolumes.All(volume => volume is not GravityVolume))
{
if (TrajectoryPrediction.Multithreading)
Expand All @@ -139,13 +139,17 @@ public void UpdateTrajectory()
}
else
{
if (TrajectoryPrediction.Multithreading)
if (TrajectoryPrediction.Parallelization)
{
Busy = true;
TrajectoryPrediction.SimulateTrajectoryMultiThreaded(_body, _framePosition, _frameVelocity, Trajectory, Locator.GetReferenceFrame()?.GetAstroObject(), true, TrajectoryPrediction.PredictGravityVolumeIntersections, () => Busy = false);
}
else
{
Busy = true;
TrajectoryPrediction.SimulateTrajectory(_body, _framePosition, _frameVelocity, Trajectory, Locator.GetReferenceFrame()?.GetAstroObject(), true, TrajectoryPrediction.PredictGravityVolumeIntersections);
Busy = false;
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions TrajectoryPrediction/TrajectoryPrediction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class TrajectoryPrediction : ModBehaviour
public static Color PlanetTrajectoriesColor { get; private set; }

public static bool Parallelization { get; private set; }
internal static Thread MainThread { get; private set; }

public static event Action OnConfigUpdate;
public static event Action OnBeginFrame;
Expand All @@ -42,7 +41,6 @@ public class TrajectoryPrediction : ModBehaviour

private void Awake()
{
MainThread = Thread.CurrentThread;
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly());
GlobalMessenger.AddListener("EnterMapView", OnEnterMapView);
GlobalMessenger.AddListener("ExitMapView", OnExitMapView);
Expand Down Expand Up @@ -95,8 +93,11 @@ private void FixedUpdate()

EndFrame();
BeginFrame();
foreach (var visualizer in TrajectoryVisualizers)
visualizer.Visualize();
new Thread(() =>
{
foreach (var visualizer in TrajectoryVisualizers)
visualizer.Visualize();
}).Start();
}
else
{
Expand Down

0 comments on commit 4ddeb2e

Please sign in to comment.