Skip to content

Commit

Permalink
feat(Extension): add vector direction extension
Browse files Browse the repository at this point in the history
A Direction extension method for Vector2 and Vector3 will get the
direction vector between a source point and a target point.
  • Loading branch information
thestonefox committed Apr 30, 2023
1 parent 66306c9 commit 883cf7d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Runtime/Extension/Vector2Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,18 @@ public static Vector2 UnsignedEulerToSignedEuler(this Vector2 eulerAngles)
{
return new Vector2(eulerAngles.x.GetSignedDegree(), eulerAngles.y.GetSignedDegree());
}

/// <summary>
/// Gets the direction from a source to a target.
/// </summary>
/// <param name="source">The starting point.</param>
/// <param name="target">The finishing point.</param>
/// <param name="isNormalized">Whether to normalize the direction.</param>
/// <returns>The direction that the target is in from the source.</returns>
public static Vector2 Direction(this Vector2 source, Vector2 target, bool isNormalized = false)
{
Vector3 heading = target - source;
return heading / (isNormalized ? heading.magnitude : 1f);
}
}
}
13 changes: 13 additions & 0 deletions Runtime/Extension/Vector3Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,18 @@ public static Vector3 UnsignedEulerToSignedEuler(this Vector3 eulerAngles)
{
return new Vector3(eulerAngles.x.GetSignedDegree(), eulerAngles.y.GetSignedDegree(), eulerAngles.z.GetSignedDegree());
}

/// <summary>
/// Gets the direction from a source to a target.
/// </summary>
/// <param name="source">The starting point.</param>
/// <param name="target">The finishing point.</param>
/// <param name="isNormalized">Whether to normalize the direction.</param>
/// <returns>The direction that the target is in from the source.</returns>
public static Vector3 Direction(this Vector3 source, Vector3 target, bool isNormalized = false)
{
Vector3 heading = target - source;
return heading / (isNormalized ? heading.magnitude : 1f);
}
}
}
10 changes: 10 additions & 0 deletions Tests/Editor/Extension/Vector2ExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,15 @@ public void UnsignedEulerToSignedEuler()
Assert.That(new Vector2(270f, 270f).UnsignedEulerToSignedEuler(), Is.EqualTo(new Vector2(-90f, -90f)).Using(comparer));
Assert.That(new Vector2(360f, 360f).UnsignedEulerToSignedEuler(), Is.EqualTo(new Vector2(0f, 0f)).Using(comparer));
}

[Test]
public void Direction()
{
Vector2 source = Vector2.zero;
Vector2 target = new Vector2(1.234f, 3.23f);
Vector2EqualityComparer comparer = new Vector2EqualityComparer(0.1f);
Assert.That(Vector2Extensions.Direction(source, target), Is.EqualTo(target).Using(comparer));
Assert.That(Vector2Extensions.Direction(source, target, true), Is.EqualTo(target.normalized).Using(comparer));
}
}
}
10 changes: 10 additions & 0 deletions Tests/Editor/Extension/Vector3ExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,15 @@ public void UnsignedEulerToSignedEuler()
Assert.That(new Vector3(270f, 270f, 270f).UnsignedEulerToSignedEuler(), Is.EqualTo(new Vector3(-90f, -90f, -90f)).Using(comparer));
Assert.That(new Vector3(360f, 360f, 360f).UnsignedEulerToSignedEuler(), Is.EqualTo(new Vector3(0f, 0f, 0f)).Using(comparer));
}

[Test]
public void Direction()
{
Vector3 source = Vector3.zero;
Vector3 target = new Vector3(1.234f, 3.23f, 2.1234f);
Vector3EqualityComparer comparer = new Vector3EqualityComparer(0.1f);
Assert.That(Vector3Extensions.Direction(source, target), Is.EqualTo(target).Using(comparer));
Assert.That(Vector3Extensions.Direction(source, target, true), Is.EqualTo(target.normalized).Using(comparer));
}
}
}

0 comments on commit 883cf7d

Please sign in to comment.