Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make controls more intuitive #128

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 50 additions & 21 deletions common/controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ glm::mat4 getProjectionMatrix(){
return ProjectionMatrix;
}

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

// 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
Expand All @@ -32,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
Expand All @@ -43,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);
Expand All @@ -52,40 +59,62 @@ 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;
}
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;
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){
position -= direction * deltaTime * speed;
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){
position -= right * deltaTime * speed;
if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS
|| glfwGetKey( window, GLFW_KEY_A ) == GLFW_PRESS){
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.
Expand All @@ -101,4 +130,4 @@ void computeMatricesFromInputs(){

// For the next frame, the "last time" will be "now"
lastTime = currentTime;
}
}