Jump to content
  • Advertisement
Sign in to follow this  
Vindicator

OpenGL Planet/moon rotation in OpenGL solar systems

This topic is 4873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone. =) I am trying to create a solar system in OpenGL, and have some trouble with rotation. I have managed to make planets rotate around the sun, by using glRotatef(v, -0.1 , 1 , 0.2); right before I create the sun and all the planet, and placing the sun with the center at the point 0,0,0. However, what I also want to do is to make it possible for moons to rotate around the rotating planets, and making the planets rotate around their own axis (like they do in reality when switching between day and night - one side isn´t always facing the sun). This is the code for how I did to create the solar system:

//creates a planet, and binds the texture if tex isn´t higher than the texture index.
void createPlanet(GLfloat k, int x, int y, int tex) {
 if (tex < MAX_NO_TEXTURES) { glBindTexture(GL_TEXTURE_2D, texture_id[tex]);};
  glutSolidSphere(k, x, y);
  }


//Draws and rotates the solar system.
void createSolarsystem()
{
 glRotatef(v, -0.1 , 1 , 0.2);
 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
 
 createPlanet(2.5, 12, 12, 0); //creates the sun (a bit bigger than the planets, texture 0).
    
 glTranslatef(0.0, 0.0, 6.0);
 createPlanet(1.0, 10, 10, 1); //creates the earth, texture 1, 40% of the sun´s size.)
 glTranslatef(0.0, 0.0, -11.0);
 createPlanet(1.0, 12, 12 , 2); //creates mars, texture 2, the same size as the earth).
}




//display function
void display()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  glRotatef(angleX,1.0,0.0,0.0);
  glRotatef(angleY,0.0,1.0,0.0);
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  createSolarsystem(); //creates the solar system
  glPopMatrix();
  glutSwapBuffers();  
}


If any of you have any ideas about how I could do to rotate planets surface and moons, that would be great. Thanks. =)

Share this post


Link to post
Share on other sites
Advertisement
You have your sun at the center of the solar system
and you gave the earth a matrix that makes it rotate with time.
You also have a similar matrix for the moon, but it's relative to itself (which you don't want) so to do it relativly you just multiply the matrices together (earth*moon)=moonabsolutematrix; this is the matrix that you draw the moon with.


Actually it's not a straight matrix multiplication because the orientation of the earth does not affect the orientation of the moon, so in this planet example you want:


vector3 moonDistance(100.0f, 0.0f, 0.0f); // However far away the moon is from the earth
vector3 absoluteMoonPosition=earthmatrix.transform(moonDistance); // Transform the moon position into world coordinates
vector3 moonRotation= what you want (constant speed rotation)
combine the position and rotation here into a single matrix for the moon


Edit: I hope you can understand this, maybe my description isn't as clear as it should be. Basically you need to multiply vectors by matrices. I'm not very experienced with opengl so I can't post code.

Share this post


Link to post
Share on other sites
Thanks. =)
I´m not quite sure that I understand this.. I´ve failed the course in Linear Algebra both this autumn and the year before that, so that might be why.
Is there some way to automatically generate the matrix with the earth corodinates, so that I can use it in the formulas you described above, to create the moon matrix?
Because all I did was to create the earth, then rotated it around the sun with glRotate - I never changed its position coordinates manually.

Do you know if it is possible to do it by having a rotation around a planet inside the rotation around the sun instead of using matrix multiplication?

Share this post


Link to post
Share on other sites
I had a quick look at some opengl tutorials.
The easiest way is to do:

glPushMatrix()
// draw Earth here
glPushMatrix()
glTranslatef(10.0f, 0.0f, 0.0f); // The distance from earth to moon
// Draw moon here
glPopMatrix();
glPopMatrix();


This method is fine if your planets are totally spherical/symmetrical.
But if not you will see how the rotation of the moon changes with the earths.
I have no idea how to set the rotation not to be multiplied with the previous matrix in OpenGL yet :/
Wait...I think I've got it:


glPushMatrix();
glTranslate(100.0f, 0.0f, 0.0f); // The distance from sun to earth

glPushMatrix();
glTranslate(10.0f, 0.0f, 0.0f); // The distance from earth to moon
glRotatef(.....) // Your rotation for the moon (increment it each frame)
// Draw the moon here
glPopMatrix();

glRotate(....); // The rotation for the earth (increment it each frame)
// Draw the earth here
glPopMatrix();

Share this post


Link to post
Share on other sites
They are spheres... so that won´t be any problem. :)
However, I tried to do like you said:

glPushMatrix();
glTranslatef(0.0, 0.0, 6.0); //earth is 6 away from the sun
glPushMatrix();
glTranslatef(0.0, 0.0, -1.5); //moon is -1.5 away from the center of the earth
glRotatef((1.5 * v), -0.1 , 0.0 , 1); //moon rotation
createPlanet(0.3, 16, 16 , 2); //creates the moon
glPopMatrix();
glRotatef((2 * v), -0.1 , 0.1 , 1); //earth rotation
createPlanet(1.0, 16, 16, 1); //create earth
glPopMatrix();

but it seems like there is still something wrong. The moon is rotating around the sun, just like the earth is doing, and the earth is not rotating either.. so I guess that way didn´t work after all. :i

Share this post


Link to post
Share on other sites
I managed to make it work now. =) It wasn´t the code above which was the big problem, but that I had used both glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); and glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); earlier in the code - if I only use glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP), the textures rotate with the planets (although they look a bit worse, but ýou can barely see that because of the shadows).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!