From 1049333cfaabb2172cb5569471a89daf54e4eec6 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sun, 9 Jul 2023 21:40:00 -0400 Subject: [PATCH 1/3] Added rotation field to player and pass them to jolt --- Nuake/src/Core/Physics/CharacterController.h | 5 ++--- Nuake/src/Scene/Systems/PhysicsSystem.cpp | 6 ++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Nuake/src/Core/Physics/CharacterController.h b/Nuake/src/Core/Physics/CharacterController.h index b76097b9..9e20bb6b 100644 --- a/Nuake/src/Core/Physics/CharacterController.h +++ b/Nuake/src/Core/Physics/CharacterController.h @@ -13,8 +13,9 @@ namespace Nuake class CharacterController { public: - Vector3 Position = Vector3(0, 0, 0); + Quat Rotation = Quat(1, 0, 0, 0); + Entity Owner; bool IsOnGround = false; @@ -25,8 +26,6 @@ namespace Nuake float Friction = 0.5f; Ref Shape; - //bool m_onJumpableGround; // A bit lower contact than just onGround - float m_bottomYOffset; float m_bottomRoundedRegionYOffset; diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.cpp b/Nuake/src/Scene/Systems/PhysicsSystem.cpp index 1a693bc4..b7b94ef5 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.cpp +++ b/Nuake/src/Scene/Systems/PhysicsSystem.cpp @@ -286,10 +286,12 @@ namespace Nuake const auto& capsuleColliderComponent = entity.GetComponent(); auto& capsule = capsuleColliderComponent.Capsule; - float friction = characterControllerComponent.Friction; - float maxSlopeAngle = characterControllerComponent.MaxSlopeAngle; + const float friction = characterControllerComponent.Friction; + const float maxSlopeAngle = characterControllerComponent.MaxSlopeAngle; auto characterController = CreateRef(capsule, friction, maxSlopeAngle); characterController->SetEntity(entity); // Used to link back to the entity + characterController->Position = transformComponent.GetGlobalPosition(); + characterController->Rotation = transformComponent.GetGlobalRotation(); characterControllerComponent.CharacterController = characterController; PhysicsManager::Get().RegisterCharacterController(characterControllerComponent.CharacterController); From f66865c0ece1fd3308ba3369a53eed664a2c3868 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sun, 9 Jul 2023 23:44:50 -0400 Subject: [PATCH 2/3] Fixed wrong camera rotation when global rotation is set --- Nuake/src/Core/Maths.cpp | 7 +------ Nuake/src/Core/Maths.h | 2 +- Nuake/src/Scene/Scene.cpp | 2 +- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/Nuake/src/Core/Maths.cpp b/Nuake/src/Core/Maths.cpp index 966018fc..22a9df50 100644 --- a/Nuake/src/Core/Maths.cpp +++ b/Nuake/src/Core/Maths.cpp @@ -4,10 +4,5 @@ using namespace Nuake; Quat Nuake::QuatFromEuler(float x, float y, float z) { - // Taken from: https://gamedev.stackexchange.com/questions/13436/glm-euler-angles-to-quaternion - Quat QuatAroundX = glm::angleAxis(glm::radians(x), Vector3(1.0, 0.0, 0.0)); - Quat QuatAroundY = glm::angleAxis(glm::radians(y), Vector3(0.0, 1.0, 0.0)); - Quat QuatAroundZ = glm::angleAxis(glm::radians(z), Vector3(0.0, 0.0, 1.0)); - - return QuatAroundZ * QuatAroundX * QuatAroundY; + return Quat(Vector3(Rad(z), Rad(y), Rad(x))); } diff --git a/Nuake/src/Core/Maths.h b/Nuake/src/Core/Maths.h index 140ae529..448633cf 100644 --- a/Nuake/src/Core/Maths.h +++ b/Nuake/src/Core/Maths.h @@ -17,6 +17,6 @@ namespace Nuake using Color = glm::vec4; using Matrix4 = glm::mat4; using Matrix3 = glm::mat3; - +#define Rad(degrees) glm::radians(degrees) Quat QuatFromEuler(float x, float y, float z); } diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 21d1062b..05c60bfe 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -142,7 +142,7 @@ namespace Nuake { auto [transform, camera, parent] = view.get(e); cam = camera.CameraInstance; cam->Translation = transform.GetGlobalPosition(); - cam->SetDirection(Vector3(Vector4(0, 0, -1, 0) * transform.GetGlobalTransform())); + cam->SetDirection(glm::rotate(glm::inverse(transform.GetGlobalRotation()), glm::vec3(1.0, 0.0, 0.0))); camTransform = transform.GetGlobalTransform(); break; From 2a6601a2dbcfccc31879cbbb7ca492872c5a7b30 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sun, 9 Jul 2023 23:51:32 -0400 Subject: [PATCH 3/3] Moved quat to forward vector in math helper class --- Nuake/src/Core/Maths.cpp | 5 +++++ Nuake/src/Core/Maths.h | 2 ++ Nuake/src/Scene/Scene.cpp | 10 +++++++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Nuake/src/Core/Maths.cpp b/Nuake/src/Core/Maths.cpp index 22a9df50..e0889bce 100644 --- a/Nuake/src/Core/Maths.cpp +++ b/Nuake/src/Core/Maths.cpp @@ -6,3 +6,8 @@ Quat Nuake::QuatFromEuler(float x, float y, float z) { return Quat(Vector3(Rad(z), Rad(y), Rad(x))); } + +Vector3 Nuake::QuatToDirection(const Quat& quat) +{ + return glm::rotate(glm::inverse(quat), glm::vec3(1.0, 0.0, 0.0)); +} \ No newline at end of file diff --git a/Nuake/src/Core/Maths.h b/Nuake/src/Core/Maths.h index 448633cf..6612d894 100644 --- a/Nuake/src/Core/Maths.h +++ b/Nuake/src/Core/Maths.h @@ -18,5 +18,7 @@ namespace Nuake using Matrix4 = glm::mat4; using Matrix3 = glm::mat3; #define Rad(degrees) glm::radians(degrees) + Quat QuatFromEuler(float x, float y, float z); + Vector3 QuatToDirection(const Quat& quat); } diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 05c60bfe..4172cbcf 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -16,6 +16,7 @@ #include #include "Engine.h" +#include "src/Core/Maths.h" #include "src/Core/FileSystem.h" #include "src/Scene/Components/Components.h" #include "src/Scene/Components/BoxCollider.h" @@ -141,10 +142,13 @@ namespace Nuake { { auto [transform, camera, parent] = view.get(e); cam = camera.CameraInstance; - cam->Translation = transform.GetGlobalPosition(); - cam->SetDirection(glm::rotate(glm::inverse(transform.GetGlobalRotation()), glm::vec3(1.0, 0.0, 0.0))); - camTransform = transform.GetGlobalTransform(); + cam->Translation = transform.GetGlobalPosition(); + + const Vector3& forward = QuatToDirection(transform.GetGlobalRotation()); + cam->SetDirection(forward); + + camTransform = transform.GetGlobalTransform(); break; } }