From 97ffe2efb559d5540c4d198a07deb504d29846af Mon Sep 17 00:00:00 2001 From: Lightling Date: Sun, 13 Feb 2022 12:46:26 -0500 Subject: [PATCH] right/up/forward vectors --- Transform.cpp | 24 ++++++++++++++++++++++++ Transform.h | 7 +++++++ 2 files changed, 31 insertions(+) diff --git a/Transform.cpp b/Transform.cpp index ca04871..2281e0a 100644 --- a/Transform.cpp +++ b/Transform.cpp @@ -38,6 +38,21 @@ DirectX::XMFLOAT4X4 Transform::GetWorldMatrixInverseTranspose() return worldMatrixInverseTranspose; } +DirectX::XMFLOAT3 Transform::GetRight() +{ + return right; +} + +DirectX::XMFLOAT3 Transform::GetUp() +{ + return up; +} + +DirectX::XMFLOAT3 Transform::GetForward() +{ + return forward; +} + // XMVECTOR & XMStoreFloat compiles down to something faster than position += x,y,z because it happens all at once void Transform::SetPosition(float _x, float _y, float _z) @@ -52,6 +67,7 @@ void Transform::SetRotation(float _pitch, float _yaw, float _roll) XMVECTOR newRotation = XMVectorSet(_pitch, _yaw, _roll, 0); XMStoreFloat3(&eulerAngles, newRotation); worldMatrixChanged = true; + UpdateDirections(); } void Transform::SetScale(float _x, float _y, float _z) @@ -84,6 +100,7 @@ void Transform::Rotate(float _pitch, float _yaw, float _roll) XMVECTOR offset = XMVectorSet(_pitch, _yaw, _roll, 0); XMStoreFloat3(&eulerAngles, newRotation + offset); worldMatrixChanged = true; + UpdateDirections(); } void Transform::Scale(float _x, float _y, float _z) @@ -103,3 +120,10 @@ void Transform::UpdateWorldMatrix() XMStoreFloat4x4(&worldMatrix, matrixScale * matrixRotation * matrixPosition); XMStoreFloat4x4(&worldMatrixInverseTranspose, XMMatrixInverse(0, XMMatrixTranspose(XMLoadFloat4x4(&worldMatrix)))); } + +void Transform::UpdateDirections() +{ + XMStoreFloat3(&right, XMVector3Rotate(XMVectorSet(1, 0, 0, 0), XMQuaternionRotationRollPitchYaw(eulerAngles.x, eulerAngles.y, eulerAngles.z))); + XMStoreFloat3(&up, XMVector3Rotate(XMVectorSet(0, 1, 0, 0), XMQuaternionRotationRollPitchYaw(eulerAngles.x, eulerAngles.y, eulerAngles.z))); + XMStoreFloat3(&forward, XMVector3Rotate(XMVectorSet(0, 0, 1, 0), XMQuaternionRotationRollPitchYaw(eulerAngles.x, eulerAngles.y, eulerAngles.z))); +} diff --git a/Transform.h b/Transform.h index 25e47ff..2039dad 100644 --- a/Transform.h +++ b/Transform.h @@ -13,6 +13,9 @@ public: DirectX::XMFLOAT3 GetScale(); DirectX::XMFLOAT4X4 GetWorldMatrix(); DirectX::XMFLOAT4X4 GetWorldMatrixInverseTranspose(); + DirectX::XMFLOAT3 GetRight(); + DirectX::XMFLOAT3 GetUp(); + DirectX::XMFLOAT3 GetForward(); void SetPosition(float _x, float _y, float _z); void SetRotation(float _pitch, float _yaw, float _roll); @@ -30,6 +33,10 @@ private: DirectX::XMFLOAT4X4 worldMatrix; DirectX::XMFLOAT4X4 worldMatrixInverseTranspose; bool worldMatrixChanged; + DirectX::XMFLOAT3 right; + DirectX::XMFLOAT3 up; + DirectX::XMFLOAT3 forward; void UpdateWorldMatrix(); + void UpdateDirections(); };