2015-07-19 43 views
1

我具有網格/遊戲對象與glRotatef旋轉,使用FreeGLUT庫:如何使用glRotatef執行關於對象的本地(非全局)軸的旋轉?

glPushMatrix(); 
    glTranslatef(GlobalPos_x, GlobalPos_y, GlobalPos_z); 

    glRotatef(Global_Yaw,0,1,0); 
    glRotatef(Global_Pitch,1,0,0); 
    glRotatef(Global_Roll,0,0,1); 

    RenderMesh(mesh_Plane); 
glPopMatrix(); 

在啓動時,對象與正向Z軸對準; Y軸向上,X軸向左。


'控制':

寬:節距每幀5度

S:節距-5℃下每幀

甲:每幀偏航5度

D:偏角-5度每幀


目標:Global_Yaw,Global_Pitch和Global_Roll值必須以某種方式從這些控件中遞增(範圍從-180到+180,-90到+90和-180到+180)。

問題是,如果飛機已經從 原來的當前位置仰90度,「偏航」反而會使其左右旋轉 Y軸(萬向節鎖定),只應在發生第一 - 人射手。

所以簡單的做'if(GetAsyncKeyState(Key_A))Global_Yaw + = 5;'不是我正在尋找的東西。


編輯:方2

繼tkausl的提醒下,我挖出了我以前的嘗試之一。我的想法是,我會在飛機當前的全局角度轉換成一個矩陣,乘以玩家的按鍵輸入生成的另一個矩陣矩陣,然後以某種方式使用這個矩陣來獲得新的Global_Yaw,Global_Pitch和Global_Roll值:

// Convert ORIENTATION to MATRIX 
Omatrix00 = cos(Global_Pitch)*cos(Global_Yaw); 
Omatrix01 = sin(Global_Pitch); 
Omatrix02 = cos(Global_Pitch)*sin(Global_Yaw); 
Omatrix03 = 0; 
Omatrix10 = -cos(Global_Roll)*sin(Global_Pitch)*cos(Global_Yaw)+sin(Global_Roll)*sin(Global_Yaw); 
Omatrix11 = cos(Global_Roll)*cos(Global_Pitch); 
Omatrix12 = -cos(Global_Roll)*sin(Global_Pitch)*sin(Global_Yaw)-sin(Global_Roll)*cos(Global_Yaw); 
Omatrix13 = 0; 
Omatrix20 = -sin(Global_Roll)*sin(Global_Pitch)*cos(Global_Yaw)-cos(Global_Roll)*sin(Global_Yaw); 
Omatrix21 = sin(Global_Roll)*cos(Global_Pitch); 
Omatrix22 = cos(Global_Roll)*cos(Global_Yaw)-sin(Global_Roll)*sin(Global_Pitch)*sin(Global_Yaw); 
Omatrix23 = 0; 
Omatrix30 = 0; 
Omatrix31 = 0; 
Omatrix32 = 0; 
Omatrix33 = 1; 


// Convert Commanded Turn Angles to MATRIX 
matrix00 = cos(pitch_speed)*cos(yaw_speed); 
matrix02 = cos(pitch_speed)*sin(yaw_speed); 
matrix01 = sin(pitch_speed); 
matrix03 = 0; 
matrix10 = -cos(roll_speed)*sin(pitch_speed)*cos(yaw_speed)+sin(roll_speed)*sin(yaw_speed); 
matrix11 = cos(roll_speed)*cos(pitch_speed); 
matrix12 = -cos(roll_speed)*sin(pitch_speed)*sin(yaw_speed)-sin(roll_speed)*cos(yaw_speed); 
matrix13 = 0; 
matrix20 = -sin(roll_speed)*sin(pitch_speed)*cos(yaw_speed)-cos(roll_speed)*sin(yaw_speed); 
matrix21 = sin(roll_speed)*cos(pitch_speed); 
matrix22 = cos(roll_speed)*cos(yaw_speed)-sin(roll_speed)*sin(pitch_speed)*sin(yaw_speed); 
matrix23 = 0; 
matrix30 = 0; 
matrix31 = 0; 
matrix32 = 0; 
matrix33 = 1; 

// Next step: Somehow using this matrix to get new Global_Yaw, Global_Pitch and Global_Roll values... 

我甚至不確定我是否在我的代碼中正確地增加了我的矩陣。

回答

0

您必須首先圍繞x軸或z軸旋轉,然後Ÿ最後才能獲得正確的結果。在大多數類型的遊戲中,您不需要滾動,您可以圍繞x旋轉,然後圍繞z旋轉,並且所有內容都按預期旋轉。

+0

這並不完全幫助我...我的問題是,Global_Yaw,Global_Pitch和Global_Roll值必須以某種方式從WASD控件增量。 (分別從-180到+180,-90到+90和-180到+180)。不過,我會編輯我的問題以更好地反映我的問題。 –

+0

哦,我想我明白了。在這種情況下,您必須使用四元數和矩陣來防止萬向節鎖定 – tkausl

相關問題