I'm currently working on a small Voxel-Editor Project. In the editor, I plan on using an Arc-Ball camera which rotate around the model. My camera is currently working but in an odd way. I can't seem to be able to move around the Z-Axis on my model which cause my camera angle to get smaller as I reach one side. For example, if I go to the left side of my ball, I wont be able to turn around it completly.
Here is my code which update my current View-Matrix into the updated one.
void t_Math::CreateLookAt(glm::mat4& InMatrix, sf::Vector3f pointToFix, GLfloat InPitch, GLfloat InYaw, GLfloat valueToScale)
{
glm::mat4 rotationMatrix = glm::mat4(1.0);
const GLfloat FinalPitchDegree = InPitch / 57.2958;
const GLfloat FinalYawDegree = InYaw / 57.2958;
rotationMatrix = glm::rotate(rotationMatrix, FinalPitchDegree, glm::vec3(1.0, 0.0, 0.0));
rotationMatrix = glm::rotate(rotationMatrix, FinalYawDegree, glm::vec3(0.0, 1.0, 0.0));
const glm::vec4 cameraPosition4 = rotationMatrix * glm::vec4(1.0);
const glm::vec3 cameraPositionFinal = valueToScale * glm::vec3(cameraPosition4);
const glm::vec3 constObjetMiddle = glm::vec3(pointToFix.x, pointToFix.y, pointToFix.z);
InMatrix = glm::lookAt(cameraPositionFinal, constObjetMiddle, glm::vec3(0.0, 1.0, 0.0));
}
Here is what I believe my code do.
The function receive a View Matrix, The model Center Coords, The Pitch, The Yaw and finally the Scalar (The zoom). Note: My Pitch and Yaw are simple GLFloat which mean they don't have limits and can go from -R to +R.
Now for the code. -We first create a base Matrix 4 -We Divide the Pitch and Yaw by 57.2958 (a radian) because GLM::rotate works with radian and I want it to be precise so in degree. -We then rotate the base Matrix on their respective Axis using the proper Pitch and Yaw -We then calculate the position of the Camera by using the ROTATED matrix which we transform into a Vec4 -We calculate the final Camera position by multiplying by the Scaling Vector (The Zoom) and we transform this Vector4 into a Vec3 -We create another Vec3 which is the Camera Target by using the value received by the function -We use all we collected to create a new lookAt matrix and we apply it to the view matrix.
I think I might be missing the Z-Axis rotation but how am I suppose to calculate it ? Should I use the Pitch or the Yaw ? Since i'm using (W-A-S-D) to move around here is what my keys does.
W: Increase Pitch S: Decrease Pitch D: Increase Yaw A: Decrease Yaw
Do I need to put the Roll into this and which keys should increase and decrease it ?