cod999

07-04-2014, 05:33 AM

I try to make a camera that can walk on X/Z plane and rotate 360 degrees. I've looked at tutorials but I can't understand them. Most of them use trigonometric functions, like the one below:

// horizontal angle : toward -Z

float horizontalAngle = 3.14f;

// vertical angle : 0, look at the horizon

float verticalAngle = 0.0f;

int xpos, ypos;

glfwGetMousePos(&xpos, &ypos);

glfwSetMousePos(1024/2, 768/2);

horizontalAngle += mouseSpeed * deltaTime * float(1024/2 - xpos );

verticalAngle += mouseSpeed * deltaTime * float( 768/2 - ypos );

glm::vec3 direction(

cos(verticalAngle) * sin(horizontalAngle),

sin(verticalAngle),

cos(verticalAngle) * cos(horizontalAngle)

);

glm::vec3 right = glm::vec3(

sin(horizontalAngle - 3.14f/2.0f),

0,

cos(horizontalAngle - 3.14f/2.0f)

);

glm::vec3 up = glm::cross( right, direction );

ViewMatrix = glm::lookAt(

position, // Camera is here

position+direction, // and looks here : at the same position, plus "direction"

up // Head is up (set to 0,-1,0 to look upside-down)

);

Why is trigonometry used in the code,how do I create a camera that can freely walk and look around?

// horizontal angle : toward -Z

float horizontalAngle = 3.14f;

// vertical angle : 0, look at the horizon

float verticalAngle = 0.0f;

int xpos, ypos;

glfwGetMousePos(&xpos, &ypos);

glfwSetMousePos(1024/2, 768/2);

horizontalAngle += mouseSpeed * deltaTime * float(1024/2 - xpos );

verticalAngle += mouseSpeed * deltaTime * float( 768/2 - ypos );

glm::vec3 direction(

cos(verticalAngle) * sin(horizontalAngle),

sin(verticalAngle),

cos(verticalAngle) * cos(horizontalAngle)

);

glm::vec3 right = glm::vec3(

sin(horizontalAngle - 3.14f/2.0f),

0,

cos(horizontalAngle - 3.14f/2.0f)

);

glm::vec3 up = glm::cross( right, direction );

ViewMatrix = glm::lookAt(

position, // Camera is here

position+direction, // and looks here : at the same position, plus "direction"

up // Head is up (set to 0,-1,0 to look upside-down)

);

Why is trigonometry used in the code,how do I create a camera that can freely walk and look around?