2014-10-31 76 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