0

Hello I am trying to orbit the earth around the sun in an orbit path.What I have done so far draw two cirlces one the sun and the earth.The earth is on the ellipse.Tried to use glutTimerFunc() to create an animation but no result, here is the code.How is it possible to make earth orbit around the sun in an ellipse path.

#include <gl/glut.h>
#include <math.h>
#define PI 3.14159
#define circlePoints 256
#define ellipsePoints 256 
int i;

/*void myWireSphere(GLfloat radius, int slices, int stacks) {
  glPushMatrix();
  glRotatef(-90.0, 1.0, 0.0, 0.0);
  glutWireSphere(radius, slices, stacks);
  glPopMatrix();
}*/
void circle(){
        glColor3f(1,1,0);
        GLfloat angleStep=2*PI/(float)circlePoints;
    GLuint pointsPerQuarter=circlePoints/4;
    GLfloat x[circlePoints];
    GLfloat y[circlePoints];
    GLfloat radius=3;
    for(i=0;i<pointsPerQuarter/2;i++)
    {
        //Define points in first quadrant
        x[i]=radius*cos(i*angleStep);
        y[i]=radius*sin(i*angleStep);
        x[pointsPerQuarter-1-i]=y[i];
        y[pointsPerQuarter-1-i]=x[i];
        //Define points in second quadrant
        x[pointsPerQuarter+i]=-y[i];
        y[pointsPerQuarter+i]=x[i];
        x[2*pointsPerQuarter-1-i]=-x[i];
        y[2*pointsPerQuarter-1-i]=y[i];
        //Define points in third quadrant
        x[2*pointsPerQuarter+i]=-x[i];
        y[2*pointsPerQuarter+i]=-y[i];
        x[3*pointsPerQuarter-1-i]=-y[i];
        y[3*pointsPerQuarter-1-i]=-x[i];
        //Define points in fourth quadrant
        x[3*pointsPerQuarter+i]=y[i];
        y[3*pointsPerQuarter+i]=-x[i];
        x[4*pointsPerQuarter-1-i]=x[i];
        y[4*pointsPerQuarter-1-i]=-y[i]; 
    }
    
    glBegin(GL_LINE_LOOP);
    for (i=0;i<circlePoints;i++)
    {
        glVertex2f(x[i],y[i]);
    }
    glEnd();
    
}
void circlearth(){
        glColor3f(0,0,1);
        GLfloat angleStep=2*PI/(float)circlePoints;
    GLuint pointsPerQuarter=circlePoints/4;
    GLfloat x[circlePoints];
    GLfloat y[circlePoints];
    GLfloat radius=1;
    for(i=0;i<pointsPerQuarter/2;i++)
    {
        //Define points in first quadrant
        x[i]=radius*cos(i*angleStep);
        y[i]=radius*sin(i*angleStep);
        x[pointsPerQuarter-1-i]=y[i];
        y[pointsPerQuarter-1-i]=x[i];
        //Define points in second quadrant
        x[pointsPerQuarter+i]=-y[i];
        y[pointsPerQuarter+i]=x[i];
        x[2*pointsPerQuarter-1-i]=-x[i];
        y[2*pointsPerQuarter-1-i]=y[i];
        //Define points in third quadrant
        x[2*pointsPerQuarter+i]=-x[i];
        y[2*pointsPerQuarter+i]=-y[i];
        x[3*pointsPerQuarter-1-i]=-y[i];
        y[3*pointsPerQuarter-1-i]=-x[i];
        //Define points in fourth quadrant
        x[3*pointsPerQuarter+i]=y[i];
        y[3*pointsPerQuarter+i]=-x[i];
        x[4*pointsPerQuarter-1-i]=x[i];
        y[4*pointsPerQuarter-1-i]=-y[i]; 
    }
    
    glBegin(GL_LINE_LOOP);
    for (i=0;i<circlePoints;i++)
    {
        glVertex2f(x[i],y[i]);
    }
    glEnd();
    
}

void ellipse(){
    glColor3f(0,0,0);
    GLfloat angleStep=2*PI/(float)ellipsePoints; 
    GLuint pointsPerQuarter=ellipsePoints;///4; 
    GLfloat x[ellipsePoints]; 
    GLfloat y[ellipsePoints]; 
    GLfloat rx=15; 
    GLfloat ry=10; 
    glBegin(GL_LINE_LOOP); 
        for(i=0;i<pointsPerQuarter;i++) 
        { 
            x[i]=rx*cos(i*angleStep); 
            y[i]=ry*sin(i*angleStep); 
        } 
        
        for(i=0;i<ellipsePoints;i++) 
        { 
            glVertex2f(x[i],y[i]); 
        } 
    glEnd(); 
}

void reshape(int w, int h)
{


    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-32,32,-24,24);
    glMatrixMode(GL_MODELVIEW);
}


void timer(int value)
{
    glutPostRedisplay();
    glutTimerFunc(500, timer, 0);
}

void init(void)
{
    glClearColor(1,1,1,0);
    glColor3f(0, 0, 0);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLineWidth(3);
    ellipse();
    glPushMatrix();
    glTranslatef(-7.0, 0.0, 0.0);
    circle();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(15.0, 0.0, 0.0);
    circlearth();
    glPopMatrix();
    glFlush();
    glutSwapBuffers(); // double buffer
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitWindowPosition(50,50);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480); 
    glutCreateWindow("Earth-Sun");
    glMatrixMode(GL_PROJECTION); 
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutTimerFunc(0000, timer, 0);
    glutMainLoop(); 
    return 0; 
}
Chris Loonam
  • 5,735
  • 6
  • 41
  • 63
therac25
  • 9
  • 2
  • Don't you need a `glutIdleFunc` to turn the new time into a new xy-position for the earth? The `...translate(12,0,0)` has to change in each frame. The idle func updates x_earth and y_earth, then calls postdiplay, and display has 'translate(x_earth. y_earth. 0)`. –  May 07 '21 at 10:40
  • see [Is it possible to make realistic n-body solar system simulation in matter of size and mass?](https://stackoverflow.com/a/28020934/2521214) and the sublinks especially [Solving Kepler's equation](https://stackoverflow.com/a/25403425/2521214) – Spektre May 08 '21 at 06:24

1 Answers1

0

I made the global vars:

double xea = 12, yea = 0;
double ang = 0;

This glutIdleFunc:

   void idle() {
        xea = 20 * sin(ang);
        yea = 10 * cos(ang);
        ang += 0.0001;
        glutPostRedisplay();
    }

And glutIdleFunc(idle); to registrate.

And changed earth's positioning in display func:

glTranslatef(xea, yea, 0.0);

And <gl/glut.h> wanted <GL/glut.h>. Hmm, who is right?

Now the earth smoothly moves, more or less on the ellipse.

But: I noticed a high power consumption (fan instatntly active), compared to simple "modern" OpenGL programs with glDrawArrays(), also with animation.

With a manual angle increment in a keyboard callback (instead of automatic 60 Hz in idle callback) this should be less of a problem.

  • Just a pedantic note: **this is not physically correct** as the speed of the earth does not match what it should ... you know near sun its faster ... and has constant `area/time` coverage. See links in my comment above. To remedy you should measure elapsed time and recompute `M` angle from it and `tropical year` period and `epoch` after that compute `E` angle using Kepler's equation and convert to Cartesian. Also using naive parametric equation like you did means sun is not at its center and need to be translated. – Spektre May 08 '21 at 06:32
  • @Spektre This is very true. I only added *some* animation in a very simple way. If you want more sophistication you can always extend the body of the `idle` function. So yes, it's rather elementary than pedantic but why tell *me* under *this* Q? n-body! –  May 08 '21 at 07:58