Results 1 to 4 of 4

Thread: Solar System Rotation (Orbit) Problem

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    6

    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;

    Code :
    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

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Solar System Rotation (Orbit) Problem

    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.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    6

    Re: Solar System Rotation (Orbit) Problem

    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;">
    Code :
     
     
    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>

  4. #4
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    6

    Re: Solar System Rotation (Orbit) Problem

    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.

Similar Threads

  1. Virtual Solar System, new MVP matrix system
    By Mr. Mipada in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-12-2018, 02:46 PM
  2. Solar System Rotation/Translation
    By carden2 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-17-2013, 03:24 PM
  3. Solar System Orbit
    By Corwin in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-24-2006, 05:45 AM
  4. Solar System Orbit
    By Corwin in forum OpenGL: General
    Replies: 3
    Last Post: 04-24-2006, 05:38 AM
  5. help with solar system
    By in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 11-07-2003, 05:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean