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.
I don't think you want the output of sin
/cos
to be in the range 0 - 1.
If you're trying to generate a movement vector in a particular direction, then you need to be generating points in a circle around the origin, which is exactly what you have before applying the scale and offset. Instead you're generating a circle around the point 0.5,0.5, which if used as direction vectors are probably pointing in the wrong direction:
Direction Angle Vector Your Vector Actual Direction N 0 0, 1 0.5, 1 ~NNE E 90 1, 0 1 , 0.5 ~ENE S 180 0, -1 0.5, 0 E W 270 -1, 0 0 , 0.5 N
Notice that you can only generate directions in one quadrant (because your result is always positive) and that none of them are quite what you wanted.