月球相对于太阳的轨道

Moon orbit in relation to sun

本文关键字:轨道 太阳 相对于 月球      更新时间:2023-10-16

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

下面是我的代码:
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;
}

你知道我的代码有什么问题吗?到目前为止,我的对象上没有纹理,所以这就是为什么它在代码中缺失的原因。我只是想了解太阳系是如何运作的在我改变大小,轨道形状和类似的东西之前。

假设您按照我在https://stackoverflow.com/a/16594168/252687中建议的那样做了,那么您所要做的就是为Moon再次复制代码。由于天体没有严格的约束,独立计算它们的轨道比嵌套它们的参照系更明智。

地球的位置是:

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

试一试:

#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;
}