I'm trying to create a first person style camera.
So far I have moving along the x and y axes sorted and now need to work on changing the pitch of the camera.
E.g. To move forward 2.5f units:
mainCamera->moveForward(2.5f);
Camera is derived from MovableObject:
void MovableObject::moveForward(float distance)
{
m_position.z += cos(m_rotation.y) * distance;
m_position.x -= sin(m_rotation.y) * distance;
}
m_position - position vector
m_rotation - rotation vector (in radians)
The camera update method sorts out the matrix stuff...
void Camera::Update()
{
GUMatrix4 finalMat, translation, rotation, scale;
GUVector3 rot = getRotation();
// Create matricies
translation = GUMatrix4::translationMatrix(getX(), getY(), getZ());
rotation = GUMatrix4::rotationMatrix(rot.x, rot.y, rot.z);
glMatrixMode(GL_PROJECTION);
finalMat = getProjMat() * rotation * translation;
glLoadMatrixf((GLfloat*) &finalMat);
}
translation - the 4x4 translation matrix.
rotation- the 4x4 rotation matrix.
The code above works beautifully for moving forwards on the x and z axes. At the moment the camera is rotated about the Y axis using the mouses horizontal movement.
void mouseMove(int x, int y) {
if (mDown) {
int dx = x - mouse_x;
int dy = y - mouse_y;
// rotate camera
if (mainCamera)
{
mainCamera->setYaw(mainCamera->getYaw() + (float)-dx);
}
mouse_x = x;
mouse_y = y;
}
}
I now want the camera to rotate about the x and z axes using the mouses vertical position. The camera then needs to move forwards with the Y axis included.
So far for changing the cameras pitch I have:
void MovableObject::changePitch(float degrees)
{
// Convert change to radians
float rads = MathHelp::degreesToRadians(degrees);
// X rotate more when looking down Z axis
m_rotation.x += cos(m_rotation.y) * rads;
}
Hopefully this is making sense to somebody.
Thanks in advance!
Dan.