Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

joystick-bounding-box: Fixed issue around bounding box determination #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Assets/BetterJoystick/Runtime/Scripts/Joystick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down