transform

This commit is contained in:
2024-09-09 11:51:57 +02:00
parent 3711a8bae0
commit f85b425ec9
3 changed files with 82 additions and 0 deletions

View File

@@ -5,4 +5,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="TransformD.cs" />
</ItemGroup>
<ItemGroup>
<None Include="TransformD.cs" />
</ItemGroup>
</Project>

63
PveTeam.Math/Transform.cs Normal file
View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
namespace PveTeam.Mathematics
{
public class Transform
{
private Matrix4x4 _backingMatrix;
private Vector3 _position;
private Quaternion _rotation;
private Vector3 _scale;
public Vector3 Position
{
get => _position;
set
{
_position = value;
RecalcBackingMatrix();
}
}
public Quaternion Rotation
{
get => _rotation;
set
{
_rotation = value;
RecalcBackingMatrix();
}
}
public Vector3 Scale
{
get => _scale;
set
{
_scale = value;
RecalcBackingMatrix();
}
}
private void RecalcBackingMatrix()
=> _backingMatrix = Matrix4x4.CreateTranslation(_position) * Matrix4x4.CreateFromQuaternion(_rotation) * Matrix4x4.CreateScale(_scale);
public static implicit operator Matrix4x4(Transform transform)
=> transform._backingMatrix;
public static explicit operator Transform(Matrix4x4 matrix)
{
return new Transform
{
_backingMatrix = matrix,
_position = new Vector3(matrix.M41, matrix.M42, matrix.M43),
_rotation = Quaternion.CreateFromRotationMatrix(matrix),
_scale = new Vector3(matrix.M11, matrix.M22, matrix.M33)
};
}
}
}

View File

@@ -0,0 +1,11 @@
namespace PveTeam.Mathematics
{
public class TransformD
{
private Matrix4x4D _backingMatrix;
private Vector3D _position;
private QuaternionD _rotation;
private Vector3D _scale;
}
}