Add GetRotation for matrices and better precision (#4691)

This commit is contained in:
metalgearsloth
2023-12-11 18:40:57 +11:00
committed by GitHub
parent 58ac82ae55
commit f24d9751d4
2 changed files with 34 additions and 8 deletions

View File

@@ -30,6 +30,7 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using JetBrains.Annotations;
using Robust.Shared.Utility;
namespace Robust.Shared.Maths
@@ -385,7 +386,7 @@ namespace Robust.Shared.Maths
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3 CreateRotation(Angle angle)
public static Matrix3 CreateRotation(double angle)
{
var cos = (float) Math.Cos(angle);
var sin = (float) Math.Sin(angle);
@@ -426,12 +427,12 @@ namespace Robust.Shared.Maths
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3 CreateTransform(float posX, float posY, float angle, float scaleX = 1, float scaleY = 1)
public static Matrix3 CreateTransform(float posX, float posY, double angle, float scaleX = 1, float scaleY = 1)
{
// returns a matrix that is equivalent to returning CreateScale(scale) * CreateRotation(angle) * CreateTranslation(posX, posY)
var sin = MathF.Sin(angle);
var cos = MathF.Cos(angle);
var sin = (float) Math.Sin(angle);
var cos = (float) Math.Cos(angle);
return new Matrix3
{
@@ -446,12 +447,12 @@ namespace Robust.Shared.Maths
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3 CreateInverseTransform(float posX, float posY, float angle, float scaleX = 1, float scaleY = 1)
public static Matrix3 CreateInverseTransform(float posX, float posY, double angle, float scaleX = 1, float scaleY = 1)
{
// returns a matrix that is equivalent to returning CreateTranslation(-posX, -posY) * CreateRotation(-angle) * CreateScale(1/scaleX, 1/scaleY)
var sin = MathF.Sin(angle);
var cos = MathF.Cos(angle);
var sin = (float) Math.Sin(angle);
var cos = (float) Math.Cos(angle);
return new Matrix3
{
@@ -493,7 +494,17 @@ namespace Robust.Shared.Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix3 CreateInverseTransform(in Vector2 position, in Angle angle, in Vector2 scale)
{
return CreateInverseTransform(position.X, position.Y, (float)angle.Theta, scale.X, scale.Y);
return CreateInverseTransform(position.X, position.Y, angle.Theta, scale.X, scale.Y);
}
/// <summary>
/// Gets the rotation of the Matrix. Will have some precision loss.
/// </summary>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Angle Rotation()
{
return new Vector2(R0C0, R1C0).ToAngle();
}
#endregion Constructors