C/C++ >> looking up and down (opengl)
Posted by Flikm on 06:51:00 06-04-2002
im writing a camera class, but im having problems with the y axis(rotating up and down). here is the code:
Code:
#define FORWARD 0
#define BACKWARD 1
#define LEFT 2
#define RIGHT 3
#define UP 4
#define DOWN 5

class Camera{
public:
GLfloat angle; //the angle to use for turning the camera
GLfloat x, y, z; //camera position
GLfloat lx, ly, lz; //line of sight
GLfloat pitch; //angle of the y axis

Camera(){
angle = 0.0f;
x = 0.0f; y = 0.0f; z = 0.0f;
lx = 0.0f; ly = 0.0f; lz = -1.0f;
pitch = 0.0f;
}

void Move( int direction ){
x = x + direction * lx * 0.1f;
z = z + direction * lz * 0.1f;
}

void Orient(){
lx = sin( angle );
lz = -cos( angle );
}

void ChangePitch(){
ly = sin( pitch ) * lz;
lz = cos( pitch ) * lz;
}

void move( int direction ){
switch( direction ){
case FORWARD:
Move( 1 );
break;
case BACKWARD:
Move( -1 );
break;
case LEFT:
angle -= 0.01f;
Orient();
break;
case RIGHT:
angle += 0.01f;
Orient();
break;
case UP:
if( pitch <= -1.0f ) break;
pitch -= 0.01f;
ChangePitch();
break;
case DOWN:
if( pitch >= 1.0f ) break;
pitch += 0.01f;
ChangePitch();
break;
default:
break;
}
}

void View(){
glLoadIdentity();
gluLookAt( x, y, z,
x + lx, y + ly, z + lz,
0.0f, 1.0f, 0.0f );
}
};

the first problem is that when the camera hits the limit(1.0f or -1.0f) and it tries to center again, it screws up (on its way up).

the second problem is that when i rotate on the x axis then the y, the camera swings in the direction of x.