diff --git a/Nuake/src/Core/Maths.cpp b/Nuake/src/Core/Maths.cpp index 966018fc..e0889bce 100644 --- a/Nuake/src/Core/Maths.cpp +++ b/Nuake/src/Core/Maths.cpp @@ -4,10 +4,10 @@ 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))); } + +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 140ae529..6612d894 100644 --- a/Nuake/src/Core/Maths.h +++ b/Nuake/src/Core/Maths.h @@ -17,6 +17,8 @@ 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); + Vector3 QuatToDirection(const Quat& quat); } 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/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 21d1062b..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(Vector3(Vector4(0, 0, -1, 0) * transform.GetGlobalTransform())); - camTransform = transform.GetGlobalTransform(); + cam->Translation = transform.GetGlobalPosition(); + + const Vector3& forward = QuatToDirection(transform.GetGlobalRotation()); + cam->SetDirection(forward); + + camTransform = transform.GetGlobalTransform(); break; } } 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);