Hello everybody
I`ve created this camera class which should resemble an fps style camera. I want the character to be able to view up-down (pitch) and rotate around the y-axis (roll) for start. This is the code i`m using
void CCamera::MoveCamera(float amount)
{
CVector3 dir = m_view - m_pos; // direction of movement
dir.Normal(); // normalize direction vector
// Move the camera.
m_pos += dir * amount;
// Move the view along with the position.
m_view += dir * amount;
}
void CCamera::RotateCamera(float angle, float X, float Y, float Z)
{
float ca = (float)cos(angle);
float sa = (float)sin(angle);
// Get direction.
CVector3 dir = m_view - m_pos;
dir.Normal();
CVector3 dir2;
// Calculate the new X position.
dir2.x = (ca + (1 - ca) * X) * dir.x;
dir2.x += ((1 - ca) * X * Y - Z * sa)* dir.y;
dir2.x += ((1 - ca) * X * Z + Y * sa) * dir.z;
// Calculate the new Y position.
dir2.y = ((1 - ca) * X * Y + Z * sa) * dir.x;
dir2.y += (ca + (1 - ca) * Y) * dir.y;
dir2.y += ((1 - ca) * Y * Z - X * sa) * dir.z;
// Calculate the new Z position.
dir2.z = ((1 - ca) * X * Z - Y * sa) * dir.x;
dir2.z += ((1 - ca) * Y * Z + X * sa) * dir.y;
dir2.z += (ca + (1 - ca) * Z) * dir.z;
// Apply to view.
m_view = m_pos + dir2;
}
I use the RotateCamera in my game loop to make the character look up-down according to the mouse movement. Like this...
curPoint = g_InputSystem->GetMousePos();
int xDelta = curPoint.x - oldPoint.x;
int yDelta = curPoint.y - oldPoint.y;
oldPoint = curPoint;
//g_camera.UpdatePitchYaw(g_InputSystem->GetMousePos());
if (xDelta != 0)
g_camera.RotateCamera(xDelta * 0.01f, 0, 1, 0); // rotate around y-axis
if (yDelta != 0)
g_camera.RotateCamera(yDelta * 0.01f, 0, 0, 1); // rotate around z-axis
//g_camera.Pitch(yDelta * 0.2f);
the problem is that after some rotating the when I move forward-backward the camera starts flying around or below the ground. I want it to stay on the ground and just be able to look at the sky or below. Is my code totally off?
thx, in advance
George