Transform now stores orientation as Quaternions. Interface unchanged

This commit is contained in:
Diren D Bharwani 2022-10-22 20:16:38 +08:00
parent db751bd141
commit ebfcf1c6bb
12 changed files with 209 additions and 301 deletions

View File

@ -104,10 +104,10 @@ namespace Sandbox
transform.SetWorldRotation(SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber()); transform.SetWorldRotation(SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber());
transform.SetWorldScale(TEST_OBJ_SCALE); transform.SetWorldScale(TEST_OBJ_SCALE);
if (const bool IS_EVEN = (y * NUM_ROWS + x) % 2; IS_EVEN) //if (const bool IS_EVEN = (y * NUM_ROWS + x) % 2; IS_EVEN)
collider.AddBoundingBox(SHVec3::One * 0.5f, SHVec3::Zero); collider.AddBoundingBox(SHVec3::One * 0.5f, SHVec3::Zero);
else //else
collider.AddBoundingSphere(0.5f, SHVec3::Zero); // collider.AddBoundingSphere(0.5f, SHVec3::Zero);
stressTestObjects.emplace_back(entity); stressTestObjects.emplace_back(entity);
} }

View File

@ -295,32 +295,33 @@ namespace SHADE
) != 0; ) != 0;
} }
SHMatrix::operator XMMATRIX() const noexcept
{
return XMLoadFloat4x4(this);
}
SHMatrix operator*(float lhs, const SHMatrix& rhs) noexcept SHMatrix operator*(float lhs, const SHMatrix& rhs) noexcept
{ {
return rhs * lhs; return rhs * lhs;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Function Member Definitions */ /* Function Member Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHMatrix::Transpose() noexcept void SHMatrix::Transpose() noexcept
{ {
const XMMATRIX M = XMLoadFloat4x4(this); XMStoreFloat4x4(this, XMMatrixTranspose(*this));
XMStoreFloat4x4(this, XMMatrixTranspose(M));
} }
void SHMatrix::Invert() noexcept void SHMatrix::Invert() noexcept
{ {
const XMMATRIX M = XMLoadFloat4x4(this); XMStoreFloat4x4(this, XMMatrixInverse(nullptr, *this));
XMStoreFloat4x4(this, XMMatrixInverse(nullptr, M));
} }
float SHMatrix::Determinant() const noexcept float SHMatrix::Determinant() const noexcept
{ {
const XMMATRIX M = XMLoadFloat4x4(this); return XMVectorGetX(XMMatrixDeterminant(*this));
return XMVectorGetX(XMMatrixDeterminant(M));
} }
std::string SHMatrix::ToString() const noexcept std::string SHMatrix::ToString() const noexcept
@ -337,9 +338,8 @@ namespace SHADE
bool SHMatrix::Decompose(SHVec3& translation, SHVec3& rotation, SHVec3& scale) const noexcept bool SHMatrix::Decompose(SHVec3& translation, SHVec3& rotation, SHVec3& scale) const noexcept
{ {
XMVECTOR s, r, t; XMVECTOR s, r, t;
const XMMATRIX M = XMLoadFloat4x4(this);
if (!XMMatrixDecompose(&s, &r, &t, M)) if (!XMMatrixDecompose(&s, &r, &t, *this))
return false; return false;
SHQuaternion orientation; SHQuaternion orientation;
@ -356,9 +356,8 @@ namespace SHADE
bool SHMatrix::Decompose(SHVec3& translation, SHQuaternion& orientation, SHVec3& scale) const noexcept bool SHMatrix::Decompose(SHVec3& translation, SHQuaternion& orientation, SHVec3& scale) const noexcept
{ {
XMVECTOR s, r, t; XMVECTOR s, r, t;
const XMMATRIX M = XMLoadFloat4x4(this);
if (!XMMatrixDecompose(&s, &r, &t, M)) if (!XMMatrixDecompose(&s, &r, &t, *this))
return false; return false;
XMStoreFloat3(&scale, s); XMStoreFloat3(&scale, s);
@ -376,8 +375,7 @@ namespace SHADE
{ {
SHMatrix result; SHMatrix result;
const XMMATRIX M = XMLoadFloat4x4(&matrix); XMStoreFloat4x4(&result, XMMatrixTranspose(matrix));
XMStoreFloat4x4(&result, XMMatrixTranspose(M));
return result; return result;
} }
@ -385,8 +383,7 @@ namespace SHADE
{ {
SHMatrix result; SHMatrix result;
const XMMATRIX M = XMLoadFloat4x4(&matrix); XMStoreFloat4x4(&result, XMMatrixInverse(nullptr, matrix));
XMStoreFloat4x4(&result, XMMatrixInverse(nullptr, M));
return result; return result;
} }
@ -401,8 +398,8 @@ namespace SHADE
SHMatrix SHMatrix::Translate(const SHVec3& pos) noexcept SHMatrix SHMatrix::Translate(const SHVec3& pos) noexcept
{ {
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixTranslation(pos.x, pos.y, pos.z));
XMStoreFloat4x4(&result, XMMatrixTranslation(pos.x, pos.y, pos.z));
return result; return result;
} }
@ -410,25 +407,23 @@ namespace SHADE
{ {
SHMatrix result; SHMatrix result;
const XMVECTOR A = XMLoadFloat3(&axis); XMStoreFloat4x4(&result, XMMatrixRotationAxis(axis, angleInRad));
XMStoreFloat4x4(&result, XMMatrixRotationAxis(A, angleInRad));
return result; return result;
} }
SHMatrix SHMatrix::Rotate(float yaw, float pitch, float roll) noexcept SHMatrix SHMatrix::Rotate(float yaw, float pitch, float roll) noexcept
{ {
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixRotationRollPitchYaw(pitch, yaw, roll));
XMStoreFloat4x4(&result, XMMatrixRotationRollPitchYaw(pitch, yaw, roll));
return result; return result;
} }
SHMatrix SHMatrix::Rotate(const SHVec3& eulerAngles) noexcept SHMatrix SHMatrix::Rotate(const SHVec3& eulerAngles) noexcept
{ {
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixRotationRollPitchYaw(eulerAngles.x, eulerAngles.y, eulerAngles.z));
XMStoreFloat4x4(&result, XMMatrixRotationRollPitchYawFromVector(eulerAngles));
return result; return result;
} }
@ -436,57 +431,55 @@ namespace SHADE
{ {
SHMatrix result; SHMatrix result;
const XMVECTOR Q = XMLoadFloat4(&q); XMStoreFloat4x4(&result, XMMatrixRotationQuaternion(q));
XMStoreFloat4x4(&result, XMMatrixRotationQuaternion(Q));
return result; return result;
} }
SHMatrix SHMatrix::RotateX(float angleInRad) noexcept SHMatrix SHMatrix::RotateX(float angleInRad) noexcept
{ {
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixRotationX(angleInRad));
XMStoreFloat4x4(&result, XMMatrixRotationX(angleInRad));
return result; return result;
} }
SHMatrix SHMatrix::RotateY(float angleInRad) noexcept SHMatrix SHMatrix::RotateY(float angleInRad) noexcept
{ {
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixRotationY(angleInRad));
XMStoreFloat4x4(&result, XMMatrixRotationY(angleInRad));
return result; return result;
} }
SHMatrix SHMatrix::RotateZ(float angleInRad) noexcept SHMatrix SHMatrix::RotateZ(float angleInRad) noexcept
{ {
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixRotationZ(angleInRad));
XMStoreFloat4x4(&result, XMMatrixRotationZ(angleInRad));
return result; return result;
} }
SHMatrix SHMatrix::Scale(float uniformScaleFactor) noexcept SHMatrix SHMatrix::Scale(float uniformScaleFactor) noexcept
{ {
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixScaling(uniformScaleFactor, uniformScaleFactor, uniformScaleFactor));
XMStoreFloat4x4(&result, XMMatrixScaling(uniformScaleFactor, uniformScaleFactor, uniformScaleFactor));
return result; return result;
} }
SHMatrix SHMatrix::Scale(float x, float y, float z) noexcept SHMatrix SHMatrix::Scale(float x, float y, float z) noexcept
{ {
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixScaling(x, y, z));
XMStoreFloat4x4(&result, XMMatrixScaling(x, y, z));
return result; return result;
} }
SHMatrix SHMatrix::Scale(const SHVec3& scale) noexcept SHMatrix SHMatrix::Scale(const SHVec3& scale) noexcept
{ {
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixScaling(scale.x, scale.y, scale.z));
XMStoreFloat4x4(&result, XMMatrixScalingFromVector(scale));
return result; return result;
} }
@ -494,12 +487,7 @@ namespace SHADE
{ {
SHMatrix result; SHMatrix result;
const XMVECTOR EYE = XMLoadFloat3(&eye); XMStoreFloat4x4(&result, XMMatrixLookAtRH(eye, target, up));
const XMVECTOR TGT = XMLoadFloat3(&target);
const XMVECTOR UP = XMLoadFloat3(&up);
XMStoreFloat4x4(&result, XMMatrixLookAtRH(EYE, TGT, UP));
return result; return result;
} }
@ -507,12 +495,7 @@ namespace SHADE
{ {
SHMatrix result; SHMatrix result;
const XMVECTOR EYE = XMLoadFloat3(&eye); XMStoreFloat4x4(&result, XMMatrixLookAtLH(eye, target, up));
const XMVECTOR TGT = XMLoadFloat3(&target);
const XMVECTOR UP = XMLoadFloat3(&up);
XMStoreFloat4x4(&result, XMMatrixLookAtLH(EYE, TGT, UP));
return result; return result;
} }
@ -522,8 +505,8 @@ namespace SHADE
const SHVec3 FWD_HAT = SHVec3::Normalise(-forward); const SHVec3 FWD_HAT = SHVec3::Normalise(-forward);
const XMVECTOR Z_HAT = XMVector3Normalize(XMLoadFloat3(&FWD_HAT)); const XMVECTOR Z_HAT = XMVector3Normalize(FWD_HAT);
const XMVECTOR X_HAT = XMVector3Normalize(XMVector3Cross(XMLoadFloat3(&up), Z_HAT)); const XMVECTOR X_HAT = XMVector3Normalize(XMVector3Cross(up, Z_HAT));
const XMVECTOR Y_HAT = XMVector3Cross(Z_HAT, X_HAT); const XMVECTOR Y_HAT = XMVector3Cross(Z_HAT, X_HAT);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&result._11), X_HAT); XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&result._11), X_HAT);
@ -543,8 +526,8 @@ namespace SHADE
const SHVec3 FWD_HAT = SHVec3::Normalise(forward); const SHVec3 FWD_HAT = SHVec3::Normalise(forward);
const XMVECTOR Z_HAT = XMVector3Normalize(XMLoadFloat3(&FWD_HAT)); const XMVECTOR Z_HAT = XMVector3Normalize(FWD_HAT);
const XMVECTOR X_HAT = XMVector3Normalize(XMVector3Cross(XMLoadFloat3(&up), Z_HAT)); const XMVECTOR X_HAT = XMVector3Normalize(XMVector3Cross(up, Z_HAT));
const XMVECTOR Y_HAT = XMVector3Cross(Z_HAT, X_HAT); const XMVECTOR Y_HAT = XMVector3Cross(Z_HAT, X_HAT);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&result._11), X_HAT); XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&result._11), X_HAT);
@ -563,7 +546,6 @@ namespace SHADE
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixPerspectiveFovRH(fov, aspectRatio, nearPlane, farPlane)); XMStoreFloat4x4(&result, XMMatrixPerspectiveFovRH(fov, aspectRatio, nearPlane, farPlane));
return result; return result;
} }
@ -572,7 +554,6 @@ namespace SHADE
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixPerspectiveFovLH(fov, aspectRatio, nearPlane, farPlane)); XMStoreFloat4x4(&result, XMMatrixPerspectiveFovLH(fov, aspectRatio, nearPlane, farPlane));
return result; return result;
} }
@ -581,7 +562,6 @@ namespace SHADE
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixPerspectiveRH(width, height, nearPlane, farPlane)); XMStoreFloat4x4(&result, XMMatrixPerspectiveRH(width, height, nearPlane, farPlane));
return result; return result;
} }
@ -590,7 +570,6 @@ namespace SHADE
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixPerspectiveLH(width, height, nearPlane, farPlane)); XMStoreFloat4x4(&result, XMMatrixPerspectiveLH(width, height, nearPlane, farPlane));
return result; return result;
} }
@ -599,7 +578,6 @@ namespace SHADE
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixOrthographicRH(width, height, nearPlane, farPlane)); XMStoreFloat4x4(&result, XMMatrixOrthographicRH(width, height, nearPlane, farPlane));
return result; return result;
} }
@ -608,7 +586,6 @@ namespace SHADE
SHMatrix result; SHMatrix result;
XMStoreFloat4x4(&result, XMMatrixOrthographicLH(width, height, nearPlane, farPlane)); XMStoreFloat4x4(&result, XMMatrixOrthographicLH(width, height, nearPlane, farPlane));
return result; return result;
} }

View File

@ -77,6 +77,8 @@ namespace SHADE
SHMatrix& operator= (const SHMatrix& rhs) = default; SHMatrix& operator= (const SHMatrix& rhs) = default;
SHMatrix& operator= (SHMatrix&& rhs) = default; SHMatrix& operator= (SHMatrix&& rhs) = default;
operator DirectX::XMMATRIX () const noexcept;
SHMatrix& operator+= (const SHMatrix& rhs) noexcept; SHMatrix& operator+= (const SHMatrix& rhs) noexcept;
SHMatrix& operator-= (const SHMatrix& rhs) noexcept; SHMatrix& operator-= (const SHMatrix& rhs) noexcept;
SHMatrix& operator*= (const SHMatrix& rhs) noexcept; SHMatrix& operator*= (const SHMatrix& rhs) noexcept;

View File

@ -40,40 +40,10 @@ namespace SHADE
: XMFLOAT4( _x, _y, _z, _w ) : XMFLOAT4( _x, _y, _z, _w )
{} {}
SHQuaternion::SHQuaternion(float yaw, float pitch, float roll) noexcept
: XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f )
{
XMStoreFloat4(this, XMQuaternionRotationRollPitchYaw(pitch, yaw, roll));
}
SHQuaternion::SHQuaternion(const SHVec3& eulerAngles) noexcept
: XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f )
{
const XMVECTOR V = XMLoadFloat3(&eulerAngles);
XMStoreFloat4(this, XMQuaternionRotationRollPitchYawFromVector(V));
}
SHQuaternion::SHQuaternion(const SHVec3& axis, float angleInRad) noexcept
: XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f )
{
const XMVECTOR AXIS = XMLoadFloat3(&axis);
XMStoreFloat4(this, XMQuaternionRotationAxis(AXIS, angleInRad));
}
SHQuaternion::SHQuaternion(const SHMatrix& rotationMatrix) noexcept
: XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f )
{
const XMMATRIX M = XMLoadFloat4x4(&rotationMatrix);
XMStoreFloat4(this, XMQuaternionRotationMatrix(M));
}
SHQuaternion::SHQuaternion(const reactphysics3d::Vector3& rp3dEuler) noexcept SHQuaternion::SHQuaternion(const reactphysics3d::Vector3& rp3dEuler) noexcept
: XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f ) : XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f )
{ {
const SHVec3& SHADE_VEC{ rp3dEuler }; XMStoreFloat4(this, XMQuaternionRotationRollPitchYawFromVector(SHVec3 { rp3dEuler }));
const XMVECTOR V = XMLoadFloat3(&SHADE_VEC);
XMStoreFloat4(this, XMQuaternionRotationRollPitchYawFromVector(V));
} }
SHQuaternion::SHQuaternion(const reactphysics3d::Quaternion& rp3dQuat) noexcept SHQuaternion::SHQuaternion(const reactphysics3d::Quaternion& rp3dQuat) noexcept
@ -113,10 +83,7 @@ namespace SHADE
{ {
SHQuaternion result; SHQuaternion result;
const XMVECTOR Q1 = XMLoadFloat4(this); XMStoreFloat4(&result, XMVectorAdd(*this, rhs));
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMVectorAdd(Q1, Q2));
return result; return result;
} }
@ -124,10 +91,7 @@ namespace SHADE
{ {
SHQuaternion result; SHQuaternion result;
const XMVECTOR Q1 = XMLoadFloat4(this); XMStoreFloat4(&result, XMVectorSubtract(*this, rhs));
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMVectorSubtract(Q1, Q2));
return result; return result;
} }
@ -135,9 +99,7 @@ namespace SHADE
{ {
SHQuaternion result; SHQuaternion result;
const XMVECTOR Q = XMLoadFloat4(this); XMStoreFloat4(&result, XMVectorNegate(*this));
XMStoreFloat4(&result, XMVectorNegate(Q));
return result; return result;
} }
@ -145,10 +107,7 @@ namespace SHADE
{ {
SHQuaternion result; SHQuaternion result;
const XMVECTOR Q1 = XMLoadFloat4(this); XMStoreFloat4(&result, XMQuaternionMultiply(*this, rhs));
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMQuaternionMultiply(Q1, Q2));
return result; return result;
} }
@ -156,9 +115,7 @@ namespace SHADE
{ {
SHQuaternion result; SHQuaternion result;
const XMVECTOR Q = XMLoadFloat4(this); XMStoreFloat4(&result, XMVectorScale(*this, rhs));
XMStoreFloat4(&result, XMVectorScale(Q, rhs));
return result; return result;
} }
@ -166,27 +123,18 @@ namespace SHADE
{ {
SHQuaternion result; SHQuaternion result;
const XMVECTOR Q1 = XMLoadFloat4(this); XMStoreFloat4(&result, XMQuaternionMultiply(*this, XMQuaternionInverse(rhs)));
const XMVECTOR Q2 = XMQuaternionInverse(XMLoadFloat4(&rhs));
XMStoreFloat4(&result, XMQuaternionMultiply(Q1, Q2));
return result; return result;
} }
bool SHQuaternion::operator==(const SHQuaternion& rhs) const noexcept bool SHQuaternion::operator==(const SHQuaternion& rhs) const noexcept
{ {
const XMVECTOR Q1 = XMLoadFloat4(this); return XMQuaternionEqual(*this, rhs);
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
return XMQuaternionEqual(Q1, Q2);
} }
bool SHQuaternion::operator!=(const SHQuaternion& rhs) const noexcept bool SHQuaternion::operator!=(const SHQuaternion& rhs) const noexcept
{ {
const XMVECTOR Q1 = XMLoadFloat4(this); return XMQuaternionNotEqual(*this, rhs);
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
return XMQuaternionNotEqual(Q1, Q2);
} }
SHQuaternion::operator reactphysics3d::Quaternion() const noexcept SHQuaternion::operator reactphysics3d::Quaternion() const noexcept
@ -199,6 +147,11 @@ namespace SHADE
return reactphysics3d::Vector3{ ToEuler() }; return reactphysics3d::Vector3{ ToEuler() };
} }
SHQuaternion::operator XMVECTOR() const noexcept
{
return XMLoadFloat4(this);
}
SHQuaternion operator*(float lhs, const SHQuaternion& rhs) noexcept SHQuaternion operator*(float lhs, const SHQuaternion& rhs) noexcept
{ {
return rhs * lhs; return rhs * lhs;
@ -213,8 +166,7 @@ namespace SHADE
XMVECTOR axis; XMVECTOR axis;
float angle; float angle;
const XMVECTOR Q = XMLoadFloat4(this); XMQuaternionToAxisAngle(&axis, &angle, *this);
XMQuaternionToAxisAngle(&axis, &angle, Q);
return angle; return angle;
} }
@ -223,8 +175,7 @@ namespace SHADE
XMVECTOR axis; XMVECTOR axis;
float angle; float angle;
const XMVECTOR Q = XMLoadFloat4(this); XMQuaternionToAxisAngle(&axis, &angle, *this);
XMQuaternionToAxisAngle(&axis, &angle, Q);
return SHVec4{XMVectorGetX(axis), XMVectorGetY(axis), XMVectorGetZ(axis), angle}; return SHVec4{XMVectorGetX(axis), XMVectorGetY(axis), XMVectorGetZ(axis), angle};
@ -238,28 +189,22 @@ namespace SHADE
void SHQuaternion::Invert() noexcept void SHQuaternion::Invert() noexcept
{ {
const XMVECTOR Q = XMLoadFloat4(this); XMStoreFloat4(this, XMQuaternionInverse(*this));
XMStoreFloat4(this, XMQuaternionInverse(Q));
} }
float SHQuaternion::Length() const noexcept float SHQuaternion::Length() const noexcept
{ {
const XMVECTOR Q = XMLoadFloat4(this); return XMVectorGetX(XMQuaternionLength(*this));
return XMVectorGetX(XMQuaternionLength(Q));
} }
float SHQuaternion::LengthSquared() const noexcept float SHQuaternion::LengthSquared() const noexcept
{ {
const XMVECTOR Q = XMLoadFloat4(this); return XMVectorGetX(XMQuaternionLengthSq(*this));
return XMVectorGetX(XMQuaternionLengthSq(Q));
} }
float SHQuaternion::Dot(const SHQuaternion& rhs) const noexcept float SHQuaternion::Dot(const SHQuaternion& rhs) const noexcept
{ {
const XMVECTOR Q1 = XMLoadFloat4(this); return XMVectorGetX(XMQuaternionDot(*this, rhs));
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
return XMVectorGetX(XMQuaternionDot(Q1, Q2));
} }
SHQuaternion SHQuaternion::RotateTowards(const SHQuaternion&, float) const noexcept SHQuaternion SHQuaternion::RotateTowards(const SHQuaternion&, float) const noexcept
@ -273,29 +218,29 @@ namespace SHADE
SHVec3 SHQuaternion::ToEuler() const noexcept SHVec3 SHQuaternion::ToEuler() const noexcept
{ {
const float xx = x * x; const float XX = x * x;
const float yy = y * y; const float YY = y * y;
const float zz = z * z; const float ZZ = z * z;
const float m31 = 2.f * x * z + 2.f * y * w; const float M_31 = 2.f * x * z + 2.f * y * w;
const float m32 = 2.f * y * z - 2.f * x * w; const float M_32 = 2.f * y * z - 2.f * x * w;
const float m33 = 1.f - 2.f * xx - 2.f * yy; const float M_33 = 1.f - 2.f * XX - 2.f * YY;
const float cy = sqrtf(m33 * m33 + m31 * m31); const float CY = sqrtf(M_33 * M_33 + M_31 * M_31);
const float cx = atan2f(-m32, cy); const float CX = atan2f(-M_32, CY);
if (cy > 16.0f * SHMath::EPSILON) if (CY > 16.0f * SHMath::EPSILON)
{ {
const float m12 = 2.f * x * y + 2.f * z * w; const float M_12 = 2.f * x * y + 2.f * z * w;
const float m22 = 1.f - 2.f * xx - 2.f * zz; const float M_22 = 1.f - 2.f * XX - 2.f * ZZ;
return SHVec3(cx, atan2f(m31, m33), atan2f(m12, m22)); return SHVec3(CX, atan2f(M_31, M_33), atan2f(M_12, M_22));
} }
else else
{ {
const float m11 = 1.f - 2.f * yy - 2.f * zz; const float m11 = 1.f - 2.f * YY - 2.f * ZZ;
const float m21 = 2.f * x * y - 2.f * z * w; const float m21 = 2.f * x * y - 2.f * z * w;
return SHVec3(cx, 0.f, atan2f(-m21, m11)); return SHVec3(CX, 0.f, atan2f(-m21, m11));
} }
} }
@ -311,13 +256,43 @@ namespace SHADE
/* Static Function Member Definitions */ /* Static Function Member Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHQuaternion SHQuaternion::FromEuler(const SHVec3& eulerAngles) noexcept
{
SHQuaternion result;
XMStoreFloat4(&result, XMQuaternionRotationRollPitchYawFromVector(eulerAngles));
return result;
}
SHQuaternion SHQuaternion::FromPitchYawRoll(float pitch, float yaw, float roll) noexcept
{
SHQuaternion result;
XMStoreFloat4(&result, XMQuaternionRotationRollPitchYaw(pitch, yaw, roll));
return result;
}
SHQuaternion SHQuaternion::FromAxisAngle(const SHVec3& axis, float angle) noexcept
{
SHQuaternion result;
XMStoreFloat4(&result, XMQuaternionRotationAxis(axis, angle));
return result;
}
SHQuaternion SHQuaternion::FromRotationMatrix(const SHMatrix& rotationMatrix) noexcept
{
SHQuaternion result;
XMStoreFloat4(&result, XMQuaternionRotationMatrix(rotationMatrix));
return result;
}
SHQuaternion SHQuaternion::Normalise(const SHQuaternion& q) noexcept SHQuaternion SHQuaternion::Normalise(const SHQuaternion& q) noexcept
{ {
SHQuaternion result; SHQuaternion result;
const XMVECTOR Q = XMLoadFloat4(&q); XMStoreFloat4(&result, XMQuaternionNormalize(q));
XMStoreFloat4(&result, XMQuaternionNormalize(Q));
return result; return result;
} }
@ -325,9 +300,7 @@ namespace SHADE
{ {
SHQuaternion result; SHQuaternion result;
const XMVECTOR Q = XMLoadFloat4(&q); XMStoreFloat4(&result, XMQuaternionConjugate(q));
XMStoreFloat4(&result, XMQuaternionConjugate(Q));
return result; return result;
} }
@ -335,9 +308,7 @@ namespace SHADE
{ {
SHQuaternion result; SHQuaternion result;
const XMVECTOR Q = XMLoadFloat4(&q); XMStoreFloat4(&result, XMQuaternionInverse(q));
XMStoreFloat4(&result, XMQuaternionInverse(Q));
return result; return result;
} }
@ -362,10 +333,7 @@ namespace SHADE
{ {
SHQuaternion result; SHQuaternion result;
const XMVECTOR Q1 = XMLoadFloat4(&q1); XMStoreFloat4(&result, XMQuaternionSlerp(q1, q2, t));
const XMVECTOR Q2 = XMLoadFloat4(&q2);
XMStoreFloat4(&result, XMQuaternionSlerp(Q1, Q2, t));
return result; return result;
} }

View File

@ -51,9 +51,6 @@ namespace SHADE
SHQuaternion () noexcept; SHQuaternion () noexcept;
SHQuaternion (float x, float y, float z, float w) noexcept; SHQuaternion (float x, float y, float z, float w) noexcept;
SHQuaternion (float yaw, float pitch, float roll) noexcept; SHQuaternion (float yaw, float pitch, float roll) noexcept;
SHQuaternion (const SHVec3& eulerAngles) noexcept;
SHQuaternion (const SHVec3& axis, float angleInRad) noexcept;
SHQuaternion (const SHMatrix& rotationMatrix) noexcept;
// Conversion from other math types // Conversion from other math types
@ -87,6 +84,7 @@ namespace SHADE
operator reactphysics3d::Quaternion () const noexcept; operator reactphysics3d::Quaternion () const noexcept;
operator reactphysics3d::Vector3 () const noexcept; operator reactphysics3d::Vector3 () const noexcept;
operator DirectX::XMVECTOR () const noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Getter Functions */ /* Getter Functions */
@ -113,6 +111,11 @@ namespace SHADE
/* Static Function Members */ /* Static Function Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] static SHQuaternion FromEuler (const SHVec3& eulerAngles) noexcept;
[[nodiscard]] static SHQuaternion FromPitchYawRoll (float pitch, float yaw, float roll) noexcept;
[[nodiscard]] static SHQuaternion FromAxisAngle (const SHVec3& axis, float angle) noexcept;
[[nodiscard]] static SHQuaternion FromRotationMatrix(const SHMatrix& rotationMatrix) noexcept;
[[nodiscard]] static SHQuaternion Normalise (const SHQuaternion& q) noexcept; [[nodiscard]] static SHQuaternion Normalise (const SHQuaternion& q) noexcept;
[[nodiscard]] static SHQuaternion Conjugate (const SHQuaternion& q) noexcept; [[nodiscard]] static SHQuaternion Conjugate (const SHQuaternion& q) noexcept;
[[nodiscard]] static SHQuaternion Inverse (const SHQuaternion& q) noexcept; [[nodiscard]] static SHQuaternion Inverse (const SHQuaternion& q) noexcept;

View File

@ -27,13 +27,13 @@ namespace SHADE
SHTransform::SHTransform() noexcept SHTransform::SHTransform() noexcept
: position { SHVec3::Zero } : position { SHVec3::Zero }
, rotation { SHVec3::Zero } , orientation { SHQuaternion::Identity }
, scale { SHVec3::One } , scale { SHVec3::One }
{} {}
SHTransform::SHTransform(const SHVec3& pos, const SHVec3& rot, const SHVec3& scl) noexcept SHTransform::SHTransform(const SHVec3& pos, const SHVec3& rot, const SHVec3& scl) noexcept
: position { pos } : position { pos }
, rotation { rot } , orientation { SHQuaternion::FromEuler(rot) }
, scale { scl } , scale { scl }
{} {}
@ -43,12 +43,12 @@ namespace SHADE
bool SHTransform::operator==(const SHTransform& rhs) const noexcept bool SHTransform::operator==(const SHTransform& rhs) const noexcept
{ {
return !(position != rhs.position || rotation != rhs.rotation || scale != rhs.scale); return !(position != rhs.position || orientation != rhs.orientation || scale != rhs.scale);
} }
bool SHTransform::operator!=(const SHTransform& rhs) const noexcept bool SHTransform::operator!=(const SHTransform& rhs) const noexcept
{ {
return (position != rhs.position || rotation != rhs.rotation || scale != rhs.scale); return (position != rhs.position || orientation != rhs.orientation || scale != rhs.scale);
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -59,7 +59,7 @@ namespace SHADE
{ {
const SHMatrix T = SHMatrix::Translate(position); const SHMatrix T = SHMatrix::Translate(position);
const SHMatrix R = SHMatrix::Rotate(rotation); const SHMatrix R = SHMatrix::Rotate(orientation);
const SHMatrix S = SHMatrix::Scale(scale); const SHMatrix S = SHMatrix::Scale(scale);
trs = S * R * T; trs = S * R * T;

View File

@ -12,8 +12,8 @@
// Project Headers // Project Headers
#include "SH_API.h" #include "SH_API.h"
#include "Math/Vector/SHVec2.h"
#include "Math/Vector/SHVec3.h" #include "Math/Vector/SHVec3.h"
#include "Math/SHQuaternion.h"
#include "Math/SHMatrix.h" #include "Math/SHMatrix.h"
namespace SHADE namespace SHADE
@ -32,7 +32,7 @@ namespace SHADE
static const SHTransform Identity; static const SHTransform Identity;
SHVec3 position; SHVec3 position;
SHVec3 rotation; SHQuaternion orientation;
SHVec3 scale; SHVec3 scale;
SHMatrix trs; SHMatrix trs;
@ -47,6 +47,7 @@ namespace SHADE
SHTransform () noexcept; SHTransform () noexcept;
SHTransform (const SHVec3& pos, const SHVec3& rot, const SHVec3& scl) noexcept; SHTransform (const SHVec3& pos, const SHVec3& rot, const SHVec3& scl) noexcept;
SHTransform (const SHVec3& pos, const SHQuaternion& quat, const SHVec3& scl) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Operator Overloads */ /* Operator Overloads */
@ -63,7 +64,6 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
const SHMatrix& ComputeTRS(); const SHMatrix& ComputeTRS();
}; };
} // namespace SHADE } // namespace SHADE

View File

@ -42,16 +42,12 @@ namespace SHADE
const SHVec3& SHTransformComponent::GetLocalRotation() const noexcept const SHVec3& SHTransformComponent::GetLocalRotation() const noexcept
{ {
return local.rotation; return localRotation;
} }
SHVec3 SHTransformComponent::GetLocalRotationDeg() const noexcept const SHQuaternion& SHTransformComponent::GetLocalOrientation() const noexcept
{ {
SHVec3 rot = local.rotation; return local.orientation;
rot.x = SHMath::RadiansToDegrees(rot.x);
rot.y = SHMath::RadiansToDegrees(rot.y);
rot.z = SHMath::RadiansToDegrees(rot.z);
return rot;
} }
const SHVec3& SHTransformComponent::GetLocalScale() const noexcept const SHVec3& SHTransformComponent::GetLocalScale() const noexcept
@ -66,16 +62,12 @@ namespace SHADE
const SHVec3& SHTransformComponent::GetWorldRotation() const noexcept const SHVec3& SHTransformComponent::GetWorldRotation() const noexcept
{ {
return world.rotation; return worldRotation;
} }
SHVec3 SHTransformComponent::GetWorldRotationDeg() const noexcept const SHQuaternion& SHTransformComponent::GetWorldOrientation() const noexcept
{ {
SHVec3 rot = world.rotation; return world.orientation;
rot.x = SHMath::RadiansToDegrees(rot.x);
rot.y = SHMath::RadiansToDegrees(rot.y);
rot.z = SHMath::RadiansToDegrees(rot.z);
return rot;
} }
const SHVec3& SHTransformComponent::GetWorldScale() const noexcept const SHVec3& SHTransformComponent::GetWorldScale() const noexcept
@ -111,32 +103,21 @@ namespace SHADE
void SHTransformComponent::SetLocalRotation(const SHVec3& newLocalRotation) noexcept void SHTransformComponent::SetLocalRotation(const SHVec3& newLocalRotation) noexcept
{ {
dirty = true; dirty = true;
local.rotation = newLocalRotation; localRotation = newLocalRotation;
}
void SHTransformComponent::SetLocalRotationDeg(SHVec3 newLocalRotationDeg) noexcept
{
dirty = true;
local.rotation.x = SHMath::DegreesToRadians(newLocalRotationDeg.x);
local.rotation.y = SHMath::DegreesToRadians(newLocalRotationDeg.y);
local.rotation.z = SHMath::DegreesToRadians(newLocalRotationDeg.z);
} }
void SHTransformComponent::SetLocalRotation(float pitch, float yaw, float roll) noexcept void SHTransformComponent::SetLocalRotation(float pitch, float yaw, float roll) noexcept
{ {
dirty = true; dirty = true;
local.rotation.x = pitch; localRotation.x = pitch;
local.rotation.y = yaw; localRotation.y = yaw;
local.rotation.z = roll; localRotation.z = roll;
} }
void SHTransformComponent::SetLocalRotationDeg(float pitch, float yaw, float roll) noexcept void SHTransformComponent::SetLocalOrientation(const SHQuaternion& newLocalOrientation) noexcept
{ {
local.rotation.x = SHMath::DegreesToRadians(pitch);
local.rotation.y = SHMath::DegreesToRadians(yaw);
local.rotation.z = SHMath::DegreesToRadians(roll);
} }
void SHTransformComponent::SetLocalScale(const SHVec3& newLocalScale) noexcept void SHTransformComponent::SetLocalScale(const SHVec3& newLocalScale) noexcept
@ -157,41 +138,24 @@ namespace SHADE
{ {
dirty = true; dirty = true;
world.rotation = newWorldRotation; worldRotation = newWorldRotation;
updateQueue.push({ UpdateCommandType::WORLD_ROTATION, newWorldRotation }); updateQueue.push({ UpdateCommandType::WORLD_ROTATION, newWorldRotation });
} }
void SHTransformComponent::SetWorldRotationDeg(const SHVec3& newWorldRotation) noexcept
{
dirty = true;
world.rotation.x = SHMath::DegreesToRadians(newWorldRotation.x);
world.rotation.y = SHMath::DegreesToRadians(newWorldRotation.y);
world.rotation.z = SHMath::DegreesToRadians(newWorldRotation.z);
updateQueue.push({ UpdateCommandType::WORLD_ROTATION, world.rotation });
}
void SHTransformComponent::SetWorldRotation(float pitch, float yaw, float roll) noexcept void SHTransformComponent::SetWorldRotation(float pitch, float yaw, float roll) noexcept
{ {
dirty = true; dirty = true;
world.rotation.x = pitch; worldRotation.x = pitch;
world.rotation.y = yaw; worldRotation.y = yaw;
world.rotation.z = roll; worldRotation.z = roll;
updateQueue.push({ UpdateCommandType::WORLD_ROTATION, SHVec3{ pitch, yaw, roll} }); updateQueue.push({ UpdateCommandType::WORLD_ROTATION, SHVec3{ pitch, yaw, roll} });
} }
void SHTransformComponent::SetWorldRotationDeg(float pitch, float yaw, float roll) noexcept void SHTransformComponent::SetWorldOrientation(const SHQuaternion& newWorldOrientation) noexcept
{ {
dirty = true;
world.rotation.x = SHMath::DegreesToRadians(pitch);
world.rotation.y = SHMath::DegreesToRadians(yaw);
world.rotation.z = SHMath::DegreesToRadians(roll);
updateQueue.push({ UpdateCommandType::WORLD_ROTATION, world.rotation });
} }
void SHTransformComponent::SetWorldScale(const SHVec3& newWorldScale) noexcept void SHTransformComponent::SetWorldScale(const SHVec3& newWorldScale) noexcept
@ -211,6 +175,6 @@ RTTR_REGISTRATION
registration::class_<SHTransformComponent>("Transform Component") registration::class_<SHTransformComponent>("Transform Component")
.property("Translate" , &SHTransformComponent::GetLocalPosition , &SHTransformComponent::SetLocalPosition ) .property("Translate" , &SHTransformComponent::GetLocalPosition , &SHTransformComponent::SetLocalPosition )
.property("Rotate" , &SHTransformComponent::GetLocalRotationDeg, select_overload<void(SHVec3)>(&SHTransformComponent::SetLocalRotationDeg)) .property("Rotate" , &SHTransformComponent::GetLocalRotation , select_overload<void(const SHVec3&)>(&SHTransformComponent::SetLocalRotation) )
.property("Scale" , &SHTransformComponent::GetLocalScale , &SHTransformComponent::SetLocalScale ); .property("Scale" , &SHTransformComponent::GetLocalScale , &SHTransformComponent::SetLocalScale );
} }

View File

@ -60,11 +60,11 @@ namespace SHADE
[[nodiscard]] const SHVec3& GetLocalPosition () const noexcept; [[nodiscard]] const SHVec3& GetLocalPosition () const noexcept;
[[nodiscard]] const SHVec3& GetLocalRotation () const noexcept; [[nodiscard]] const SHVec3& GetLocalRotation () const noexcept;
[[nodiscard]] SHVec3 GetLocalRotationDeg () const noexcept; [[nodiscard]] const SHQuaternion& GetLocalOrientation () const noexcept;
[[nodiscard]] const SHVec3& GetLocalScale () const noexcept; [[nodiscard]] const SHVec3& GetLocalScale () const noexcept;
[[nodiscard]] const SHVec3& GetWorldPosition () const noexcept; [[nodiscard]] const SHVec3& GetWorldPosition () const noexcept;
[[nodiscard]] const SHVec3& GetWorldRotation () const noexcept; [[nodiscard]] const SHVec3& GetWorldRotation () const noexcept;
[[nodiscard]] SHVec3 GetWorldRotationDeg () const noexcept; [[nodiscard]] const SHQuaternion& GetWorldOrientation () const noexcept;
[[nodiscard]] const SHVec3& GetWorldScale () const noexcept; [[nodiscard]] const SHVec3& GetWorldScale () const noexcept;
[[nodiscard]] const SHMatrix& GetLocalToWorld () const noexcept; [[nodiscard]] const SHMatrix& GetLocalToWorld () const noexcept;
@ -78,15 +78,13 @@ namespace SHADE
void SetLocalPosition (const SHVec3& newLocalPosition) noexcept; void SetLocalPosition (const SHVec3& newLocalPosition) noexcept;
void SetLocalRotation (const SHVec3& newLocalRotation) noexcept; void SetLocalRotation (const SHVec3& newLocalRotation) noexcept;
void SetLocalRotationDeg (SHVec3 newLocalRotationDeg) noexcept;
void SetLocalRotation (float pitch, float yaw, float roll) noexcept; void SetLocalRotation (float pitch, float yaw, float roll) noexcept;
void SetLocalRotationDeg (float pitch, float yaw, float roll) noexcept; void SetLocalOrientation (const SHQuaternion& newLocalOrientation) noexcept;
void SetLocalScale (const SHVec3& newLocalScale) noexcept; void SetLocalScale (const SHVec3& newLocalScale) noexcept;
void SetWorldPosition (const SHVec3& newWorldPosition) noexcept; void SetWorldPosition (const SHVec3& newWorldPosition) noexcept;
void SetWorldRotation (const SHVec3& newWorldRotation) noexcept; void SetWorldRotation (const SHVec3& newWorldRotation) noexcept;
void SetWorldRotationDeg (const SHVec3& newWorldRotation) noexcept;
void SetWorldRotation (float pitch, float yaw, float roll) noexcept; void SetWorldRotation (float pitch, float yaw, float roll) noexcept;
void SetWorldRotationDeg (float pitch, float yaw, float roll) noexcept; void SetWorldOrientation (const SHQuaternion& newWorldOrientation) noexcept;
void SetWorldScale (const SHVec3& newWorldScale) noexcept; void SetWorldScale (const SHVec3& newWorldScale) noexcept;
private: private:
@ -94,10 +92,14 @@ namespace SHADE
/* Type Definitions */ /* Type Definitions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
// Differentiate between rotation and orientation for setters
// Setting a quaternion directly is different from using euler angle rotations.
enum class UpdateCommandType enum class UpdateCommandType
{ {
WORLD_POSITION WORLD_POSITION
, WORLD_ROTATION , WORLD_ROTATION
, WORLD_ORIENTATION
, WORLD_SCALE , WORLD_SCALE
}; };
@ -120,6 +122,12 @@ namespace SHADE
bool dirty; bool dirty;
// We store euler angle rotations separately to interface with transform quaternions.
// Reading quaternions are unreliable.
SHVec3 localRotation; // Stored in degrees
SHVec3 worldRotation; // Stored in degrees
SHTransform local; // Local TRS holds Local To World Transform SHTransform local; // Local TRS holds Local To World Transform
SHTransform world; SHTransform world;

View File

@ -17,6 +17,8 @@
#include "Scene/SHSceneManager.h" #include "Scene/SHSceneManager.h"
#include "ECS_Base/Managers/SHComponentManager.h" #include "ECS_Base/Managers/SHComponentManager.h"
#include "ECS_Base/Managers/SHEntityManager.h" #include "ECS_Base/Managers/SHEntityManager.h"
#include "ECS_Base/Managers/SHSystemManager.h"
#include "Math/SHMathHelpers.h"
namespace SHADE namespace SHADE
{ {
@ -45,14 +47,16 @@ namespace SHADE
{ {
// Get the current scene graph to traverse and update // Get the current scene graph to traverse and update
const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph(); const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph();
UpdateEntity(SCENE_GRAPH.GetRoot());
// TODO(Diren): Consider how to clear dirty in pause / stop mode and update physics, but do not clear in play mode.
UpdateEntity(SCENE_GRAPH.GetRoot(), false);
} }
void SHTransformSystem::TransformPostPhysicsUpdate::Execute(double dt) noexcept void SHTransformSystem::TransformPostPhysicsUpdate::Execute(double) noexcept
{ {
// Get the current scene graph to traverse and update // Get the current scene graph to traverse and update
const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph(); const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph();
UpdateEntityAndClear(SCENE_GRAPH.GetRoot()); UpdateEntity(SCENE_GRAPH.GetRoot(), true);
} }
void SHTransformSystem::Init() void SHTransformSystem::Init()
@ -96,13 +100,13 @@ namespace SHADE
tf->local.position = SHVec3::Transform(tf->world.position, worldToLocal); tf->local.position = SHVec3::Transform(tf->world.position, worldToLocal);
tf->local.rotation = tf->world.rotation; tf->localRotation = tf->worldRotation;
tf->local.scale = tf->world.scale; tf->local.scale = tf->world.scale;
if (PARENT_TF != nullptr) if (PARENT_TF != nullptr)
{ {
// Compute Local Rotation // Compute Local Rotation
tf->local.rotation -= PARENT_TF->GetLocalRotation(); tf->localRotation -= PARENT_TF->GetLocalRotation();
// Compute Local Scale // Compute Local Scale
tf->local.scale /= PARENT_TF->GetLocalScale(); tf->local.scale /= PARENT_TF->GetLocalScale();
@ -143,13 +147,13 @@ namespace SHADE
childTransform->local.position = SHVec3::Transform(childTransform->world.position, worldToLocal); childTransform->local.position = SHVec3::Transform(childTransform->world.position, worldToLocal);
childTransform->local.rotation = childTransform->world.rotation; childTransform->localRotation = childTransform->worldRotation;
childTransform->local.scale = childTransform->world.scale; childTransform->local.scale = childTransform->world.scale;
if (parent) if (parent)
{ {
// Compute Local Rotation // Compute Local Rotation
childTransform->local.rotation -= parent->GetLocalRotation(); childTransform->localRotation -= parent->GetLocalRotation();
// Compute Local Scale // Compute Local Scale
childTransform->local.scale /= parent->GetLocalScale(); childTransform->local.scale /= parent->GetLocalScale();
@ -158,12 +162,10 @@ namespace SHADE
childTransform->local.trs = localToWorld; childTransform->local.trs = localToWorld;
} }
} }
} }
} }
void SHTransformSystem::UpdateEntity(const SHSceneNode* node) void SHTransformSystem::UpdateEntity(const SHSceneNode* node, bool clearDirtyFlag)
{ {
const auto* NODE_TRANSFORM = SHComponentManager::GetComponent_s<SHTransformComponent>(node->GetEntityID()); const auto* NODE_TRANSFORM = SHComponentManager::GetComponent_s<SHTransformComponent>(node->GetEntityID());
const bool HAS_PARENT_CHANGED = NODE_TRANSFORM && NODE_TRANSFORM->dirty; const bool HAS_PARENT_CHANGED = NODE_TRANSFORM && NODE_TRANSFORM->dirty;
@ -185,37 +187,10 @@ namespace SHADE
} }
} }
UpdateEntity(child); UpdateEntity(child, clearDirtyFlag);
}
}
void SHTransformSystem::UpdateEntityAndClear(const SHSceneNode* node)
{
const auto* NODE_TRANSFORM = SHComponentManager::GetComponent_s<SHTransformComponent>(node->GetEntityID());
const bool HAS_PARENT_CHANGED = NODE_TRANSFORM && NODE_TRANSFORM->dirty;
for (const auto* child : node->GetChildren())
{
auto* childTransform = SHComponentManager::GetComponent_s<SHTransformComponent>(child->GetEntityID());
if (childTransform)
{
// Only update if node in hierarchy and component are both active
const bool IS_NODE_ACTIVE = child->IsActive();
if (IS_NODE_ACTIVE && childTransform->isActive)
{
if (childTransform->dirty || HAS_PARENT_CHANGED)
{
UpdateTransform(*childTransform, NODE_TRANSFORM);
childTransform->dirty = true;
}
}
}
UpdateEntityAndClear(child);
// Clear dirty flag after all children are updated // Clear dirty flag after all children are updated
if (childTransform) if (childTransform && clearDirtyFlag)
childTransform->dirty = false; childTransform->dirty = false;
} }
} }
@ -244,12 +219,17 @@ namespace SHADE
} }
case SHTransformComponent::UpdateCommandType::WORLD_ROTATION: case SHTransformComponent::UpdateCommandType::WORLD_ROTATION:
{ {
tf.local.rotation = tf.world.rotation; tf.localRotation = tf.worldRotation;
if (parent) if (parent)
tf.local.rotation -= parent->GetLocalRotation(); tf.localRotation -= parent->GetLocalRotation();
break; break;
} }
case SHTransformComponent::UpdateCommandType::WORLD_ORIENTATION:
{
// TODO(Diren): Test using scripts by concat quaternions?
break;
}
case SHTransformComponent::UpdateCommandType::WORLD_SCALE: case SHTransformComponent::UpdateCommandType::WORLD_SCALE:
{ {
tf.local.scale = tf.world.scale; tf.local.scale = tf.world.scale;
@ -268,7 +248,14 @@ namespace SHADE
tf.local.trs = localToWorld; tf.local.trs = localToWorld;
tf.world.position = SHVec3::Transform(tf.local.position, localToWorld); tf.world.position = SHVec3::Transform(tf.local.position, localToWorld);
tf.world.rotation = tf.local.rotation + (parent ? parent->GetLocalRotation() : SHVec3::Zero);
tf.worldRotation = tf.localRotation + (parent ? parent->GetLocalRotation() : SHVec3::Zero);
// TODO(Diren): Wrap rotations between -360 and 360
tf.world.orientation = SHQuaternion::FromEuler(tf.worldRotation);
tf.local.orientation = SHQuaternion::FromEuler(tf.localRotation);
tf.world.scale = tf.local.scale * (parent ? parent->GetLocalScale() : SHVec3::One); tf.world.scale = tf.local.scale * (parent ? parent->GetLocalScale() : SHVec3::One);
tf.world.ComputeTRS(); tf.world.ComputeTRS();

View File

@ -114,8 +114,7 @@ namespace SHADE
SHEventHandle ChangeParent (SHEventPtr changeParentEvent); SHEventHandle ChangeParent (SHEventPtr changeParentEvent);
static void UpdateChildrenLocalTransforms (SHSceneNode* node); static void UpdateChildrenLocalTransforms (SHSceneNode* node);
static void UpdateEntity (const SHSceneNode* node); static void UpdateEntity (const SHSceneNode* node, bool clearDirtyFlag);
static void UpdateEntityAndClear (const SHSceneNode* node);
static void UpdateTransform (SHTransformComponent& tf, const SHTransformComponent* parent = nullptr); static void UpdateTransform (SHTransformComponent& tf, const SHTransformComponent* parent = nullptr);
}; };

View File

@ -140,12 +140,12 @@ namespace SHADE
isRigidBody = true; isRigidBody = true;
rb->position = tf->GetWorldPosition(); rb->position = tf->GetWorldPosition();
rb->orientation = tf->GetWorldRotation(); rb->orientation = SHQuaternion::FromEuler(tf->GetWorldRotation());
if (hasColliders) if (hasColliders)
{ {
c->position = tf->GetWorldPosition(); c->position = tf->GetWorldPosition();
c->orientation = tf->GetWorldRotation(); c->orientation = SHQuaternion::FromEuler(tf->GetWorldRotation());
// Get array of colliders and add them back into the rigidbody // Get array of colliders and add them back into the rigidbody
for (auto& collider : c->colliders | std::views::keys) for (auto& collider : c->colliders | std::views::keys)
AddCollider(&collider); AddCollider(&collider);
@ -160,7 +160,7 @@ namespace SHADE
hasColliders = true; hasColliders = true;
c->position = tf->GetWorldPosition(); c->position = tf->GetWorldPosition();
c->orientation = tf->GetWorldRotation(); c->orientation = SHQuaternion::FromEuler(tf->GetWorldRotation());
for (auto& collider : c->colliders | std::views::keys) for (auto& collider : c->colliders | std::views::keys)
AddCollider(&collider); AddCollider(&collider);