Skip to content

Commit c512e8d

Browse files
committed
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.
1 parent 316cccc commit c512e8d

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

common/controls.cpp

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ glm::mat4 getProjectionMatrix(){
1919
return ProjectionMatrix;
2020
}
2121

22+
const float PI = glm::pi<float>();
2223

2324
// Initial position : on +Z
2425
glm::vec3 position = glm::vec3( 0, 0, 5 );
2526
// Initial horizontal angle : toward -Z
26-
float horizontalAngle = 3.14f;
27+
float horizontalAngle = PI;
2728
// Initial vertical angle : none
2829
float verticalAngle = 0.0f;
2930
// Initial Field of View
@@ -54,38 +55,56 @@ void computeMatricesFromInputs(){
5455
horizontalAngle += mouseSpeed * float(1024/2 - xpos );
5556
verticalAngle += mouseSpeed * float( 768/2 - ypos );
5657

58+
if (verticalAngle < PI/2+0.01) {
59+
verticalAngle = PI/2+0.01;
60+
}
61+
if (verticalAngle > 3*PI/2-0.01) {
62+
verticalAngle = 3*PI/2-0.01;
63+
}
64+
5765
// Direction : Spherical coordinates to Cartesian coordinates conversion
5866
glm::vec3 direction(
5967
cos(verticalAngle) * sin(horizontalAngle),
6068
sin(verticalAngle),
6169
cos(verticalAngle) * cos(horizontalAngle)
6270
);
63-
64-
// Right vector
65-
glm::vec3 right = glm::vec3(
66-
sin(horizontalAngle - 3.14f/2.0f),
67-
0,
68-
cos(horizontalAngle - 3.14f/2.0f)
69-
);
70-
71+
72+
// Forwards vector
73+
glm::vec3 forwards = glm::normalize(glm::vec3(direction.x,0,direction.z));
74+
glm::vec3 backwards = (-1.0f)*forwards;
75+
7176
// Up vector
72-
glm::vec3 up = glm::cross( right, direction );
77+
glm::vec3 up = glm::vec3( 0, 1, 0 );
78+
glm::vec3 down = (-1.0f)*up;
79+
80+
// Right vector
81+
glm::vec3 right = glm::cross(forwards,up);
82+
glm::vec3 left = (-1.0f)*right;
7383

7484
// Move forward
7585
if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS){
76-
position += direction * deltaTime * speed;
86+
position += forwards * deltaTime * speed;
7787
}
7888
// Move backward
7989
if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS){
80-
position -= direction * deltaTime * speed;
90+
position += backwards * deltaTime * speed;
8191
}
8292
// Strafe right
8393
if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS){
8494
position += right * deltaTime * speed;
8595
}
8696
// Strafe left
8797
if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS){
88-
position -= right * deltaTime * speed;
98+
position += left * deltaTime * speed;
99+
}
100+
// Move up
101+
if (glfwGetKey( window, GLFW_KEY_SPACE ) == GLFW_PRESS){
102+
position += up * deltaTime * speed;
103+
}
104+
// Move down
105+
if (glfwGetKey( window, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS
106+
|| glfwGetKey( window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS){
107+
position += down * deltaTime * speed;
89108
}
90109

91110
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(){
101120

102121
// For the next frame, the "last time" will be "now"
103122
lastTime = currentTime;
104-
}
123+
}

0 commit comments

Comments
 (0)