0
\$\begingroup\$

I want to imitate the planetary system with Earth and Sun drawn as solid spheres on the same window. I have drawn the two: the earth as the bigger sphere and the sun as the smaller sphere.

Now I want the earth to revolve around the sun when I press key A and when I press key B the earth must rotate around its axes. How do I achieve this? Thanks.

Here is the code so far:

 #include <GL/glut.h>
 void init(void){
 glClearColor(0.0,0.0,0.0,0.0);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }
 void display(void){
 glClearColor(0.05,0.05,0.5,0.0);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glColor3f(0.03,0.05,0.09);
 glutSolidSphere(0.5,30,30);//Earth
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 glTranslatef(.7,.7,.7);
 glColor3f(1.0,1.0,1.0);
 glutSolidSphere(0.2,50,50);//Sun
 glLoadIdentity();
 glFlush();

 }
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowPosition(10,10);
glutInitWindowSize(400,400);
init();
glutCreateWindow("Planets");
glutDisplayFunc(display);

glFlush();
glutMainLoop();

}
\$\endgroup\$
  • 4
    \$\begingroup\$ Please avoid using deprecated OpenGL! Check out this site for some modern OpenGL tutorials. \$\endgroup\$ – Soapy Nov 12 '15 at 16:05
  • \$\begingroup\$ @Soapy, Hey there is no way I can avoid it because it is one required for my Computer Graphics course!!! I CANNOT oppose what the university says, right? It is not that I like to do deprecated things or something but its a requirement for the course I am doing!!! BTW, its no reason you should have down voted my question, if you don't know the question just leave it, others will answer!! \$\endgroup\$ – Nation Chirara Nov 12 '15 at 17:29
  • 2
    \$\begingroup\$ @Glovanrich you could argue with it considering they're teaching outdated technology, maybe you can impress them with new OpenGL. Also I didn't give you a downvote. \$\endgroup\$ – Soapy Nov 12 '15 at 18:03
  • 1
    \$\begingroup\$ The downvote says "This question does not show any research effort". Your question is considered "how to get started" as you have not shown how you tried to make your spheres spin, and you're basically asking us to write your assignment for you. \$\endgroup\$ – Alexandre Vaillancourt Nov 12 '15 at 18:03
  • \$\begingroup\$ @Soapy It is possible but I hate the "we are aware of that". \$\endgroup\$ – Nation Chirara Nov 16 '15 at 10:21
2
\$\begingroup\$

Lets approach this from outside the code. You are attempting to have user input make on object revolve around another. This means that we have two objects, the fixed object A, and the revolving object B. For A we draw it at one location and it never changes. For object B we start it at an initial position and update it's position every tick based on user input. Let's start with a simple path of movement and have B move on a circle centered at A. Recall that any point on a circle can be looked at as (r cos(x), 0, r sin(x)) where r is the radius and x is the angle, note that our y axis is 0 because we are rotating about it. So every time the user presses a key you can rotate by changing the current angle that object B is at.

Steps make it a bit easier.

Initialize position of objects A and B. Initialize angle of B to be 0.

User Input If (KeyDown = "A") angle x += 1 // degree, radian : your pick else if (KeyDown = "B") angle x -= 1

At Each Tick Draw B at (r cos(x), 0, r sin(x))

\$\endgroup\$
  • \$\begingroup\$ Thanks for response, It is making sense, let me implement it, I will get back to you. \$\endgroup\$ – Nation Chirara Nov 16 '15 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.