SP3-16 Quaternions #112

Merged
direnbharwani merged 17 commits from SP3-16-Math into main 2022-10-23 20:07:17 +08:00
12 changed files with 209 additions and 301 deletions
Showing only changes of commit ebfcf1c6bb - Show all commits

View File

@ -104,10 +104,10 @@ namespace Sandbox
transform.SetWorldRotation(SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber());
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);
else
collider.AddBoundingSphere(0.5f, SHVec3::Zero);
//else
// collider.AddBoundingSphere(0.5f, SHVec3::Zero);
stressTestObjects.emplace_back(entity);
}

View File

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

View File

@ -77,6 +77,8 @@ namespace SHADE
SHMatrix& operator= (const 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;

View File

@ -40,40 +40,10 @@ namespace SHADE
: 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
: XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f )
{
const SHVec3& SHADE_VEC{ rp3dEuler };
const XMVECTOR V = XMLoadFloat3(&SHADE_VEC);
XMStoreFloat4(this, XMQuaternionRotationRollPitchYawFromVector(V));
XMStoreFloat4(this, XMQuaternionRotationRollPitchYawFromVector(SHVec3 { rp3dEuler }));
}
SHQuaternion::SHQuaternion(const reactphysics3d::Quaternion& rp3dQuat) noexcept
@ -113,10 +83,7 @@ namespace SHADE
{
SHQuaternion result;
const XMVECTOR Q1 = XMLoadFloat4(this);
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMVectorAdd(Q1, Q2));
XMStoreFloat4(&result, XMVectorAdd(*this, rhs));
return result;
}
@ -124,10 +91,7 @@ namespace SHADE
{
SHQuaternion result;
const XMVECTOR Q1 = XMLoadFloat4(this);
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMVectorSubtract(Q1, Q2));
XMStoreFloat4(&result, XMVectorSubtract(*this, rhs));
return result;
}
@ -135,9 +99,7 @@ namespace SHADE
{
SHQuaternion result;
const XMVECTOR Q = XMLoadFloat4(this);
XMStoreFloat4(&result, XMVectorNegate(Q));
XMStoreFloat4(&result, XMVectorNegate(*this));
return result;
}
@ -145,10 +107,7 @@ namespace SHADE
{
SHQuaternion result;
const XMVECTOR Q1 = XMLoadFloat4(this);
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMQuaternionMultiply(Q1, Q2));
XMStoreFloat4(&result, XMQuaternionMultiply(*this, rhs));
return result;
}
@ -156,9 +115,7 @@ namespace SHADE
{
SHQuaternion result;
const XMVECTOR Q = XMLoadFloat4(this);
XMStoreFloat4(&result, XMVectorScale(Q, rhs));
XMStoreFloat4(&result, XMVectorScale(*this, rhs));
return result;
}
@ -166,27 +123,18 @@ namespace SHADE
{
SHQuaternion result;
const XMVECTOR Q1 = XMLoadFloat4(this);
const XMVECTOR Q2 = XMQuaternionInverse(XMLoadFloat4(&rhs));
XMStoreFloat4(&result, XMQuaternionMultiply(Q1, Q2));
XMStoreFloat4(&result, XMQuaternionMultiply(*this, XMQuaternionInverse(rhs)));
return result;
}
bool SHQuaternion::operator==(const SHQuaternion& rhs) const noexcept
{
const XMVECTOR Q1 = XMLoadFloat4(this);
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
return XMQuaternionEqual(Q1, Q2);
return XMQuaternionEqual(*this, rhs);
}
bool SHQuaternion::operator!=(const SHQuaternion& rhs) const noexcept
{
const XMVECTOR Q1 = XMLoadFloat4(this);
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
return XMQuaternionNotEqual(Q1, Q2);
return XMQuaternionNotEqual(*this, rhs);
}
SHQuaternion::operator reactphysics3d::Quaternion() const noexcept
@ -199,6 +147,11 @@ namespace SHADE
return reactphysics3d::Vector3{ ToEuler() };
}
SHQuaternion::operator XMVECTOR() const noexcept
{
return XMLoadFloat4(this);
}
SHQuaternion operator*(float lhs, const SHQuaternion& rhs) noexcept
{
return rhs * lhs;
@ -213,8 +166,7 @@ namespace SHADE
XMVECTOR axis;
float angle;
const XMVECTOR Q = XMLoadFloat4(this);
XMQuaternionToAxisAngle(&axis, &angle, Q);
XMQuaternionToAxisAngle(&axis, &angle, *this);
return angle;
}
@ -223,8 +175,7 @@ namespace SHADE
XMVECTOR axis;
float angle;
const XMVECTOR Q = XMLoadFloat4(this);
XMQuaternionToAxisAngle(&axis, &angle, Q);
XMQuaternionToAxisAngle(&axis, &angle, *this);
return SHVec4{XMVectorGetX(axis), XMVectorGetY(axis), XMVectorGetZ(axis), angle};
@ -238,28 +189,22 @@ namespace SHADE
void SHQuaternion::Invert() noexcept
{
const XMVECTOR Q = XMLoadFloat4(this);
XMStoreFloat4(this, XMQuaternionInverse(Q));
XMStoreFloat4(this, XMQuaternionInverse(*this));
}
float SHQuaternion::Length() const noexcept
{
const XMVECTOR Q = XMLoadFloat4(this);
return XMVectorGetX(XMQuaternionLength(Q));
return XMVectorGetX(XMQuaternionLength(*this));
}
float SHQuaternion::LengthSquared() const noexcept
{
const XMVECTOR Q = XMLoadFloat4(this);
return XMVectorGetX(XMQuaternionLengthSq(Q));
return XMVectorGetX(XMQuaternionLengthSq(*this));
}
float SHQuaternion::Dot(const SHQuaternion& rhs) const noexcept
{
const XMVECTOR Q1 = XMLoadFloat4(this);
const XMVECTOR Q2 = XMLoadFloat4(&rhs);
return XMVectorGetX(XMQuaternionDot(Q1, Q2));
return XMVectorGetX(XMQuaternionDot(*this, rhs));
}
SHQuaternion SHQuaternion::RotateTowards(const SHQuaternion&, float) const noexcept
@ -273,29 +218,29 @@ namespace SHADE
SHVec3 SHQuaternion::ToEuler() const noexcept
{
const float xx = x * x;
const float yy = y * y;
const float zz = z * z;
const float XX = x * x;
const float YY = y * y;
const float ZZ = z * z;
const float m31 = 2.f * x * z + 2.f * y * w;
const float m32 = 2.f * y * z - 2.f * x * w;
const float m33 = 1.f - 2.f * xx - 2.f * yy;
const float M_31 = 2.f * x * z + 2.f * y * w;
const float M_32 = 2.f * y * z - 2.f * x * w;
const float M_33 = 1.f - 2.f * XX - 2.f * YY;
const float cy = sqrtf(m33 * m33 + m31 * m31);
const float cx = atan2f(-m32, cy);
if (cy > 16.0f * SHMath::EPSILON)
const float CY = sqrtf(M_33 * M_33 + M_31 * M_31);
const float CX = atan2f(-M_32, CY);
if (CY > 16.0f * SHMath::EPSILON)
{
const float m12 = 2.f * x * y + 2.f * z * w;
const float m22 = 1.f - 2.f * xx - 2.f * zz;
const float M_12 = 2.f * x * y + 2.f * z * w;
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
{
const float m11 = 1.f - 2.f * yy - 2.f * zz;
const float m21 = 2.f * x * y - 2.f * z * w;
const float m11 = 1.f - 2.f * YY - 2.f * ZZ;
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 */
/*-----------------------------------------------------------------------------------*/
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 result;
const XMVECTOR Q = XMLoadFloat4(&q);
XMStoreFloat4(&result, XMQuaternionNormalize(Q));
XMStoreFloat4(&result, XMQuaternionNormalize(q));
return result;
}
@ -325,9 +300,7 @@ namespace SHADE
{
SHQuaternion result;
const XMVECTOR Q = XMLoadFloat4(&q);
XMStoreFloat4(&result, XMQuaternionConjugate(Q));
XMStoreFloat4(&result, XMQuaternionConjugate(q));
return result;
}
@ -335,9 +308,7 @@ namespace SHADE
{
SHQuaternion result;
const XMVECTOR Q = XMLoadFloat4(&q);
XMStoreFloat4(&result, XMQuaternionInverse(Q));
XMStoreFloat4(&result, XMQuaternionInverse(q));
return result;
}
@ -362,10 +333,7 @@ namespace SHADE
{
SHQuaternion result;
const XMVECTOR Q1 = XMLoadFloat4(&q1);
const XMVECTOR Q2 = XMLoadFloat4(&q2);
XMStoreFloat4(&result, XMQuaternionSlerp(Q1, Q2, t));
XMStoreFloat4(&result, XMQuaternionSlerp(q1, q2, t));
return result;
}

View File

@ -51,9 +51,6 @@ namespace SHADE
SHQuaternion () noexcept;
SHQuaternion (float x, float y, float z, float w) 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
@ -87,6 +84,7 @@ namespace SHADE
operator reactphysics3d::Quaternion () const noexcept;
operator reactphysics3d::Vector3 () const noexcept;
operator DirectX::XMVECTOR () const noexcept;
/*---------------------------------------------------------------------------------*/
/* Getter Functions */
@ -113,6 +111,11 @@ namespace SHADE
/* 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 Conjugate (const SHQuaternion& q) noexcept;
[[nodiscard]] static SHQuaternion Inverse (const SHQuaternion& q) noexcept;

View File

@ -26,15 +26,15 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/
SHTransform::SHTransform() noexcept
: position { SHVec3::Zero }
, rotation { SHVec3::Zero }
, scale { SHVec3::One }
: position { SHVec3::Zero }
, orientation { SHQuaternion::Identity }
, scale { SHVec3::One }
{}
SHTransform::SHTransform(const SHVec3& pos, const SHVec3& rot, const SHVec3& scl) noexcept
: position { pos }
, rotation { rot }
, scale { scl }
: position { pos }
, orientation { SHQuaternion::FromEuler(rot) }
, scale { scl }
{}
/*-----------------------------------------------------------------------------------*/
@ -43,12 +43,12 @@ namespace SHADE
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
{
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 R = SHMatrix::Rotate(rotation);
const SHMatrix R = SHMatrix::Rotate(orientation);
const SHMatrix S = SHMatrix::Scale(scale);
trs = S * R * T;

View File

@ -12,8 +12,8 @@
// Project Headers
#include "SH_API.h"
#include "Math/Vector/SHVec2.h"
#include "Math/Vector/SHVec3.h"
#include "Math/SHQuaternion.h"
#include "Math/SHMatrix.h"
namespace SHADE
@ -31,22 +31,23 @@ namespace SHADE
static const SHTransform Identity;
SHVec3 position;
SHVec3 rotation;
SHVec3 scale;
SHVec3 position;
SHQuaternion orientation;
SHVec3 scale;
SHMatrix trs;
SHMatrix trs;
/*---------------------------------------------------------------------------------*/
/* Constructors & Destructor */
/*---------------------------------------------------------------------------------*/
SHTransform (const SHTransform&) = default;
SHTransform (SHTransform&&) = default;
~SHTransform () = default;
SHTransform (const SHTransform&) = default;
SHTransform (SHTransform&&) = default;
~SHTransform () = default;
SHTransform () noexcept;
SHTransform (const SHVec3& pos, const SHVec3& rot, const SHVec3& scl) noexcept;
SHTransform () 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 */
@ -63,7 +64,6 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
const SHMatrix& ComputeTRS();
};
} // namespace SHADE

View File

@ -42,16 +42,12 @@ namespace SHADE
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;
rot.x = SHMath::RadiansToDegrees(rot.x);
rot.y = SHMath::RadiansToDegrees(rot.y);
rot.z = SHMath::RadiansToDegrees(rot.z);
return rot;
return local.orientation;
}
const SHVec3& SHTransformComponent::GetLocalScale() const noexcept
@ -66,16 +62,12 @@ namespace SHADE
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;
rot.x = SHMath::RadiansToDegrees(rot.x);
rot.y = SHMath::RadiansToDegrees(rot.y);
rot.z = SHMath::RadiansToDegrees(rot.z);
return rot;
return world.orientation;
}
const SHVec3& SHTransformComponent::GetWorldScale() const noexcept
@ -111,32 +103,21 @@ namespace SHADE
void SHTransformComponent::SetLocalRotation(const SHVec3& newLocalRotation) noexcept
{
dirty = true;
local.rotation = 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);
localRotation = newLocalRotation;
}
void SHTransformComponent::SetLocalRotation(float pitch, float yaw, float roll) noexcept
{
dirty = true;
local.rotation.x = pitch;
local.rotation.y = yaw;
local.rotation.z = roll;
localRotation.x = pitch;
localRotation.y = yaw;
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
@ -157,41 +138,24 @@ namespace SHADE
{
dirty = true;
world.rotation = newWorldRotation;
worldRotation = 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
{
dirty = true;
world.rotation.x = pitch;
world.rotation.y = yaw;
world.rotation.z = roll;
worldRotation.x = pitch;
worldRotation.y = yaw;
worldRotation.z = 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
@ -210,7 +174,7 @@ RTTR_REGISTRATION
using namespace rttr;
registration::class_<SHTransformComponent>("Transform Component")
.property("Translate" , &SHTransformComponent::GetLocalPosition , &SHTransformComponent::SetLocalPosition )
.property("Rotate" , &SHTransformComponent::GetLocalRotationDeg, select_overload<void(SHVec3)>(&SHTransformComponent::SetLocalRotationDeg))
.property("Scale" , &SHTransformComponent::GetLocalScale , &SHTransformComponent::SetLocalScale );
.property("Translate" , &SHTransformComponent::GetLocalPosition , &SHTransformComponent::SetLocalPosition )
.property("Rotate" , &SHTransformComponent::GetLocalRotation , select_overload<void(const SHVec3&)>(&SHTransformComponent::SetLocalRotation) )
.property("Scale" , &SHTransformComponent::GetLocalScale , &SHTransformComponent::SetLocalScale );
}

View File

@ -60,11 +60,11 @@ namespace SHADE
[[nodiscard]] const SHVec3& GetLocalPosition () 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& GetWorldPosition () 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 SHMatrix& GetLocalToWorld () const noexcept;
@ -76,28 +76,30 @@ namespace SHADE
/* Setter Functions */
/*---------------------------------------------------------------------------------*/
void SetLocalPosition (const SHVec3& newLocalPosition) noexcept;
void SetLocalRotation (const SHVec3& newLocalRotation) noexcept;
void SetLocalRotationDeg (SHVec3 newLocalRotationDeg) noexcept;
void SetLocalRotation (float pitch, float yaw, float roll) noexcept;
void SetLocalRotationDeg (float pitch, float yaw, float roll) noexcept;
void SetLocalScale (const SHVec3& newLocalScale) noexcept;
void SetWorldPosition (const SHVec3& newWorldPosition) noexcept;
void SetWorldRotation (const SHVec3& newWorldRotation) noexcept;
void SetWorldRotationDeg (const SHVec3& newWorldRotation) noexcept;
void SetWorldRotation (float pitch, float yaw, float roll) noexcept;
void SetWorldRotationDeg (float pitch, float yaw, float roll) noexcept;
void SetWorldScale (const SHVec3& newWorldScale) noexcept;
void SetLocalPosition (const SHVec3& newLocalPosition) noexcept;
void SetLocalRotation (const SHVec3& newLocalRotation) noexcept;
void SetLocalRotation (float pitch, float yaw, float roll) noexcept;
void SetLocalOrientation (const SHQuaternion& newLocalOrientation) noexcept;
void SetLocalScale (const SHVec3& newLocalScale) noexcept;
void SetWorldPosition (const SHVec3& newWorldPosition) noexcept;
void SetWorldRotation (const SHVec3& newWorldRotation) noexcept;
void SetWorldRotation (float pitch, float yaw, float roll) noexcept;
void SetWorldOrientation (const SHQuaternion& newWorldOrientation) noexcept;
void SetWorldScale (const SHVec3& newWorldScale) noexcept;
private:
/*---------------------------------------------------------------------------------*/
/* Type Definitions */
/*---------------------------------------------------------------------------------*/
// Differentiate between rotation and orientation for setters
// Setting a quaternion directly is different from using euler angle rotations.
enum class UpdateCommandType
{
WORLD_POSITION
, WORLD_ROTATION
, WORLD_ORIENTATION
, WORLD_SCALE
};
@ -120,6 +122,12 @@ namespace SHADE
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 world;

View File

@ -17,6 +17,8 @@
#include "Scene/SHSceneManager.h"
#include "ECS_Base/Managers/SHComponentManager.h"
#include "ECS_Base/Managers/SHEntityManager.h"
#include "ECS_Base/Managers/SHSystemManager.h"
#include "Math/SHMathHelpers.h"
namespace SHADE
{
@ -45,14 +47,16 @@ namespace SHADE
{
// Get the current scene graph to traverse and update
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
const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph();
UpdateEntityAndClear(SCENE_GRAPH.GetRoot());
UpdateEntity(SCENE_GRAPH.GetRoot(), true);
}
void SHTransformSystem::Init()
@ -96,13 +100,13 @@ namespace SHADE
tf->local.position = SHVec3::Transform(tf->world.position, worldToLocal);
tf->local.rotation = tf->world.rotation;
tf->local.scale = tf->world.scale;
tf->localRotation = tf->worldRotation;
tf->local.scale = tf->world.scale;
if (PARENT_TF != nullptr)
{
// Compute Local Rotation
tf->local.rotation -= PARENT_TF->GetLocalRotation();
tf->localRotation -= PARENT_TF->GetLocalRotation();
// Compute Local Scale
tf->local.scale /= PARENT_TF->GetLocalScale();
@ -143,13 +147,13 @@ namespace SHADE
childTransform->local.position = SHVec3::Transform(childTransform->world.position, worldToLocal);
childTransform->local.rotation = childTransform->world.rotation;
childTransform->local.scale = childTransform->world.scale;
childTransform->localRotation = childTransform->worldRotation;
childTransform->local.scale = childTransform->world.scale;
if (parent)
{
// Compute Local Rotation
childTransform->local.rotation -= parent->GetLocalRotation();
childTransform->localRotation -= parent->GetLocalRotation();
// Compute Local Scale
childTransform->local.scale /= parent->GetLocalScale();
@ -158,12 +162,10 @@ namespace SHADE
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 bool HAS_PARENT_CHANGED = NODE_TRANSFORM && NODE_TRANSFORM->dirty;
@ -185,37 +187,10 @@ namespace SHADE
}
}
UpdateEntity(child);
}
}
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);
UpdateEntity(child, clearDirtyFlag);
// Clear dirty flag after all children are updated
if (childTransform)
if (childTransform && clearDirtyFlag)
childTransform->dirty = false;
}
}
@ -244,12 +219,17 @@ namespace SHADE
}
case SHTransformComponent::UpdateCommandType::WORLD_ROTATION:
{
tf.local.rotation = tf.world.rotation;
tf.localRotation = tf.worldRotation;
if (parent)
tf.local.rotation -= parent->GetLocalRotation();
tf.localRotation -= parent->GetLocalRotation();
break;
}
case SHTransformComponent::UpdateCommandType::WORLD_ORIENTATION:
{
// TODO(Diren): Test using scripts by concat quaternions?
break;
}
case SHTransformComponent::UpdateCommandType::WORLD_SCALE:
{
tf.local.scale = tf.world.scale;
@ -268,7 +248,14 @@ namespace SHADE
tf.local.trs = 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.ComputeTRS();

View File

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

View File

@ -140,12 +140,12 @@ namespace SHADE
isRigidBody = true;
rb->position = tf->GetWorldPosition();
rb->orientation = tf->GetWorldRotation();
rb->orientation = SHQuaternion::FromEuler(tf->GetWorldRotation());
if (hasColliders)
{
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
for (auto& collider : c->colliders | std::views::keys)
AddCollider(&collider);
@ -160,7 +160,7 @@ namespace SHADE
hasColliders = true;
c->position = tf->GetWorldPosition();
c->orientation = tf->GetWorldRotation();
c->orientation = SHQuaternion::FromEuler(tf->GetWorldRotation());
for (auto& collider : c->colliders | std::views::keys)
AddCollider(&collider);