月球轨道与太阳有关

时间:2013-05-16 17:51:32

标签: c++ opengl rotation

我可以让地球围绕太阳旋转并围绕自己的轴旋转,但我不能让月球围绕地球旋转。 (我只是希望它围绕它循环,我不需要计算重力或类似的东西。)

这是我的代码:

double earth_x = 50.0 * cos(orbit / 180.0 * Math::Constants<double>::pi);
double earth_y = 45.0 * sin(orbit / 180.0 * Math::Constants<double>::pi);
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
glPushMatrix();
glTranslate(earth_x, earth_y, 0.0);
glMaterialAmbientAndDiffuse(GLMaterialEnums::FRONT,GLColor<GLfloat,4>(0.2f,50.0f,50.5f));
glRotatef(110,0.0,23.0,110.0f); 

glRotatef(orbit2, 0.0f, 0.0f,1.0f);
drawPlanetGrid(5, 1, 20, 20, 1.5);

glPopMatrix();

glMaterialAmbientAndDiffuse(GLMaterialEnums::FRONT,GLColor<GLfloat,4>(50.2f,50.0f,50.5f));
//Moon
glTranslate(earth_x+10, earth_y+10, 0.0);
glRotate(moonOrb, 0.0, 0.0, 1.0);

drawEllipsoid(1, 1, 9, 9);  

moonOrb += .5;
if (moonOrb > 360)
moonOrb = 0.0;
orbit += .9;
if (orbit > 360)    
{
    orbit = 0;
}
orbit2 += 6.5;
if (orbit2 > 360)
{
 orbit2 = 0;
}

知道我的代码有什么问题吗? 到目前为止,我的对象上没有纹理,所以这就是代码中缺少的原因。在对尺寸,轨道形状和类似物做出任何改变之前,我真的只是想了解太阳系是如何工作的。

2 个答案:

答案 0 :(得分:3)

假设你做了我在https://stackoverflow.com/a/16594168/252687中推荐的内容,你所要做的就是再次为月亮复制代码。由于天体没有严格约束,因此独立计算轨道更为明智,而不是嵌套它们的参考框架。

地球的位置是:

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

月亮的位置是:

moon_x = earth_x + 15.0 * cos(moon_orbit / 180.0 * PI)
moon_y = earth_y + 15.0 * sin(moon_orbit / 180.0 * PI)

代码看起来像这样:

drawTheSun();

glPushMatrix();  // enter the Earth's frame of reference
glTranslate(earth_x, earth_y, 0.0);  // move to the position of the Earth
glRotate(110, 0.0, 23.0, 110.0f);  // earth-local transformations
drawTheEarth();
glPopMatrix();  // exit the Earth's frame of reference

glPushMatrix();  // enter the Moon's frame of reference
glTranslate(moon_x, moon_y, 0.0);  // move to the position of the Moon
glRotate(110, 0.0, 23.0, 110.0f);  // moon-local transformations
drawTheMoon();
glPopMatrix();  // exit the Moon's frame of reference

答案 1 :(得分:1)

试一试:

#include <GL/glut.h>

void display()
{
    static int lastMs = glutGet( GLUT_ELAPSED_TIME );
    int curMs = glutGet( GLUT_ELAPSED_TIME );
    double dt = ( curMs - lastMs ) / 1000.0;
    lastMs = curMs;

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    double ar = w / h;
    gluPerspective( 60, w / h, 0.1, 100 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( 0, 0, -20 );

    static float earth = 0;
    static float moon = 0;

    earth += 2 * dt;
    moon += 36 * dt;

    glPushMatrix();
    {
        glColor3ub( 255, 255, 0 );
        glutSolidSphere( 4, 8, 8 );

        glPushMatrix();
        {
            glRotatef( earth, 0, 0, 1 );
            glTranslatef( 10, 0, 0 );

            glColor3ub( 0, 255, 255 );
            glutSolidSphere( 1.5, 8, 8 );

            glPushMatrix();
            {
                glRotatef( moon, 0, 0, 1 );
                glTranslatef( 2, 0, 0 );

                glColor3ub( 128, 128, 128 );
                glutSolidSphere( 0.5, 8, 8 );
            }
            glPopMatrix();
        }
        glPopMatrix();
    }   
    glPopMatrix();

    glutSwapBuffers();
}

void timer(int extra)
{
    glutPostRedisplay();
    glutTimerFunc(16, timer, 0);
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "GLUT" );
    glutDisplayFunc( display );
    glutTimerFunc(0, timer, 0);
    glutMainLoop();
    return 0;
}