I thought this will be simple and straightforward, but it is not. I have a camera in openGL application and I am transforming the displayed image respectively to camera "location". Location is changed by Up and Down. Camera may be rotated using Left and Right. When the camera is rotated, the forward and backvard movement should be different. This is what I've made up:
//Somewhere in event handler switch
else if (key == GLUT_KEY_UP|| key == GLUT_KEY_DOWN) {
char direction = key==GLUT_KEY_UP?1:-1; //Whether it is forward or backward movement
std::cout<<"Moving camera in direction "<<(int)direction<<" with angle "<<rotate_y<<'\n';
std::cout<<" - this means X is multiplied by "<<((sin((rotate_y/180)*M_PI)+1)/2)<<'\n';
camera_x += 0.5*((sin((rotate_y/180)*M_PI)+1)/2)*direction;
camera_z += 0.5*((cos((rotate_y/180)*M_PI)+1)/2)*direction;
}
The original rotation is in degrees, but sin()
from math.h
accepts radians. I'm adding 1 to the result to get results between 0-2. Then I change the sin
or cos
amplitude from 2 to 1, when I divide the result by 2.
This means - when the camera is looking by 0, 90, 180, 270 or 360 degrees, the functions should return 0-1 values. Am I right?
The 0.5 at the beginning is just movement speed.