Solar System Rotation (Orbit) Problem

Hi,

I’ve decided to try and make a small program which simulates the solar system, purely to help me better understand openGL. At the moment I have made an Sun, Earth, Moon system however I cant think of how to make another planet that will orbit the Sun rather than the last model I drew (namely the moon). Part of code for display sun, earth, moon;

 
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

    GLfloat red[] = {1,0.2,0,1};
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
    glutSolidSphere(1.0, 50, 50);

    glRotatef((GLfloat) year, 0.0, 1.0, 0.0);
    glTranslatef(7.0, 0.0, 0.0);
    glRotatef((GLfloat) day, 0.0, 1.0, 0.0);
    GLfloat blue[] = {0,0.2,1,1};
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
    glutSolidSphere(0.2,40,30);

    glRotatef((GLfloat) lunar, 0.0, 1.0, 0.0);
    glTranslatef(0.5,0.0,0.0);
    GLfloat grey[] = {0.7,0.7,0.8,1};
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
    glutSolidSphere(0.05, 100, 30);

    glPopMatrix();
    glutSwapBuffers();
}

I tried to create a point at the center of the earth by translating the reverse of how i created the moon and the again to make a point at the center of the Sun . However this did not work.

Is there a function that can choose where you translate from? Or in other words how do I go about solving this?

Thank you :slight_smile:

Before you draw anything use glPushMatrix then you can use glTranslate and glRotatef. After the draw (glutSolidSphere) you then restore the modelview matrix. There are variations on this theme such as saving the modelview and the use glLoadMatrix instead.

Thanks so much for that I’ve now got all the inner planets. However I’m having trouble adding a second moon to mars. Because obviously glPushMatrix uses the Sun as a reference.

Should I be drawing the moons in a glBegin(); ??

Thanks You

Current code:

<div class=“ubbcode-block”><div class=“ubbcode-header”>Click to reveal… <input type=“button” class=“form-button” value=“Show me!” onclick=“toggle_spoiler(this, ‘Yikes, my eyes!’, ‘Show me!’)” />]<div style=“display: none;”>




void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    GLfloat red[] = {1,0.2,0,1};
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
    glutSolidSphere(10.0, 50, 50);
    glPopMatrix();

    glPushMatrix();
    glRotatef((GLfloat) EarthYear, 0.0, 1.0, 0.0);
    glTranslatef(20.0, 0.0, 0.0);
    glRotatef((GLfloat) day, 0.0, 1.0, 0.0);
    GLfloat blue[] = {0,0.2,1,1};
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
    glutSolidSphere(1,40,30);

    glRotatef((GLfloat) lunar, 0.0, 1.0, 0.0);
    glTranslatef(1.5,0.0,0.0);
    GLfloat grey[] = {0.7,0.7,0.8,1};
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
    glutSolidSphere(0.25, 100, 30);
    glPopMatrix();

    glPushMatrix();
    glRotatef((GLfloat) MercYear, 0.0, 1.0, 0.0);
    glTranslatef(12.0,0.0,0.0);
    GLfloat merc[] = {0.7,0.3,0.2,1};
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, merc);
    glutSolidSphere(0.35, 100, 30);
    glPopMatrix();

    glPushMatrix();
    glRotatef((GLfloat) VenusYear, 0.0, 1.0, 0.0);
    glTranslatef(14.0,0.0,0.0);
    GLfloat venus[] = {0.7,0.7,0.2,1};
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, venus);
    glutSolidSphere(0.9, 100, 30);
    glPopMatrix();

    glPushMatrix();
    glRotatef((GLfloat) MarsYear, 0.0, 1.0, 0.0);
    glTranslatef(25,0.0,0.0);
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, merc);
    glutSolidSphere(0.5, 100, 30);

    glRotatef((GLfloat) lunar, 1.0, 0.0, 0.0);
    glTranslatef(0.0,0.8,0.0);
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
    glutSolidSphere(0.125, 100, 30);
    glPopMatrix();

    glutSwapBuffers();
}


[/QUOTE]</div>

Oh I see what I was doing wrong. Its sorted now, I just need to call glPushMatrix once more. Sorry for asking what turned out to be a very easy problem.