Limit the rotation of camera on axis X

I am trying to make the camera unable to turn 360 to the x axis, so my camera can fixed to looking from top to bottom only (not allowing look backward except turn around base on y axis). the code below is how i rotate my camera base on x axis, and i can’t figure out how can i limit the camera that won’t look beyong 180.

 void Camera::RotateX(float angle) 
{
	float radians = angle*PI/180.0f;

	CVec3df v1 = upward * (-(float)sin(radians));
	CVec3df v2 = forward * ((float)cos(radians));

	forward = (v1 + v2).normalise();

	upward = forward.cross(left).normalise();
} 

Don’t you simply add a line at the top:

if (angle > 180.0F) angle = 180.0F;
float radians = …

or am I missing something?

the value of angle is the amount of angle going to turn not turn to which angle. this is the input, i think i got somethings wrong in my implementation but i am not sure what

void handleMouseMotion(int x, int y)
{

POINT pointCursorPos; // declare a point

// mouse position tracking
GetCursorPos(&pointCursorPos); // get current point position

int xCenter = currentWidth/2;
int yCenter = currentHeight/2;

int delX = xCenter - pointCursorPos.x;
int delY = pointCursorPos.y - yCenter;

if (!(delX || delY))
	return;

cam.RotateX(delY*sensitivity);
cam.RotateY(delX*sensitivity);

	
SetCursorPos(xCenter, yCenter);

}

void Camera::RotateX(float angle)
{
  float radians = angle*PI/180.0f;
  CVec3df v1 = upward * (-(float)sin(radians));
  CVec3df v2 = forward * ((float)cos(radians));
  forward = (v1 + v2).normalise();
  upward = forward.cross(left).normalise();

  if (upward.y < 0)  //we're upside down
  {
    if (forward.y > 0) //we're looking up
    {
      forward = CVec3df(0, 1, 0);
    }
    else  //we're looking down
    {
      forward = CVec3df(0, -1, 0);
    }
    upward = forward.cross(left).normalise();
  }
} 

Just made that up. Untested :slight_smile:

wow, it worked. thanks a lot :smiley: