I created a camera class. You can rotate 360 degrees with the mouse and move up and down. As I expected, the same as in all games. It is also possible to move back and forth, as in all games. But I do not know how to implement moving left and right.
I do the following:
This calls each frame:
gluLookAt(_posX , _posY , _posZ,
_viewX, _viewY, _viewZ,
_upX, _upY, _upZ );
My move function
Does not work:
void Camera::moveLeft()
{
float rot= (_viewY / 180 * PI);
_moveX -= float(cos(rot)) * 0.5;
_moveZ -= float(sin(rot)) * 0.5;
}
Does the move forward in the scene:
void Camera::moveForward()
{
float viewX = _viewX - _posX;
float viewY = _viewY - _posY;
float viewZ = _viewZ - _posZ;
_posX += viewX * speed
_posY += viewY * speed;
_posZ += viewZ * speed;
_viewX += viewX * speed;
_viewY += viewY * speed;
_viewZ += viewZ * speed;
}
When I only move with the mouse, there is no problem. But if I use this function and rotate with the mouse, I get some strange camera movements.
Any ideas on how to solve this?
thank
@edit
So, I deleted the glTranslated statement, and I changed the moveLeft function to the following:
void Camera::moveLeft(){
float x = ((_viewY * _upZ) - (_viewZ * _upY));
float y = ((_viewZ * _upX) - (_viewX * _upZ));
float z = ((_viewX * _upY) - (_viewY * _upX));
float magnitude = sqrt( (x * x) + (y * y) + (z * z) );
x /= magnitude;
y /= magnitude;
z /= magnitude;
_posX -= x;
_posY -= y;
_posZ -= z;
} Code>
- , "", , .