From c3582cf5eed1bcc68c50268024ed1209e8f203f5 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 7 Jan 2023 16:14:55 +0800 Subject: [PATCH 1/2] Added a rotate method with quaternions for Vector3 --- SHADE_Engine/src/Math/Transform/SHTransform.cpp | 12 +++++++++++- SHADE_Managed/src/Math/Vector3.cxx | 4 ++++ SHADE_Managed/src/Math/Vector3.hxx | 17 +++++++++++++++-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/SHADE_Engine/src/Math/Transform/SHTransform.cpp b/SHADE_Engine/src/Math/Transform/SHTransform.cpp index ef7c5fda..7be2a60c 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransform.cpp +++ b/SHADE_Engine/src/Math/Transform/SHTransform.cpp @@ -35,7 +35,17 @@ namespace SHADE : position { pos } , orientation { SHQuaternion::FromEuler(rot) } , scale { scl } - {} + { + ComputeTRS(); + } + + SHTransform::SHTransform(const SHVec3& pos, const SHQuaternion& quat, const SHVec3& scl) noexcept + : position { pos } + , orientation { quat } + , scale { scl } + { + ComputeTRS(); + } /*-----------------------------------------------------------------------------------*/ /* Operator Overload Definitions */ diff --git a/SHADE_Managed/src/Math/Vector3.cxx b/SHADE_Managed/src/Math/Vector3.cxx index d2862ac7..647174a2 100644 --- a/SHADE_Managed/src/Math/Vector3.cxx +++ b/SHADE_Managed/src/Math/Vector3.cxx @@ -167,6 +167,10 @@ namespace SHADE { return Convert::ToCLI(SHVec3::Rotate(Convert::ToNative(vec), Convert::ToNative(axis), radians)); } + Vector3 Vector3::Rotate(Vector3 vec, Quaternion quat) + { + return Convert::ToCLI(SHVec3::Rotate(Convert::ToNative(vec), Convert::ToNative(quat))); + } Vector3 Vector3::Min(Vector3 lhs, Vector3 rhs) { float lx = lhs.x, rx = rhs.x; diff --git a/SHADE_Managed/src/Math/Vector3.hxx b/SHADE_Managed/src/Math/Vector3.hxx index 76b11420..31a373ba 100644 --- a/SHADE_Managed/src/Math/Vector3.hxx +++ b/SHADE_Managed/src/Math/Vector3.hxx @@ -19,10 +19,17 @@ of DigiPen Institute of Technology is prohibited. // Project Includes #include "Vector2.hxx" -value struct Quaternion; - namespace SHADE { + /*---------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*-------------------------------------------------------------------------- --- */ + value struct Quaternion; + + /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-------------------------------------------------------------------------- --- */ + /// /// CLR version of SHADE Engine's Vector3 class that represents a 3-Dimensional Vector. /// Designed to closely match Unity's Vector3 struct. @@ -308,6 +315,12 @@ namespace SHADE /// The Vector3 that represents the rotated vector. static Vector3 Rotate(Vector3 vec, Vector3 axis, float radians); /// + /// Rotates a Vector3 using a Quaternion. + /// + /// A Vector3 to rotate. + /// A Quaternion to rotate the vector with. + static Vector3 Rotate(Vector3 vec, Quaternion quat); + /// /// Computes and returns a Vector3 that is made from the smallest components of /// the two specified Vector3s. /// -- 2.40.1 From 222bda9a13436cfc71a0fbd8f7043fb5ee4ec686 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 7 Jan 2023 16:16:35 +0800 Subject: [PATCH 2/2] Replaced Transform's Forward.get with new rotate method --- SHADE_Managed/src/Components/Transform.cxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/SHADE_Managed/src/Components/Transform.cxx b/SHADE_Managed/src/Components/Transform.cxx index bc61eff3..d5b38967 100644 --- a/SHADE_Managed/src/Components/Transform.cxx +++ b/SHADE_Managed/src/Components/Transform.cxx @@ -107,8 +107,7 @@ namespace SHADE Vector3 Transform::Forward::get() { - const SHVec3 DIRECTION = SHVec3::Rotate(-SHVec3::UnitZ, Convert::ToNative(GlobalRotation)); - return Convert::ToCLI(DIRECTION); + return Vector3::Rotate(Vector3::Forward, GlobalRotation); } /*-----------------------------------------------------------------------------------*/ -- 2.40.1