I have been wondering how I can get my FPS camera to basically move forward and backward depending on the camera's direction, but I've been failing miserably, I would like to know the way to do this optimally, as right now my code is switch directions behind my triangle (w becomes s and s becomes w) and generally not work (move diagonally instead of forward, sometimes), rotation works perfectly, but translation screws up my matrix...
void glfwCursorCallback(GLFWwindow* window, double x, double y) {
camera.rx += (x - camera.lcx) * 0.01f;
camera.ry += (y - camera.lcy) * 0.01f;
kmMat4RotationYawPitchRoll(&camera.mat, camera.ry , camera.rx, 0.0f);
camera.lcx = x;
camera.lcy = y;
}
...
kmMat4PerspectiveProjection(&projection, 90.0f, aspect, 0.1f, 1000.f);
float x = 0.0f, y = 0.0f, z = -1.0f;
while(!glfwWindowShouldClose(window)) {
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
/* pitch - ry */
x += 0.1*sin(camera.ry)*cos(camera.rx);
y += 0.1*sin(camera.ry)*sin(camera.rx);
z += 0.1*cos(camera.ry);
}
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
x -= 0.1*sin(camera.ry)*cos(camera.rx);
y -= 0.1*sin(camera.ry)*sin(camera.rx);
z -= 0.1*cos(camera.ry);
}
glClear(GL_COLOR_BUFFER_BIT);
kmMat4Translation(&transform, x, y, z);
kmMat4Multiply(&object, &camera.mat, &transform);
kmMat4Multiply(&final, &projection, &object);
glUniformMatrix4fv(shader.mpm, 1, GL_FALSE, final.mat);
...
I have no idea how to do it since I never did it before, so I would love some pointers from the more experienced folk around here!
Edit: The purpose is to have the camera move forward according to orientation. Also, It works perfectly if i omit x
, y
and just set z
to +- 0.1... so it's not an issue of matrix multiplications