From 6aaf693e4b5e4212dc7247ede586b2c6650e2677 Mon Sep 17 00:00:00 2001 From: Florian Glawogger <73254596+FlorianGlawogger@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:30:52 +0100 Subject: [PATCH] Improve stop drawing check to also check distance to start once furthest distance is not updating anymore --- .../FieldLineStopDrawing/CloseFieldLine.cs | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/unity/Assets/Maroon/reusableScripts/FieldLines/FieldLineStopDrawing/CloseFieldLine.cs b/unity/Assets/Maroon/reusableScripts/FieldLines/FieldLineStopDrawing/CloseFieldLine.cs index 88ca9f595..ace130a67 100644 --- a/unity/Assets/Maroon/reusableScripts/FieldLines/FieldLineStopDrawing/CloseFieldLine.cs +++ b/unity/Assets/Maroon/reusableScripts/FieldLines/FieldLineStopDrawing/CloseFieldLine.cs @@ -4,12 +4,13 @@ [RequireComponent(typeof(FieldLine))] public class CloseFieldLine : MonoBehaviour { - private FieldLine fieldLine; - [Tooltip("Don't stop drawing until at least this many line segments have been drawn (i.e. the number of StopDrawing calls)")] [SerializeField] private int minLineSegmentCount = 3; + private FieldLine fieldLine; private int currentLineSegmentIndex; + private float furthestDistanceToStart; + private float distanceToStartWhenDrawingShouldStop; private void Awake() { @@ -23,7 +24,10 @@ private void Awake() private void OnStartDrawing() { + // Reset field line's segment-related variables currentLineSegmentIndex = -1; + furthestDistanceToStart = 0f; + distanceToStartWhenDrawingShouldStop = ((fieldLine.minLineSegmentLength + fieldLine.maxLineSegmentLength) / 2f); } /// @@ -36,16 +40,31 @@ private bool StopDrawing(Vector3 lineSegmentPosition) { currentLineSegmentIndex++; - if (currentLineSegmentIndex < minLineSegmentCount) + // Check if distance to start pos got larger + Vector3 fieldLineStartPosition = fieldLine.transform.TransformPoint(Vector3.zero - fieldLine.originOffset); + float lineDistanceToStart = Vector3.Distance(lineSegmentPosition, fieldLineStartPosition); + if (lineDistanceToStart > furthestDistanceToStart) { - // Don't stop drawing until minLineSegmentCount has been reached - return false; + // distance to start position got larger + furthestDistanceToStart = lineDistanceToStart; } - Vector3 fieldLineStartPosition = fieldLine.transform.TransformPoint(Vector3.zero - fieldLine.originOffset); - float lineDistanceToStart = Vector3.Distance(lineSegmentPosition, fieldLineStartPosition); - bool closeToStart = lineDistanceToStart <= (fieldLine.minLineSegmentLength * 2); + if (currentLineSegmentIndex < minLineSegmentCount) + // Don't stop drawing until at least minLineSegmentCount has been reached + return false; + + if (lineDistanceToStart < furthestDistanceToStart) + { + // furthestDistanceToStart did not update + // i.e. we might be already getting close to start pos + if (lineDistanceToStart < distanceToStartWhenDrawingShouldStop) + { + return true; + } + } - return closeToStart; + // If very small field line loop, we also should stop when very close to Start even when we have not yet reached a smaller furthestDistanceToStart + bool VeryCloseToStartPos = lineDistanceToStart <= (fieldLine.minLineSegmentLength * 2); + return VeryCloseToStartPos; } }