From 73a9b61d5c24ad449cacc222c038b05880481ead Mon Sep 17 00:00:00 2001 From: Nik Date: Fri, 5 Jan 2024 02:29:50 +1100 Subject: [PATCH] joystick-bounding-box: Fixed issue around bounding box determination --- .../Runtime/Scripts/Joystick.cs | 14 +++++++++---- .../Scripts/JoystickRect/Models/CircleRect.cs | 3 ++- .../JoystickRect/Models/RectangleRect.cs | 21 ++++++++----------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Assets/BetterJoystick/Runtime/Scripts/Joystick.cs b/Assets/BetterJoystick/Runtime/Scripts/Joystick.cs index d1679bd..0bab275 100644 --- a/Assets/BetterJoystick/Runtime/Scripts/Joystick.cs +++ b/Assets/BetterJoystick/Runtime/Scripts/Joystick.cs @@ -81,13 +81,13 @@ private void OnDragEvent(MouseDragEvent obj) else { var centering = GetCentering(); - var mousePosition = obj.MousePosition; - if (!_joystickRect.InRange(obj.MousePosition)) + var mousePosition = obj.MousePosition - GetJoystickRelativeAreaPosition(); + if (!_joystickRect.InRange(mousePosition)) { - mousePosition = _joystickRect.GetPointOnEdge(obj.MousePosition); + mousePosition = _joystickRect.GetPointOnEdge(mousePosition); } - var panelPosition = this.WorldToLocal(mousePosition - centering); + var panelPosition = mousePosition - centering; _joystickInner.style.top = panelPosition.y; _joystickInner.style.left = panelPosition.x; newValue = mousePosition - _joystickRect.Center; @@ -123,6 +123,12 @@ private void PlaceJoystickAtCenter() _joystickInner.style.top = (joystickResolvedStyle.height - GetTopOffset(joystickResolvedStyle)) / 2f - centering.y; _joystickInner.style.left = (joystickResolvedStyle.width - GetLeftOffset(joystickResolvedStyle)) / 2f - centering.x; } + + private Vector2 GetJoystickRelativeAreaPosition() + { + var joystickElementPosition = worldBound; + return new Vector2(joystickElementPosition.x, joystickElementPosition.y); + } private float GetTopOffset(IResolvedStyle resolved) { diff --git a/Assets/BetterJoystick/Runtime/Scripts/JoystickRect/Models/CircleRect.cs b/Assets/BetterJoystick/Runtime/Scripts/JoystickRect/Models/CircleRect.cs index 27459a5..dfd501e 100644 --- a/Assets/BetterJoystick/Runtime/Scripts/JoystickRect/Models/CircleRect.cs +++ b/Assets/BetterJoystick/Runtime/Scripts/JoystickRect/Models/CircleRect.cs @@ -17,7 +17,8 @@ public CircleRect(VisualElement root) public float Radius => _root.layout.width / 2f; - public Vector2 Center => _root.layout.center; + // Center relative to the container + public Vector2 Center => _root.layout.center - _root.layout.position; public bool InRange(Vector2 point) { diff --git a/Assets/BetterJoystick/Runtime/Scripts/JoystickRect/Models/RectangleRect.cs b/Assets/BetterJoystick/Runtime/Scripts/JoystickRect/Models/RectangleRect.cs index db0901c..475cea1 100644 --- a/Assets/BetterJoystick/Runtime/Scripts/JoystickRect/Models/RectangleRect.cs +++ b/Assets/BetterJoystick/Runtime/Scripts/JoystickRect/Models/RectangleRect.cs @@ -17,34 +17,31 @@ public RectangleRect(VisualElement root) public float Radius => Mathf.Max(_root.layout.width, _root.layout.height) / 2f; - public Vector2 Center => _root.layout.center; + public Vector2 Center => _root.layout.center - _root.layout.position; public bool InRange(Vector2 point) { - return _root.layout.Contains(point); + // Check the point exists within the rectangle relative to bounds + return new Rect(0,0, _root.layout.width, _root.layout.height).Contains(point); } public Vector2 GetPointOnEdge(Vector2 point) { - return GetClosestPointOnRectEdge(_root.layout, point); + return GetClosestPointOnRectEdge(point); } - private Vector2 GetClosestPointOnRectEdge(Rect rect, Vector2 point) + private Vector2 GetClosestPointOnRectEdge(Vector2 point) { - // Get the center point of the rectangle - var center = rect.center; - - // Calculate the half-width and half-height of the rectangle - var halfWidth = rect.width / 2; - var halfHeight = rect.height / 2; + var halfWidth = Center.x; + var halfHeight = Center.y; // Calculate the distance between the center point and the outer point - var direction = point - center; + var direction = point - Center; // Calculate the distance between the center point and the closest point on the edge of the rectangle var deltaX = Mathf.Clamp(direction.x, -halfWidth, halfWidth); var deltaY = Mathf.Clamp(direction.y, -halfHeight, halfHeight); - var closestPoint = center + new Vector2(deltaX, deltaY); + var closestPoint = Center + new Vector2(deltaX, deltaY); return closestPoint; }