I am currently working on moving a camera in 3d space. I want it to move ONLY in the x and z-axis (horizontal). So far it was moving correctly when the rotation of the camera is all zero, but when I change the value of it, the camera starts to act a little weird, moving in the wrong direction. I am using SFML, but I think it's the math that causes the problem. It is weird because the code I move the camera is identical to one of the projects I wrote earlier, but the project works fine.
Here's the code that constrains the rotation:
camera.rot.x = movey;
if (glm::degrees(camera.rot.x) < -180.0f) camera.rot.x = glm::radians(180.0f) camera.rot.x;
if (glm::degrees(camera.rot.x) > 180.0f) camera.rot.x = camera.rot.x glm::radians(-180.0f);
camera.rot.y -= movex;
if (glm::degrees(camera.rot.y) < -180.0f) camera.rot.y = glm::radians(180.0f) camera.rot.y;
if (glm::degrees(camera.rot.y) > 180.0f) camera.rot.y = camera.rot.y glm::radians(-180.0f);
The code I use to move the camera:
float x = sin(camera.rot.y) * camera.speed; float z = cos(camera.rot.y) * camera.speed;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { camera.pos.x -= x; camera.pos.z -= z; }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { camera.pos.x = x; camera.pos.z = z; }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { camera.pos.x = z; camera.pos.z -= x; }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { camera.pos.x -= z; camera.pos.z = x; }
Appreciate it if anyone can help me with this question.
For anyone that's wondering, here's the code i use to transform 3d coords to screen coords:
glm::mat3 x(0.0f);
x[0][0] = 1.0f;
x[1][1] = cos(camera.rot.x);
x[1][2] = sin(camera.rot.x);
x[2][1] = -sin(camera.rot.x);
x[2][2] = cos(camera.rot.x);
glm::mat3 y(0.0f);
y[0][0] = cos(camera.rot.y);
y[0][2] = -sin(camera.rot.y);
y[1][1] = 1.0f;
y[2][0] = sin(camera.rot.y);
y[2][2] = cos(camera.rot.y);
glm::mat3 z(0.0f);
z[0][0] = cos(camera.rot.z);
z[0][1] = sin(camera.rot.z);
z[1][0] = -sin(camera.rot.z);
z[1][1] = cos(camera.rot.z);
z[2][2] = 1.0f;
glm::mat3 trans = x * y * z;
//trans is the perspective matrix
glm::vec3 e(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, (float)tan(camera.fov / 2) * (float)(SCREEN_WIDTH / 2));
//e is the distance from the camera pinhole relate to the 'screen'
glm::mat3 ScreenTrans(0.0f);
ScreenTrans[0][0] = 1.0f;
ScreenTrans[0][2] = e.x / e.z;
ScreenTrans[1][1] = 1.0f;
ScreenTrans[1][2] = e.y / e.z;
ScreenTrans[2][2] = 1.0f / e.z;
//ScreenTrans is the matrix use to transform Camera space into Screen space
std::vector<glm::vec3> 3D_coords = Some3DPoints;
for (glm::vec3 coord: 3D_coords){
glm::vec3 afterTrans = trans * coord;
glm::vec3 cameraSpace = afterTrans - camera.pos;
glm::vec3 2DScreenCoord = cameraSpace * ScreenTrans;
}
CodePudding user response:
After digging into my code for a long time, i find that the problem is in the code where I constrains my rotations, and how I get the "trans" matrix.
First, the constrains code should be:
camera.rot.x -= movey;
if (glm::degrees(camera.rot.x) < -90.0f) camera.rot.x = glm::radians(180.0f) camera.rot.x;
if (glm::degrees(camera.rot.x) > 90.0f) camera.rot.x = camera.rot.x glm::radians(-180.0f);
camera.rot.y = movex;
if (glm::degrees(camera.rot.y) < -180.0f) camera.rot.y = glm::radians(360.0f) camera.rot.y;
if (glm::degrees(camera.rot.y) > 180.0f) camera.rot.y = camera.rot.y glm::radians(-360.0f);
I miss calculated the value and mistake rot.x as the y-axis and rot.y as the x-axis.
The next is the "trans" matrix. It should be:
glm::mat3 trans = z * y * x;
It should be FIRST handling the rotation of the y-axis, than the x-axis, or else your rotation of the y-axis(camera space) will always be on the y-axis(world space).