Sign in to follow this  
Garra

simple solar system problem

Recommended Posts

Garra    103
i'm working on a modified version of the redbook solar system code. Pretty much i just added a moon that rotates around the earth. My problem is i need it to rotate around the earth every 28 days and haven't made it work correctly yet. If anyone can help please do.
#include <GL\glut.h>

static int year = 0, day = 0, mday = 0;

void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT);
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);

	glPushMatrix();
		glColor3f(1, 1, 0);
		glutWireSphere(1.0, 20, 16); /* draw sun */
		glRotatef((GLfloat) year, 0.0, 1.0, 0.0);
		glTranslatef(2.0, 0.0, 0.0);
		glRotatef((GLfloat) day, 0.0, 1.0, 0.0);
		glColor3f(0, 1, 0);
		glutWireSphere(0.3, 10, 8); /* draw earth planet */ 

		glRotatef((GLfloat) year, 0.0, 1.0, 0.0);
		glTranslatef(0.7, 0.0, 0.0);
		glRotatef((GLfloat) day, 0.0, 1.0, 0.0);
		glColor3f(1, 1, 1);
		glutWireSphere(0.1, 10, 8); /* draw moon */ 
	glPopMatrix();

	glutSwapBuffers();

}
void reshape(int w, int h)
{
	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}


void keyboard(unsigned char key, int x, int y)
{
	switch (key) 
	{
	case 'd':
	day = (day + 1) % 360;
	glutPostRedisplay();
	break;
	case 'D':
	day = (day + 10) % 360;
	glutPostRedisplay();
	break;
	case 'y':
	year = (year + 5) % 360;
	glutPostRedisplay();
	break;
	case 'Y':
	year = (year - 5) % 360;
	glutPostRedisplay();
	break;
	default:

	break;
	}
}
int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(700, 400);
	glutInitWindowPosition(100, 100);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutMainLoop();
	return 0;

}

Share this post


Link to post
Share on other sites
Lord_Evil    680
What doesn't work correctly? I guess it's the movement of the moon, so I'll try and answer that:

The problem seems that your transformations don't match:

first you rotate earth around the sun by 'year', then you move it out by '2' and rotate it around its axis by 'day'

next, you rotate the moon around earth by another 'year', move it out by '0.7' and rotate it around itself by 'day'

You'd have to change the moon transformation to rotate around eart by another variable, let's say 'month', which can be calulated to be day/28. Additionally, you might want to rotate the moon around its axis by yet another variable in order to not make it spin with the same speed as earth.

Share this post


Link to post
Share on other sites
Mussi    4409
The earth rotates around it's axis in a day, and about the sun in a year, so you should change the order of your transformations, first rotate the earth for the value of day, then move it out by '2.0' and then rotate it by a year.

Share this post


Link to post
Share on other sites
Lord_Evil    680
No, using fixed function you'll have to first rotate around the sun, then move earth out and finally rotate around earths local axis (and so on with the moon).

From the OpenGL FAQ (No. 9.070):
The root cause of the problem is that OpenGL matrix operations postmultiply onto the matrix stack, thus causing transformations to occur in object space.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this