2013-05-16 59 views
3

我正在研究太陽系,我試圖讓地球在太陽附近正確地旋轉。我可以讓它在軌道上旋轉,但是我不能讓它以正確的角度傾斜,如下圖所示: enter image description here地球傾斜openGL C++

我可以傾斜我的星球,但它像這當它走動(它不「面對」相同的方向)

enter image description here 任何想法,我可以做什麼?這裏是我的代碼:

glMatrixMode(GL_MODELVIEW); 
glPushMatrix(); 
//sun 
glMaterialAmbientAndDiffuse(GLMaterialEnums::FRONT,GLColor<GLfloat,4>(1.5f,1.0f,0.0f)); 
glTranslate(0.0f, 0.0f, 0.0f); 
glRotate(0.0, 0.0, 0.0, 00.0); 
drawEllipsoid(10.0, 1.0, 4, 4); 

//Earth 
glRotate(orbit, 0.0, 0.0, 1.0); 
glTranslate(30.0, 0.0, 0.0); 
glPushMatrix(); 
glMaterialAmbientAndDiffuse(GLMaterialEnums::FRONT,GLColor<GLfloat,4>(0.5f,10.5f,10.5f)); 
glRotatef(110,0.0,23.0,110.0f); 
glRotatef(orbit2, 0.0f, 0.0f,1.0f); 
drawPlanetGrid(5, 1, 4, 4, 1); 

glRotate(30.0, 0.0,0.0,0.0); 
glTranslate(orbit2, 0.0, 0.0); 
glPopMatrix(); 
orbit += .2; 

if (orbit > 360)  
{ 
orbit = 0; 
} 
orbit2 += 6.5; 
if (orbit2 > 360) 
{ 
orbit2 = 0; 
} 

抱歉,該代碼未被評論。我只是在試驗它。

+0

我愛球:) –

+0

謝謝...我是用完美的圓形球體,但如果他們被轉動我不知道或不是......所以我這樣做是爲了讓他們知道他們是不是 – TRod

+0

你的基本問題是你不能通過組合轉換來實現這一點(假設原點是明星)。如果您使用旋轉變換來移動行星的質心,則還會使極點旋轉。如果您在定位行星後傾斜行星,用於傾斜的旋轉變換會將其移出黃道平面。 –

回答

3

太陽和地球之間的關係不像分胳膊和手肘那樣是層次結構。他們是獨立的對象。因此,不要使用嵌套矩陣,而應手動計算地球的位置。爲此,您可以使用簡單的三角函數:

earth_x = 30.0 * cos(orbit/180.0 * PI) 
earth_y = 30.0 * sin(orbit/180.0 * PI) 

翻譯到這一點後,你可以以同樣的方式你以前沒有,但這次轉型會不會被地球的位置會影響使用glRotate()翹起地球。

最終,你的代碼應該是這樣的:

drawTheSun(); 

glPushMatrix(); // enter the Earth's frame of reference 
glTranslate(earth_x, earth_y, 0.0); 
glRotate(110,0.0,23.0,110.0f); // tilt however you wanna tilt 
drawTheEarth(); 
glPopMatrix(); // exit the Earth's frame of reference 
+0

等待,earth_x和earth_y會在我的代碼中出現在哪裏? – TRod

+1

你應該用它們來翻譯成地球的參照系。看我的編輯。 –

+0

男人,你超級聰明。我有另一個問題,你可能會幫助:http://stackoverflow.com/questions/16594194/moon-orbit-in-relation-to-sun 謝謝,並感謝幫助我與這一個。有用! – TRod