Add Vector3.SignedAngle utility method

This commit is contained in:
Wojtek Figat
2024-10-29 23:55:42 +01:00
parent b1a54d2967
commit c1bd42ff7e
3 changed files with 50 additions and 2 deletions

View File

@@ -324,6 +324,15 @@ float Float3::Angle(const Float3& from, const Float3& to)
return Math::Acos(dot) * RadiansToDegrees;
}
template<>
float Float3::SignedAngle(const Float3& from, const Float3& to, const Float3& axis)
{
const float angle = Angle(from, to);
const Float3 cross = Cross(from, to);
const float sign = Math::Sign(axis.X * cross.X + axis.Y * cross.Y + axis.Z * cross.Z);
return angle * sign;
}
template<>
Float3 Float3::SnapToGrid(const Float3& pos, const Float3& gridSize)
{
@@ -652,6 +661,15 @@ double Double3::Angle(const Double3& from, const Double3& to)
return Math::Acos(dot) * RadiansToDegrees;
}
template<>
double Double3::SignedAngle(const Double3& from, const Double3& to, const Double3& axis)
{
const double angle = Angle(from, to);
const Double3 cross = Cross(from, to);
const double sign = Math::Sign(axis.X * cross.X + axis.Y * cross.Y + axis.Z * cross.Z);
return angle * sign;
}
template<>
Double3 Double3::SnapToGrid(const Double3& pos, const Double3& gridSize)
{
@@ -881,6 +899,12 @@ int32 Int3::Angle(const Int3& from, const Int3& to)
return 0;
}
template<>
int32 Int3::SignedAngle(const Int3& from, const Int3& to, const Int3& axis)
{
return 0;
}
template<>
Int3 Int3::SnapToGrid(const Int3& pos, const Int3& gridSize)
{

View File

@@ -1345,7 +1345,7 @@ namespace FlaxEngine
}
/// <summary>
/// Calculates the angle (in degrees) between <paramref name="from"/> and <paramref name="to"/>. This is always the smallest value.
/// Calculates the angle (in degrees) between <paramref name="from"/> and <paramref name="to"/> vectors. This is always the smallest value.
/// </summary>
/// <param name="from">The first vector.</param>
/// <param name="to">The second vector.</param>
@@ -1358,6 +1358,21 @@ namespace FlaxEngine
return (Real)Math.Acos(dot) * Mathr.RadiansToDegrees;
}
/// <summary>
/// Calculates the signed angle (in degrees) between <paramref name="from"/> and <paramref name="to"/> vectors. This is always the smallest value. The sign of the result depends on: the order of input vectors, and the direction of the <paramref name="axis"/> vector.
/// </summary>
/// <param name="from">The first vector.</param>
/// <param name="to">The second vector.</param>
/// <param name="axis">The axis around which the vectors are rotated.</param>
/// <returns>The angle (in degrees).</returns>
public static Real SignedAngle(Vector3 from, Vector3 to, Vector3 axis)
{
Real angle = Angle(from, to);
Vector3 cross = Cross(from, to);
Real sign = Mathr.Sign(axis.X * cross.X + axis.Y * cross.Y + axis.Z * cross.Z);
return angle * sign;
}
/// <summary>
/// Projects a 3D vector from object space into screen space.
/// </summary>

View File

@@ -812,13 +812,22 @@ public:
static FLAXENGINE_API T TriangleArea(const Vector3Base& v0, const Vector3Base& v1, const Vector3Base& v2);
/// <summary>
/// Calculates the angle (in degrees) between from and to. This is always the smallest value.
/// Calculates the angle (in degrees) between from and to vectors. This is always the smallest value.
/// </summary>
/// <param name="from">The first vector.</param>
/// <param name="to">The second vector.</param>
/// <returns>The angle (in degrees).</returns>
static FLAXENGINE_API T Angle(const Vector3Base& from, const Vector3Base& to);
/// <summary>
/// Calculates the signed angle (in degrees) between from and to vectors. This is always the smallest value. The sign of the result depends on: the order of input vectors, and the direction of the axis vector.
/// </summary>
/// <param name="from">The first vector.</param>
/// <param name="to">The second vector.</param>
/// <param name="axis">The axis around which the vectors are rotated.</param>
/// <returns>The angle (in degrees).</returns>
static FLAXENGINE_API T SignedAngle(const Vector3Base& from, const Vector3Base& to, const Vector3Base& axis);
/// <summary>
/// Snaps the input position onto the grid.
/// </summary>