diff --git a/src/Components/Transform.cpp b/src/Components/Transform.cpp index 5a75e5c..6eaf2fa 100644 --- a/src/Components/Transform.cpp +++ b/src/Components/Transform.cpp @@ -32,6 +32,32 @@ void TransformComponent::Update(float _deltaTime) return; } +void TransformComponent::LookAt(const glm::vec3& target, const glm::vec3& up) { + // Calculate the direction vector from position to target + glm::vec3 direction = glm::normalize(target - position); + + // Handle the case when direction is parallel to up vector + if (glm::length(direction) == 0.0f) { + // Cannot look at the same position; no rotation needed + return; + } + + // Create a lookAt matrix + glm::mat4 lookAtMatrix = glm::lookAt(position, target, up); + + // Extract the rotation part of the matrix + glm::mat4 rotationMatrix = glm::mat4(glm::mat3(lookAtMatrix)); + + // Convert the rotation matrix to a quaternion + glm::quat rotationQuat = glm::quat_cast(rotationMatrix); + + // Extract Euler angles from the quaternion + glm::vec3 eulerRadians = glm::eulerAngles(rotationQuat); + + // Convert radians to degrees and set the rotation + rotation = glm::degrees(eulerRadians); + } + // New Methods glm::mat4 TransformComponent::GetTransformMatrix() const { diff --git a/src/Components/Transform.h b/src/Components/Transform.h index 34459e3..94c7b17 100644 --- a/src/Components/Transform.h +++ b/src/Components/Transform.h @@ -1,15 +1,23 @@ // Transform.h #pragma once - +#define GLM_ENABLE_EXPERIMENTAL #include "Component.h" -#include + #include +#include +#include // For glm::lookAt +#include // For glm::quat +#include // For glm::quat_cast +#include // For glm::pi +#include // For glm::eulerAngles + + class TransformComponent : public Component { public: glm::vec3 position; - glm::vec3 rotation; + glm::vec3 rotation; glm::vec3 scale; glm::vec3 GetPosition() const @@ -32,9 +40,13 @@ public: rotation = {x, y, z}; } - void LookAt(const glm::vec3& target, const glm::vec3& up) { - viewMatrix = glm::lookAt(position, target, up); - } + /** + * @brief Orients the transform to face the target position. + * + * @param target The target position to look at. + * @param up The up vector to use for orientation. + */ + void LookAt(const glm::vec3& target, const glm::vec3& up); TransformComponent(); virtual const std::string &GetName() const override; @@ -48,7 +60,6 @@ public: glm::mat4 GetTransformMatrix() const; void SetTransformMatrix(const glm::mat4& transform); - private: static const std::string name; }; diff --git a/src/Engine.cpp b/src/Engine.cpp index 6b61514..20f17ca 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -304,7 +304,7 @@ void MyEngine::Run() { ScopedTimer timer("RenderGame"); - m_RenderWindow->Show(&m_GameRunning); // The spinning triangle as ImGui::Image + m_RenderWindow->Show(&m_GameRunning, m_Ms); // The spinning triangle as ImGui::Image } { ScopedTimer timer("ShowEditor"); diff --git a/src/Windows/RenderWindow.cpp b/src/Windows/RenderWindow.cpp index 6cdfec8..f0356d4 100644 --- a/src/Windows/RenderWindow.cpp +++ b/src/Windows/RenderWindow.cpp @@ -331,7 +331,7 @@ void RenderWindow::Show(bool *GameRunning, double deltaTime) } else { - m_ActiveCamera = m_EditorCamera.GetComponent(); + m_ActiveCamera = m_EditorCamera->GetComponent(); // 1. Mouse Input for Rotation if (g_InputManager.IsMouseButtonPressed(MouseButton::RIGHT)) @@ -355,20 +355,20 @@ void RenderWindow::Show(bool *GameRunning, double deltaTime) editorDistance = glm::clamp(editorDistance, minZoom, maxZoom); // 3. Keyboard Input for Panning (WASD) - glm::vec3 forward = glm::normalize(editorTarget - m_EditorCamera.GetComponent()->GetPosition()); + glm::vec3 forward = glm::normalize(editorTarget - m_EditorCamera->GetComponent()->GetPosition()); glm::vec3 right = glm::normalize(glm::cross(forward, glm::vec3(0.0f, 1.0f, 0.0f))); glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f); deltaTime = static_cast(deltaTime); if (g_InputManager.IsKeyPressed(KeyCode::W)) - editorTarget += up * movementSpeed * deltaTime; + editorTarget += up.y * movementSpeed * deltaTime; if (g_InputManager.IsKeyPressed(KeyCode::S)) - editorTarget -= up * movementSpeed * deltaTime; + editorTarget -= up.y * movementSpeed * deltaTime; if (g_InputManager.IsKeyPressed(KeyCode::A)) - editorTarget -= right * movementSpeed * deltaTime; + editorTarget -= right.x * movementSpeed * deltaTime; if (g_InputManager.IsKeyPressed(KeyCode::D)) - editorTarget += right * movementSpeed * deltaTime; + editorTarget += right.x * movementSpeed * deltaTime; // 4. Calculate the New Camera Position glm::vec3 direction; @@ -380,8 +380,8 @@ void RenderWindow::Show(bool *GameRunning, double deltaTime) glm::vec3 newPosition = editorTarget - direction * editorDistance; // 5. Update the Editor Camera's Transform - auto editorTransform = m_EditorCamera.GetComponent(); - editorTransform->SetPosition(newPosition); + auto editorTransform = m_EditorCamera->GetComponent(); + editorTransform->SetPosition(newPosition.x,newPosition.y,newPosition.z); editorTransform->LookAt(editorTarget, up); // 6. Retrieve Updated Matrices @@ -458,9 +458,11 @@ void RenderWindow::InitGLResources() // throw this in here cus we dont have a constructor m_ActiveCamera = nullptr; + m_EditorCamera = std::make_shared(-1, "EditorCamera"); + // Setup the editor camera - m_EditorCamera.AddComponent(std::make_shared()); - m_EditorCamera.AddComponent(std::make_shared()); + m_EditorCamera->AddComponent(std::make_shared()); + m_EditorCamera->AddComponent(std::make_shared()); { std::shared_ptr shaderAsset = g_AssetManager.loadAsset(AssetType::SHADER, "assets/shaders/UnlitMaterial"); diff --git a/src/Windows/RenderWindow.h b/src/Windows/RenderWindow.h index 9805ba6..be77e37 100644 --- a/src/Windows/RenderWindow.h +++ b/src/Windows/RenderWindow.h @@ -42,7 +42,7 @@ private: Shader* m_ShaderPtr = nullptr; Shader* m_LineShaderPtr = nullptr; - GameObject m_EditorCamera; + std::shared_ptr m_EditorCamera; };