I'm currently doing a model of the solar system. I have a planet that moves around the sun. This is the planet:
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glRotatef(theta * 10, 0.0, 1.0, 0.0);
glTranslatef(P1[0], P1[1], P1[2]);
gluSphere(quad, 0.05, 100, 20);
glPopMatrix();
Now, I want to draw a trial around the sun exactly where the planet moves. How do I do this? I'm supposed to use GL_LINES to draw it. So far I've got this, but I'm not getting the desired result. The circular path isn't exactly the same as the rotation orbit of the planet.
glBegin(GL_LINES);
for (float i = 0; i < 2 * PI; i += 0.01)
{
float x = P1[0] * cos(i) + 0.0;
float y = 0.0;
float z = P1[2] * sin(i) + 0.0;
glVertex3f(x, y, z);
}
glEnd();
Given the information about the planet, how do I draw its orbit trial?
If the planet is following a circular orbit, you need to know the center of the circle, the radius, and the axis of rotation. In your case, the axis of rotation is the y-axis. Therefore the points on along orbit can be computed by the trigonometric functions sin
and cos
Define a center point (float CPT[3]
) and a radius (float radius
). Use the Line primitive type GL_LINE_LOOP
to draw the circular orbit:
glBegin(GL_LINES);
for (float angle = 0; i < 2 * PI; angle += 0.01)
{
float x = CPT[0] + cos(angle) * radius;
float y = CPT[1];
float z = CPT[2] + sin(angle) * radius;
glVertex3f(x, y, z);
}
glEnd();