Changed Rotation from Euler to Quaternion (TransformComponent)

This commit is contained in:
Simon Lübeß
2021-09-03 16:44:50 +02:00
parent d555a80d8d
commit ff4cd48f1e
3 changed files with 72 additions and 18 deletions
+44 -5
View File
@@ -5,13 +5,13 @@ namespace GlitchyEngine.World
public struct TransformComponent
{
Vector3 _position = .Zero;
Vector3 _rotation = .Zero;
Quaternion _rotation = .Identity;
Vector3 _scale = .One;
Matrix _localTransform;
public bool IsDirty;
Matrix _localTransform = .Identity;
public bool IsDirty = false;;
public Matrix WorldTransform;
public Matrix WorldTransform = .Identity;
/// The frame when the transform was recalculated
public uint Frame;
@@ -35,7 +35,8 @@ namespace GlitchyEngine.World
}
}
public Vector3 Rotation
/// Gets or sets the rotation.
public Quaternion Rotation
{
get => _rotation;
set mut
@@ -47,6 +48,44 @@ namespace GlitchyEngine.World
IsDirty = true;
}
}
/**
* Gets or sets the rotation using euler angles.
* @Note The rotations will be applied in the following order: YZX (the order in the vector is still XYZ!)
*/
public Vector3 RotationEuler
{
get => Quaternion.ToEulerAngles(_rotation);
set mut
{
Quaternion quat = Quaternion.FromEulerAngles(value.Y, value.X, value.Z);
if(_rotation == quat)
return;
_rotation = quat;
IsDirty = true;
}
}
/**
* Gets or sets the rotation using axis angle, where Axis is the axis around which will be rotated and angle is the angle
* that was rotated around the axis in radians.
*/
public (Vector3 Axis, float Angle) RotationAxisAngle
{
get => _rotation.ToAxisAngle();
set mut
{
Quaternion quat = Quaternion.FromAxisAngle(value.Axis, value.Angle);
if(_rotation == quat)
return;
_rotation = quat;
IsDirty = true;
}
}
public Vector3 Scale
{
+2 -3
View File
@@ -16,7 +16,7 @@ namespace GlitchyEngine.World
private static void UpdateEntity(Entity entity, TransformComponent* transform, EcsWorld world)
{
// Todo: this probably needs a rewrite as it may scale poorly with deep hierarchies!
// Todo: test scaling with deep hierarchies!
// transform was updated this frame -> skip
if(transform.Frame == _frame)
@@ -28,8 +28,7 @@ namespace GlitchyEngine.World
if(transform.IsDirty)
{
transform.LocalTransform = .Translation(transform.Position) * .RotationX(transform.Rotation.X) *
.RotationY(transform.Rotation.Y) * .RotationZ(transform.Rotation.Z) * .Scaling(transform.Scale);
transform.LocalTransform = .Translation(transform.Position) * .RotationQuaternion(transform.Rotation) * .Scaling(transform.Scale);
transform.IsDirty = false;