昨天我問:How could simply calling Pitch and Yaw cause the camera to roll?有人可以解釋我如何使用Quanternions來使用鼠標像FPS一樣環顧四周?
基本上,我發現是因爲「Gimbal Lock」,如果你的俯仰+偏航你將不可避免地產生滾動效果。欲瞭解更多信息,你可以閱讀這個問題。
我試圖阻止這種情況發生。當你在一個普通的FPS射手環顧四周,你沒有讓你的相機在所有地方滾動!
這是我目前的被動鼠標FUNC:
int windowWidth = 640;
int windowHeight = 480;
int oldMouseX = -1;
int oldMouseY = -1;
void mousePassiveHandler(int x, int y)
{
int snapThreshold = 50;
if (oldMouseX != -1 && oldMouseY != -1)
{
cam.yaw((x - oldMouseX)/10.0);
cam.pitch((y - oldMouseY)/10.0);
oldMouseX = x;
oldMouseY = y;
if ((fabs(x - (windowWidth/2)) > snapThreshold) || (fabs(y - (windowHeight/2)) > snapThreshold))
{
oldMouseX = windowWidth/2;
oldMouseY = windowHeight/2;
glutWarpPointer(windowWidth/2, windowHeight/2);
}
}
else
{
oldMouseX = windowWidth/2;
oldMouseY = windowHeight/2;
glutWarpPointer(windowWidth/2, windowHeight/2);
}
glutPostRedisplay();
}
導致相機俯仰/偏航基於鼠標移動(同時保持在中心光標)。我還發布了我的原始相機類here。
該線程中的某個人建議我使用Quaternion來防止這種情況的發生,但在閱讀wikipedia page之後,我根本就不理會它們。
如何在我的OpenGL/Glut應用程序中創建一個四元數,這樣我就可以正確地讓我的「相機」環顧四周而不會產生不必要的滾動?
這不是導致相機滾動的萬向節鎖。萬向節鎖僅在特定情況下發生。通常,在許多舊式FPS中(大多數新式FPS都沒有這個問題),您不能用90°角度觀察天空,以避免會導致玩家只看到天空的萬向節鎖如果你的球員的脖子被阻擋。 查看維基百科文章獲得更多信息。 – rockeye 2009-05-29 12:43:45