Original Post
When using my camera I only am able to move once. For example: Im looking at a ball and I move the mouse down. I only move down a notch and I cant move anymore. Here is my code for the mouse look function: And here is my main function:
void Camera::MouseLook(int screenWidth, int screenHeight, float MouseSensitivity)
{
float CurrentRotationX;
// the coordinates of our mouse coordinates
int MouseX, MouseY;
// the middle of the screen in the x direction
int MiddleX = screenWidth/2;
// the middle of the screen in the y direction
int MiddleY = screenHeight/2;
// vector that describes mouseposition - center
Math::Vector3 MouseDirection(0, 0, 0);
// The maximum angle we can look up or down, in radians
double maxAngle = 1;
// This function gets the position of the mouse
SDL_GetMouseState(&MouseX, &MouseY);
// if the mouse hasn't moved, return without doing
// anything to our view
if((MouseX == MiddleX) && (MouseY == MiddleY))
return;
// otherwise move the mouse back to the middle of the screen
SDL_WarpMouse(MiddleX, MiddleY);
// get the distance and direction the mouse moved in x (in
// pixels). can't use the actual number of pixels in radians,
// as only six pixels would cause a full 360 degree rotation.
// So I use a mousesensitivity variable that can be changed to
// vary how many radians we want to turn in the x-direction for
// a given mouse movement distance
// The positive rotation is counter-clockwise.
// Moving the mouse down is a negative rotation about the x axis
// Moving the mouse right is a negative rotation about the y axis
MouseDirection.x = (MiddleX - MouseX)/MouseSensitivity;
MouseDirection.y = (MiddleY - MouseY)/MouseSensitivity;
CurrentRotationX += MouseDirection.y;
// don't want to rotate up more than one radian, so I cap it.
if(CurrentRotationX > 1)
{
CurrentRotationX = 1;
return;
}
// don't want to rotate down more than one radian, so I cap it.
if(CurrentRotationX < -1)
{
CurrentRotationX = -1;
return;
}
else
{
// get the axis to rotate around the x-axis.
Math::Vector3 Axis(view.x-pos.x,view.y-pos.y,view.z-pos.z);
Axis.Cross(upVector);
// To be able to use the quaternion conjugate, the axis to
// rotate around must be normalized.
Axis.Normalize();
// Rotate around the y axis
Rotate(MouseDirection.y, Axis.x, Axis.y, Axis.z);
// Rotate around the x axis
Rotate(MouseDirection.x, 0, 1, 0);
}
}
int main(int argc, char *argv[])
{
initialize();
GLInit();
int done=0;
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
if(event.type == SDL_MOUSEMOTION)
{
SDL_WM_SetCaption("Mouse is moved.",0);
camera.MouseLook(640,480,1.0);
}
}
draw();
}
return 0;
}