diff --git a/src/main.cpp b/src/main.cpp index 6e94f6f..ed3ca5c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,10 @@ #include #include #include +#include +#include #include +#include #include #include @@ -107,15 +110,15 @@ void processInput(GLFWwindow* window) { float shiftFactor = (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) ? -0.5 : 0.5; shiftFactor *= deltaTime; if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS) { - parts.back().rotation.x += shiftFactor; + parts.back().rotation *= glm::angleAxis(shiftFactor, glm::vec3(1, 0, 0)); syncPartPhysics(parts.back()); } if (glfwGetKey(window, GLFW_KEY_Y) == GLFW_PRESS) { - parts.back().rotation.y += shiftFactor; + parts.back().rotation *= glm::angleAxis(shiftFactor, glm::vec3(0, 1, 0)); syncPartPhysics(parts.back()); } if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS) { - parts.back().rotation.z += shiftFactor; + parts.back().rotation *= glm::angleAxis(shiftFactor, glm::vec3(0, 0, 1)); syncPartPhysics(parts.back()); } } diff --git a/src/part.h b/src/part.h index f812f01..b1a349e 100644 --- a/src/part.h +++ b/src/part.h @@ -1,5 +1,7 @@ #pragma once -#include +#include +#include +#include #include #include "rendering/material.h" @@ -7,7 +9,7 @@ namespace rp = reactphysics3d; struct Part { glm::vec3 position; - glm::vec3 rotation; + glm::quat rotation = glm::identity(); glm::vec3 scale; Material material; diff --git a/src/physics/simulation.cpp b/src/physics/simulation.cpp index c0f92e9..36ab8fe 100644 --- a/src/physics/simulation.cpp +++ b/src/physics/simulation.cpp @@ -34,12 +34,8 @@ void simulationInit() { void syncPartPhysics(Part& part) { glm::mat4 rotMat = glm::mat4(1.0f); - rotMat = glm::rotate(rotMat, part.rotation.x, glm::vec3(1., 0., 0.)); - rotMat = glm::rotate(rotMat, part.rotation.y, glm::vec3(0., 1., 0.)); - rotMat = glm::rotate(rotMat, part.rotation.z, glm::vec3(0., 0., 1.)); - glm::quat quat(rotMat); - rp::Transform transform(glmToRp(part.position), glmToRp(quat)); + rp::Transform transform(glmToRp(part.position), glmToRp(part.rotation)); if (!part.rigidBody) { part.rigidBody = world->createRigidBody(transform); } else { @@ -63,6 +59,7 @@ void physicsStep(float deltaTime) { for (Part& part : parts) { const rp::Transform& transform = part.rigidBody->getTransform(); part.position = rpToGlm(transform.getPosition()); - part.rotation = glm::eulerAngles(rpToGlm(transform.getOrientation())); + // part.rotation = glm::eulerAngles(rpToGlm(transform.getOrientation())); + part.rotation = rpToGlm(transform.getOrientation()); } } \ No newline at end of file diff --git a/src/rendering/defaultmeshes.cpp b/src/rendering/defaultmeshes.cpp index 9b85a78..e6706a8 100644 --- a/src/rendering/defaultmeshes.cpp +++ b/src/rendering/defaultmeshes.cpp @@ -5,12 +5,12 @@ Mesh* CUBE_MESH; void initMeshes() { static float vertices[] = { // positions // normals // texture coords - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, diff --git a/src/rendering/renderer.cpp b/src/rendering/renderer.cpp index c23abc7..6a5fba7 100644 --- a/src/rendering/renderer.cpp +++ b/src/rendering/renderer.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -75,9 +76,7 @@ void render(GLFWwindow* window) { for (Part part : parts) { glm::mat4 model = glm::mat4(1.0f); model = glm::translate(model, part.position); - model = glm::rotate(model, part.rotation.x, glm::vec3(1., 0., 0.)); - model = glm::rotate(model, part.rotation.y, glm::vec3(0., 1., 0.)); - model = glm::rotate(model, part.rotation.z, glm::vec3(0., 0., 1.)); + model = model * glm::mat4_cast(part.rotation); model = glm::scale(model, part.scale); shader->set("model", model); glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model)));