0

I have been asked to design and animate a solar system in openGL. I am doing so in C. I am a bit confused as to exactly how I should go about animating the orbits. How should I be incrementing the rotation angle for each planet to control the speed of it's orbit around the sun?

Here is all of the code I have written so far, just trying to take it step by step:

#include <GL/glut.h>
#include <GL/glu.h>

#define FACTOR 30.0
#define SLICES 25
#define STACKS 25

//Viewing angle variables
int eye_x = 2.0;
int eye_y = 3.0;
int eye_z = 10.0;
int up_x = 0.0;
int up_y = 1.0;
int up_z = 0.0;

//Planet diameters in relation to earth
double sun_radius = 100.0;
double earth_radius = 1.0;
double moon_radius = 0.2724;
double mercury_radius = 0.383;
double venus_radius = 0.949;
double mars_radius = 0.532;
double jupiter_radius = 11.21;
double saturn_radius = 9.45;
double uranus_radius = 4.01;
double neptune_radius = 3.88;
double pluto_radius = 0.187;

//Planet distances from sun in relation to earth's distance
double mercury_distance = (sun_radius / FACTOR) + 0.387;
double venus_distance = mercury_distance + 0.723;
double earth_distance = venus_distance + 1.0;
double mars_distance = earth_distance + 1.52;
double jupiter_distance = mars_distance + 5.20;
double saturn_distance = jupiter_distance + 9.58;
double uranus_distance = saturn_distance + 19.20;
double neptune_distance = uranus_distance + 30.05;
double pluto_distance = neptune_distance + 39.24;

/**
 * Init function initializing the background to black.
 */
void init()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glOrtho(-100.0, 100.0, -100.0, 100.0, -100.0, 100.0);
    glMatrixMode(GL_MODELVIEW | GL_PROJECTION);
    glEnable(GL_DEPTH_TEST);

    gluLookAt(eye_x, eye_y, eye_z, 0.0, 0.0, 0.0, up_x, up_y, up_z);
}

/*
void stars()
{
    int noOfStars = rand() % 10;
    int i = 0;

    while(i < noOfStars)
    {
        glColor3f(1.0, 1.0, 1.0);
        glPointSize(20.0f);

        int x = rand() % 10;
        int y = rand() % 10;
        int z = -8.0;

        glBegin(GL_POINTS);
            glVertex3f(x, y, z);
        glEnd();

        i++;
    }

    glFlush();
    glutSwapBuffers();
}
*/

void display()
{   
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    //stars();

    //"Zoom out"
    glTranslatef(0.0, 0.0, -20.0);

    //Draw sun
    glColor4f(1.0, 0.5, 0.0, 0.3);
    glutWireSphere(sun_radius / FACTOR, SLICES, STACKS);    

    //Draw mercury
    //Rotate around sun
    //glRotatef(, 0.0, 1.0, 0.0);
    //Distance from sun to mercury
    glTranslatef(mercury_distance, 0.0, 0.0);
    glPushMatrix();
        //glRotatef( , 0.0, 1.0, 0.0);
        glColor4f(1.0, 0.75, 0.75, 0.3);
        glutWireSphere(mercury_radius, SLICES, STACKS);
    glPopMatrix();

    /*
    //Draw venus
    //Distance from sun to venus
    glTranslatef(venus_distance, 0.0, 0.0);
    glPushMatrix();
        glColor4f(1.0, 0.75, 0.75, 0.3);
        glutWireSphere(venus_radius, SLICES, STACKS);
    glPopMatrix();

    //Draw earth
    //Distance from sun to earth
    glTranslatef(earth_distance, 0.0, 0.0);
    glPushMatrix();
        glColor4f(1.0, 0.75, 0.75, 0.3);
        glutWireSphere(earth_radius, SLICES, STACKS);
    glPopMatrix();
    */

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
   glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowPosition(0,0);
    glutInitWindowSize(1000, 1000);
    glutCreateWindow("solar system");

    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();

    return 0;
}   
genpfault
  • 51,148
  • 11
  • 85
  • 139
user3579421
  • 45
  • 2
  • 8
  • look here http://stackoverflow.com/a/28020934/2521214 and here http://stackoverflow.com/a/25403425/2521214 – Spektre Jan 29 '15 at 09:33

1 Answers1

0

First, there is not a single distance from the sun for any of the planets - they move in a Kepler ellipse. (Earth was closest to the sun two weeks ago at the beginning of January and will be farthest away in summer.)

Accordingly, the position angle of a planet will not change at a constant rate, if you want to do this accurately. You can of course simplify and say that planetary orbits are close enough to circles for this not to matter (and say that Pluto is not a planet any more, so it even doesn't matter there).

But let's go for an exact solution: This is governed by Newton's law:

F = g * M_1 * M_2 / r^2 // How do you put equations here?

and energy conservation, trading kinetic energy E = M v^2 / 2 for potential energy E = - g * M_1 * M_2 / r

So, to simulate the motion of a planet around the sun, get its position, its velocity and the gravitational force acting upon it, calculate where you end up one time step later, calculate the new velocity and force acting on it, and repeat. (Do the same for all planets and ignore gravitational interactions between them for the moment.)

That would be an actual simulation of the solar system. If you only want to simulate the positions at any given time, look up Keplers laws - essentially a consequence of applying Newtons law.


I just saw that the article above even has a section on "Position as a function of time" - so that should help you with the algorithm.

Alex
  • 336
  • 3
  • 11
  • Thank you very much Alex! Your answer was very helpful. I think at first I am going to try for a simpler solution assuming the orbits are circles, and then try for the ellipse. I am not quite sure how I could implement those equations into openGL effectively, since I am very new to this library, but I will definitely give it a shot! Thank you again Alex! – user3579421 Jan 19 '15 at 21:00
  • Using circles of course simplifies the equations. If you only consider the 8 major planets, you can even revert to 2D orbits, since all the large planets move in roughly the same plane. [Wikipedias Solar system article](http://en.wikipedia.org/wiki/Solar_System) says "Most large objects in orbit around the Sun lie near the plane of Earth's orbit, known as the ecliptic. " So you only have one angle to worry about, and that one changes at a constant rate for each planet. – Alex Jan 23 '15 at 14:35
  • @alex the inclination of major bodies are few degrees but on that huge distance it get visible on the night sky .... look at the images on my first link in OP comment of mine the lines are major bodies trajectories seen from Earth's surface. on view from outside Earth it is not that obvious – Spektre Jan 29 '15 at 09:36