0
我認爲這將是簡單而直接的,但事實並非如此。我在openGL應用程序中有一個攝像頭,我將顯示的圖像分別轉換爲攝像頭「位置」。位置改變了向上和向下。相機可使用左轉和右轉。當相機旋轉時,前進和後退運動應該不同。這是我已經決定了:分別將相機移動到其位置
//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;
}
原來的旋轉度,但sin()
從math.h
接受弧度。我將結果加1以得到0-2之間的結果。然後,我將sin
或cos
幅度從2改爲1,當我將結果除以2時。
這意味着 - 當相機的視角爲0,90,180,270或360度時,函數應返回0-1值。我對嗎?
開頭的0.5就是移動速度。
謝謝你讓我明白這一點。看起來OpenGL世界有點扭曲,所以我必須做'sin(360角)'才能得到理想的結果。 –