Rotate the Earth in elliptical path :OpenGL

Anonymous

I need to revolve Earth around the Sun in OpenGL. I have currently been able to revolve it around the Sun, but I need to revolve it in an elliptical orbit. Now the ellipse has been produced but I can't figure out how to revolve it along that ellipse.

Draw function looks like:

    //orbit
    glColor3f(1,1,1); 
    drawEllipse(3.2f,1.0f); //draws ellipse 
    //sun
    glTranslatef(0,0,0);//suns position
    glColor3d(1,1,0);
    glutSolidSphere(2,50,50);

    //EARTH
    glRotatef(angle,0.0f,1.0f,0.0f);
    glTranslatef(6.0f,0.0f,0.0f);//postion earths


    glPushMatrix();
    glRotatef(angle2, 0.0f,1.0f,0.0f);
    glColor3d(0.5,0.8,1);

    glutSolidSphere(1,50,50);
    glPopMatrix();
Nabin
OpenGL does not store what you draw. If you draw a line in OpenGL, then OpenGL 
will take that line, perform various math operations on it, and write pixels
 into a framebuffer that makes the shape of a line. OpenGL does not remember
 that you drew a line; all OpenGL can do is write pixels to the framebuffer.

So you can draw sphere at each point on ellipse.

Define two variables that tracks the (x,y) co-ordinates for translating the Earth along the ellipse

float xForEarth, yForEarth;

And a counter that counts the degree (from 0 to 360 and then again to 0)

int counterForEarth = 0;

And use the following code (inside your drawing method) to make Ellipse and draw Earth on it:

glBegin(GL_POINTS);
    for (int i=0; i < 360; i++) 
    { 
        float degInRad = i*DEG2RAD; //const float DEG2RAD = 3.14159/180.0;
        glVertex3f(cos(degInRad)*6.0, 
            sin(degInRad)*2.3,0.0f);
    }
    glEnd();
    if(counterForEarth>359)
        counterForEarth = 0;//reset to 0 when becomes 360
    else
        counterForEarth++;//this will control the speed. Do counterForEarth += 2 to increase it's speed and vice-versa

    xForEarth = cos(counterForEarth*DEG2RAD)*6.0f;//to change the x co-ordinate
    yForEarth = sin(counterForEarth*DEG2RAD)*2.3f;//to change the y co-ordinate
    glPushMatrix();
    glTranslatef(xForEarth,yForEarth,0.0f);
    glRotatef(angle,0.0f,1.0f,0.0f);
    glutSolidSphere(.4,30,30);//draw Earth
    glPopMatrix();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related