From a812cc9577fd6bb409bb78c2404edab3d96d5a4d Mon Sep 17 00:00:00 2001 From: n-l-i <57808975+n-l-i@users.noreply.github.com> Date: Mon, 9 Jan 2023 09:41:18 +0100 Subject: [PATCH 1/4] Make controls more intuitive This changes the controls to be more similar to what you would see in most video games, i.e. the up-vector always points in the same direction, here in the +Y direction, and arrow keys only move in the xz-plane, not up and down. This also necessitates the introduction of dedicated up and down controls, here using the spacebar and shift keys correspondingly. --- common/controls.cpp | 47 +++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/common/controls.cpp b/common/controls.cpp index fdf91b3d5..3943fd924 100644 --- a/common/controls.cpp +++ b/common/controls.cpp @@ -19,11 +19,12 @@ glm::mat4 getProjectionMatrix(){ return ProjectionMatrix; } +const float PI = glm::pi(); // Initial position : on +Z glm::vec3 position = glm::vec3( 0, 0, 5 ); // Initial horizontal angle : toward -Z -float horizontalAngle = 3.14f; +float horizontalAngle = PI; // Initial vertical angle : none float verticalAngle = 0.0f; // Initial Field of View @@ -54,30 +55,39 @@ void computeMatricesFromInputs(){ horizontalAngle += mouseSpeed * float(1024/2 - xpos ); verticalAngle += mouseSpeed * float( 768/2 - ypos ); + if (verticalAngle < PI/2+0.01) { + verticalAngle = PI/2+0.01; + } + if (verticalAngle > 3*PI/2-0.01) { + verticalAngle = 3*PI/2-0.01; + } + // Direction : Spherical coordinates to Cartesian coordinates conversion glm::vec3 direction( cos(verticalAngle) * sin(horizontalAngle), sin(verticalAngle), cos(verticalAngle) * cos(horizontalAngle) ); - - // Right vector - glm::vec3 right = glm::vec3( - sin(horizontalAngle - 3.14f/2.0f), - 0, - cos(horizontalAngle - 3.14f/2.0f) - ); - + + // Forwards vector + glm::vec3 forwards = glm::normalize(glm::vec3(direction.x,0,direction.z)); + glm::vec3 backwards = (-1.0f)*forwards; + // Up vector - glm::vec3 up = glm::cross( right, direction ); + glm::vec3 up = glm::vec3( 0, 1, 0 ); + glm::vec3 down = (-1.0f)*up; + + // Right vector + glm::vec3 right = glm::cross(forwards,up); + glm::vec3 left = (-1.0f)*right; // Move forward if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS){ - position += direction * deltaTime * speed; + position += forwards * deltaTime * speed; } // Move backward if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS){ - position -= direction * deltaTime * speed; + position += backwards * deltaTime * speed; } // Strafe right if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS){ @@ -85,7 +95,16 @@ void computeMatricesFromInputs(){ } // Strafe left if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS){ - position -= right * deltaTime * speed; + position += left * deltaTime * speed; + } + // Move up + if (glfwGetKey( window, GLFW_KEY_SPACE ) == GLFW_PRESS){ + position += up * deltaTime * speed; + } + // Move down + if (glfwGetKey( window, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS + || glfwGetKey( window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS){ + position += down * deltaTime * speed; } float FoV = initialFoV;// - 5 * glfwGetMouseWheel(); // Now GLFW 3 requires setting up a callback for this. It's a bit too complicated for this beginner's tutorial, so it's disabled instead. @@ -101,4 +120,4 @@ void computeMatricesFromInputs(){ // For the next frame, the "last time" will be "now" lastTime = currentTime; -} \ No newline at end of file +} From 026265eb33a37ad9dd35e41c45050621e3874cf7 Mon Sep 17 00:00:00 2001 From: n-l-i <57808975+n-l-i@users.noreply.github.com> Date: Mon, 9 Jan 2023 09:41:25 +0100 Subject: [PATCH 2/4] Add WASD-controls as an alternative to arrow keys --- common/controls.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/common/controls.cpp b/common/controls.cpp index 3943fd924..f7f7d5bd5 100644 --- a/common/controls.cpp +++ b/common/controls.cpp @@ -82,19 +82,23 @@ void computeMatricesFromInputs(){ glm::vec3 left = (-1.0f)*right; // Move forward - if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS){ + if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS + || glfwGetKey( window, GLFW_KEY_W ) == GLFW_PRESS){ position += forwards * deltaTime * speed; } // Move backward - if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS){ + if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS + || glfwGetKey( window, GLFW_KEY_S ) == GLFW_PRESS){ position += backwards * deltaTime * speed; } // Strafe right - if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS){ + if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS + || glfwGetKey( window, GLFW_KEY_D ) == GLFW_PRESS){ position += right * deltaTime * speed; } // Strafe left - if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS){ + if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS + || glfwGetKey( window, GLFW_KEY_A ) == GLFW_PRESS){ position += left * deltaTime * speed; } // Move up From f68cf8ec28640ff4492f4e806d94f7dcbcd0aa4f Mon Sep 17 00:00:00 2001 From: n-l-i <57808975+n-l-i@users.noreply.github.com> Date: Mon, 9 Jan 2023 09:41:27 +0100 Subject: [PATCH 3/4] Invert vertical mouse movement --- common/controls.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/controls.cpp b/common/controls.cpp index f7f7d5bd5..032499941 100644 --- a/common/controls.cpp +++ b/common/controls.cpp @@ -53,7 +53,7 @@ void computeMatricesFromInputs(){ // Compute new orientation horizontalAngle += mouseSpeed * float(1024/2 - xpos ); - verticalAngle += mouseSpeed * float( 768/2 - ypos ); + verticalAngle -= mouseSpeed * float( 768/2 - ypos ); if (verticalAngle < PI/2+0.01) { verticalAngle = PI/2+0.01; From 58af56f53d008cc13ba33ec089d255ac8ed0b0c5 Mon Sep 17 00:00:00 2001 From: n-l-i <57808975+n-l-i@users.noreply.github.com> Date: Mon, 9 Jan 2023 09:41:29 +0100 Subject: [PATCH 4/4] Fix camera direction bug --- common/controls.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/common/controls.cpp b/common/controls.cpp index 032499941..c049977ab 100644 --- a/common/controls.cpp +++ b/common/controls.cpp @@ -33,8 +33,6 @@ float initialFoV = 45.0f; float speed = 3.0f; // 3 units / second float mouseSpeed = 0.005f; - - void computeMatricesFromInputs(){ // glfwGetTime is called only once, the first time this function is called @@ -44,6 +42,14 @@ void computeMatricesFromInputs(){ double currentTime = glfwGetTime(); float deltaTime = float(currentTime - lastTime); + // First time this function is called, the cursor position is reset since + // an unintended movement will otherwise register on the first frame + static bool first_frame = true; + if (first_frame){ + glfwSetCursorPos(window, 1024/2, 768/2); + first_frame = false; + } + // Get mouse position double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos);