2014-10-31 107 views
0

我試圖模擬太陽系,並需要獲得月球公轉的行星我目前使用下面的代碼以旋轉的行星繞太陽公轉旋轉對象不斷旋轉的物體

glPushMatrix(); 
     glRotated((GLdouble)(spin*earth.speed), 0.0, 0.0, 1.0); 
     glTranslated(earth.xPos, earth.yPos, earth.zPos); 
     earth.draw(); 
glPopMatrix(); 

我正在嘗試使用下面的代碼來使我的月球軌道大地,但是此刻我所能做的就是圍繞特定點旋轉。

glPushMatrix(); 
    //define one time only start location 
    bool start = true; 
    if (start) 
    { 
     glTranslated(earthMoon.xPos, earthMoon.yPos, earthMoon.zPos); 
     start = false; 
    } 
    //orbit earths start point 
    //perfectly fits around earth 
     glTranslatef(-0.1, -0.1, 0); 
     glRotatef(spin*10, 0, 0, 1); 
     glTranslatef(0.1, 0.1, 0); 
     // need translation vector to follow earth 


    //glTranslated(earthMoon.xPos, earthMoon.yPos, earthMoon.zPos); 
    earthMoon.draw(); 
glPopMatrix(); 

我想我需要做的是找到某種方式來從rotatef函數中知道地球的位置。

我有行星一類具有以下屬性和方法:

float radius; 
float xPos; 
float yPos; 
float zPos; 
float speed; 
planet(float r, float x, float y, float z, float speed); 
~planet(); 
void draw(void) 
{ 
    glPushMatrix(); 
     glColor3f(0.0, 1.0, 1.0); 
     glutSolidSphere(radius, 20, 10); 
    glPopMatrix(); 
} 

當地球旋轉類的座標沒有更新

有誰知道如何得到這個工作?

回答

0

發現按預期的情況下,任何人的作品的修復與這個概念掙扎

//earth 
    glPushMatrix(); 
     //earth orbit 
     glRotated((GLdouble)(spin*earth.speed), 0.0, 0.0, 1.0); 
     glTranslated(earth.xPos, earth.yPos, earth.zPos); 
      //earth mooon 
     glPushMatrix(); 
      //orbit around earth 
      glRotatef(spin * 5, 0, 0, 1); 
      glTranslatef(0.1, 0.1, 0.0); 
      //rotate around self 
      glRotated((GLdouble)spin, 0.0, 1.0, 0.0); 
      //draw moon 
      earthMoon.draw(); 
     glPopMatrix(); 
     //rotate around self 
     glRotated((GLdouble)spin, 0.0, 1.0, 0.0); 
     //draw earth 
     earth.draw(); 
    glPopMatrix(); 
    // 

希望這有助於其他人

1

不要彈出你的矩陣一旦你畫了一個地球,
那麼你的新的參照關係將是地球的位置, 你只需要調用月球繪圖代碼,它會在你的地球旋轉。

+0

這就是偉大的旋轉大多是圍繞地球然而現在它也經歷了地球的中心點 – 2014-10-31 17:34:48

+0

編輯:它的軌道中心現在,但我似乎無法使軌道足夠小,它不會打其他行星,我可以使它更大,沒有問題,但它只是不會做小控制軌道大小的代碼是 glTranslatef(-0.01,-0.01,0); glRotatef(spin * 10,0,0,1); glTranslatef(0.01,0.01,0); – 2014-10-31 18:02:41

+0

不知道什麼是錯誤的沒有看到它^^ – Chaotikmind 2014-10-31 19:07:12