Greetings,

It has been a while since I have posted and as usual new things yield new questions from me. I am ever at the mercy of the forum gurus. Anywho, I'm making a 3d solar system. One thing that I want to make possible is to turn on and off a set of 9 arrows that point to the planets as they orbit (meaning the arrow's end point would have to follow the orbit as well but it's start point would have to stay in place ideally) I also want to open another window that allows me to view the solar system from the perspective of the moon as it orbits around earth (As if the viewer were the moon themselves). I have the window with the planets in orbit timed properly, and I have the other window open (Right now the the same exact view specified by the original window as I have yet to fill this window with the proper moon view)

My problem is this. I need to get the x,y, and z coordinates of each planet, and additionally of the moon in the solar system, after having done all these rotations and translations, but I don't have a clue how to do that.

Let's say I have a Sun at 0,0,0. I translate a planet out a certain ditance along the x axis. This is okay, because the x,y,z turn into: dist,y,z.

However where I start losing my bearings are when I do any glRotatef.

Now imagine for a moment that the planet has a glRotatef for it's orbit's inclination. So I have to take into account that rotation and get the center of the planet in x,y,z after that rotation along yz plane. Now take it a step further because the planet is in orbit, so it's making another constant rotation to revolve around the sun changing along the xz plane as well updating every 50 ms. That's just for the arrows.

Take it even further now because I still need the view from the moon. I want the view from the moon so I translate earth from the sun, rotate it on it's incline for it's orbit (which actually happens to be 0 incline) then begin revolving it around the sun...now on earth itself I have to translate the moon from the earth, then rotate the moon on its orbits incline, and then revolve that around the earth!! I'm so lost already just thinking about how to track those x,y,z coordinates...

I have recently become aware of glGetFloatv which is supposed to somehow get the float values of a whole modelview matrix, but I am so completely befuddled as to how to effectively use glGetFloatv that I am wondering if it is the only way to track the x,y,z of the planets or if there is an easier way that I can understand. IF someone could help me understand how glGetFloatv works and what other functions i might need to say at each update my planet A is at this exact x,y,z coordinate in the world, then I would greatly appreciate it.

Here I have some of my code to make this more clear

/**
*  function: main
*  returns: int
*  parameters: an int and a char*
*  Description: This will be the main method to drive.
*/

int main(int argc, char **argv)
{

/* deal with any GLUT command Line options */

glutInit(&argc, argv);

// uncomment block for command line prompting
//dealWithParameters(argc, argv);

// uncomment block for stored run execution
string solarFile = "solar.txt";
getInpt(solarFile);

/* create an output window */

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

/* set up the physical window size */

glutInitWindowPosition(0,0);
glutInitWindowSize(800, 800);

/* set the name of the window and try to create it */

mainWin = glutCreateWindow("CS 488 - HW 3");
glutDisplayFunc(mainDisplay);

if (glutGet(GLUT_WINDOW_COLORMAP_SIZE) != 0)
{
exit(1);
}

/* set up for accepting mouse commands */
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutSpecialFunc(skeyboard);

/* set up the logical graphics space */
glLoadIdentity(); //reset all to basic matrix
gluPerspective (45, 1.0, 1.0, 200.0);
setWindow(); //THIS METHOD SETS LIGHTING IN THE WORLD

//switch over to model view to make world transforms
glLoadIdentity(); //reset all to basic matrix

glutInitWindowPosition(800,0);
glutInitWindowSize(400, 400);
subWin = glutCreateWindow("Moon View");
glutDisplayFunc(subDisplay);

/* set up for accepting keyboard commands */

//glutKeyboardFunc(keyboard);

/* set up the logical graphics space */
glLoadIdentity(); //reset all to basic matrix
gluPerspective (45, 1.0, 1.0, 200.0);

setWindow(); //THIS METHOD SETS LIGHTING IN THE WORLD

//switch over to model view to make world transforms
glLoadIdentity(); //reset all to basic matrix

glutTimerFunc(UPDATE_RATE, update, 1);

/* set everything going */
/***********************/

glutMainLoop();

// free up the memory from the quadric
/**
* function: mainDisplay
* returns: void
* parameters: none
* Description: This is the driver to display the main window
*/

void mainDisplay()
{
/* clear screen color */
glutSetWindow (mainWin);
isMain = true;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0); //clear to black with alpha of 1
gluLookAt(0, 0, 20,   0, 0, 0,    0, 1, 0);
glRotatef(-theta, 1.0, 0.0, 0.0);
glRotatef(phi, 0.0, 1.0, 0.0);

display();
//THIS METHOD IS THE SAME AS SUBDISPLAY RIGHT NOW FOR THE //SUBWINDOW
}
/**
*  function: display
*  returns: void
*  parameters: none
*  Description: display function for drawing to the screen
*/

void display(void)
{
//PLANETS IS A VECTOR OF THE PLANETS AND THEIR MOONS
//MOON DRAW DRAWS THE MOONS FOR A GIVEN PLANET
//ORBCIRC DRAWS A CIRCLE THAT DEFINES THE ORBIT

GLfloat	modl[16];
int p = 0;
/* work out the relative speed values */
float sunRotateRate = angle;
float distDivider = (Planets[9].distance-Planets[1].distance)/3;
moonRotateRate = angle*20;

for(int i=0; i< 10; ++i)
{
int p=0;

if(Planets[i].revAngle < 360)
{
Planets[i].revAngle += (0.05/Planets[i].period)*360;
}
else
{
Planets[i].revAngle = 0;
}
pRevolveRate = Planets[i].revAngle;

if(Planets[i].rotAngle < 360)
{
Planets[i].rotAngle += (0.05/Planets[i].rotation)*360;
}
else
{
Planets[i].rotAngle = 0;
}
pRotateRate = Planets[i].rotAngle;

pDist = (Planets[i].distance/distDivider)+0.2;
incline = (Planets[i].incl);
tilt = (Planets[i].tilt);

/* draw scene */

//draws solar system

glPushMatrix();

glScalef(1.0*scale,1.0*scale,1.0*scale);
glTranslatef(xd,yd,0.0);

//draw a specific planet
glPushMatrix();

glRotatef(incline,0.0,0.0,1.0);

switch(i)
{
case 0:
glRotatef(sunRotateRate, 0.0, 1.0, 0.0 );
break;
case 1:
pDist += 0.2;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0);
/*
if(drwAr == true)
{		     		drawArrows(modl[0],modl[5],modl[10],1.0,1.0,0.0);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0);
break;
case 2:
pDist += 0.4;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 3:
pDist += 0.7;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 4:
pDist += 0.9;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 5:
pDist += 1.8;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 6:
pDist += 3.3;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 7:
pDist += 3.6;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 8:
pDist += 3.5;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 9:
pDist += 3.5;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
}

glPopMatrix(); //end nine planet draw

glPopMatrix(); //end drawing of solar system

} //end big for loop

/* swap buffers */
glutSwapBuffers();
}

These are the main, mainDisplay, and of course the display function.

Display()
This where I go through the vector of planets (just a vector of structs with definitions for planet/moon tilt,incline,rotation rates, revolve rates etc.) and I draw each planet and it's subsequent moons a certain distance away from the sun. Notice in mainDisplay I rotate the whole scene as well with the mouse, so I have to take that into account when drawing the arrow.

This is a huge question and I am throwing it out on the forums in hopes that someone reads and hopefully can help...I have little hope, but figured I can't know unless I try. If you require additional info let me know. Thanks in advance to those who might venture to help me. The core question here really is. HOW DO I GET X,Y,Z OF AN OBJECT AFTER ROTATIONS AND TRANSLATIONS HAVE CHANGED IT ALL OVER THE PLACE?

## All 3 Replies

You definitely need to use matrices. Either use glGetFloatv to get the modelview matrix, or you can use your own matrix to keep track of the rotations. Once you've got the matrix, multiply it by the object's original coordinates, and then you'll have the object's coords for world space.

If you don't know what matrices are, I suggest you read this, as it explains very well what matrices are and how to use them.
http://www.geocities.com/SiliconValley/2151/matrices.html

Greetings,

It has been a while since I have posted and as usual new things yield new questions from me. I am ever at the mercy of the forum gurus. Anywho, I'm making a 3d solar system. One thing that I want to make possible is to turn on and off a set of 9 arrows that point to the planets as they orbit (meaning the arrow's end point would have to follow the orbit as well but it's start point would have to stay in place ideally) I also want to open another window that allows me to view the solar system from the perspective of the moon as it orbits around earth (As if the viewer were the moon themselves). I have the window with the planets in orbit timed properly, and I have the other window open (Right now the the same exact view specified by the original window as I have yet to fill this window with the proper moon view)

My problem is this. I need to get the x,y, and z coordinates of each planet, and additionally of the moon in the solar system, after having done all these rotations and translations, but I don't have a clue how to do that.

Let's say I have a Sun at 0,0,0. I translate a planet out a certain ditance along the x axis. This is okay, because the x,y,z turn into: dist,y,z.

However where I start losing my bearings are when I do any glRotatef.

Now imagine for a moment that the planet has a glRotatef for it's orbit's inclination. So I have to take into account that rotation and get the center of the planet in x,y,z after that rotation along yz plane. Now take it a step further because the planet is in orbit, so it's making another constant rotation to revolve around the sun changing along the xz plane as well updating every 50 ms. That's just for the arrows.

Take it even further now because I still need the view from the moon. I want the view from the moon so I translate earth from the sun, rotate it on it's incline for it's orbit (which actually happens to be 0 incline) then begin revolving it around the sun...now on earth itself I have to translate the moon from the earth, then rotate the moon on its orbits incline, and then revolve that around the earth!! I'm so lost already just thinking about how to track those x,y,z coordinates...

I have recently become aware of glGetFloatv which is supposed to somehow get the float values of a whole modelview matrix, but I am so completely befuddled as to how to effectively use glGetFloatv that I am wondering if it is the only way to track the x,y,z of the planets or if there is an easier way that I can understand. IF someone could help me understand how glGetFloatv works and what other functions i might need to say at each update my planet A is at this exact x,y,z coordinate in the world, then I would greatly appreciate it.

Here I have some of my code to make this more clear

/**
*  function: main
*  returns: int
*  parameters: an int and a char*
*  Description: This will be the main method to drive.
*/

int main(int argc, char **argv)
{

/* deal with any GLUT command Line options */

glutInit(&argc, argv);

// uncomment block for command line prompting
//dealWithParameters(argc, argv);

// uncomment block for stored run execution
string solarFile = "solar.txt";
getInpt(solarFile);

/* create an output window */

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

/* set up the physical window size */

glutInitWindowPosition(0,0);
glutInitWindowSize(800, 800);

/* set the name of the window and try to create it */

mainWin = glutCreateWindow("CS 488 - HW 3");
glutDisplayFunc(mainDisplay);

if (glutGet(GLUT_WINDOW_COLORMAP_SIZE) != 0)
{
exit(1);
}

/* set up for accepting mouse commands */
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutSpecialFunc(skeyboard);

/* set up the logical graphics space */
glLoadIdentity(); //reset all to basic matrix
gluPerspective (45, 1.0, 1.0, 200.0);
setWindow(); //THIS METHOD SETS LIGHTING IN THE WORLD

//switch over to model view to make world transforms
glLoadIdentity(); //reset all to basic matrix

glutInitWindowPosition(800,0);
glutInitWindowSize(400, 400);
subWin = glutCreateWindow("Moon View");
glutDisplayFunc(subDisplay);

/* set up for accepting keyboard commands */

//glutKeyboardFunc(keyboard);

/* set up the logical graphics space */
glLoadIdentity(); //reset all to basic matrix
gluPerspective (45, 1.0, 1.0, 200.0);

setWindow(); //THIS METHOD SETS LIGHTING IN THE WORLD

//switch over to model view to make world transforms
glLoadIdentity(); //reset all to basic matrix

glutTimerFunc(UPDATE_RATE, update, 1);

/* set everything going */
/***********************/

glutMainLoop();

// free up the memory from the quadric
/**
* function: mainDisplay
* returns: void
* parameters: none
* Description: This is the driver to display the main window
*/

void mainDisplay()
{
/* clear screen color */
glutSetWindow (mainWin);
isMain = true;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0); //clear to black with alpha of 1
gluLookAt(0, 0, 20,   0, 0, 0,    0, 1, 0);
glRotatef(-theta, 1.0, 0.0, 0.0);
glRotatef(phi, 0.0, 1.0, 0.0);

display();
//THIS METHOD IS THE SAME AS SUBDISPLAY RIGHT NOW FOR THE //SUBWINDOW
}
/**
*  function: display
*  returns: void
*  parameters: none
*  Description: display function for drawing to the screen
*/

void display(void)
{
//PLANETS IS A VECTOR OF THE PLANETS AND THEIR MOONS
//MOON DRAW DRAWS THE MOONS FOR A GIVEN PLANET
//ORBCIRC DRAWS A CIRCLE THAT DEFINES THE ORBIT

GLfloat	modl[16];
int p = 0;
/* work out the relative speed values */
float sunRotateRate = angle;
float distDivider = (Planets[9].distance-Planets[1].distance)/3;
moonRotateRate = angle*20;

for(int i=0; i< 10; ++i)
{
int p=0;

if(Planets[i].revAngle < 360)
{
Planets[i].revAngle += (0.05/Planets[i].period)*360;
}
else
{
Planets[i].revAngle = 0;
}
pRevolveRate = Planets[i].revAngle;

if(Planets[i].rotAngle < 360)
{
Planets[i].rotAngle += (0.05/Planets[i].rotation)*360;
}
else
{
Planets[i].rotAngle = 0;
}
pRotateRate = Planets[i].rotAngle;

pDist = (Planets[i].distance/distDivider)+0.2;
incline = (Planets[i].incl);
tilt = (Planets[i].tilt);

/* draw scene */

//draws solar system

glPushMatrix();

glScalef(1.0*scale,1.0*scale,1.0*scale);
glTranslatef(xd,yd,0.0);

//draw a specific planet
glPushMatrix();

glRotatef(incline,0.0,0.0,1.0);

switch(i)
{
case 0:
glRotatef(sunRotateRate, 0.0, 1.0, 0.0 );
break;
case 1:
pDist += 0.2;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0);
/*
if(drwAr == true)
{		     		drawArrows(modl[0],modl[5],modl[10],1.0,1.0,0.0);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0);
break;
case 2:
pDist += 0.4;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 3:
pDist += 0.7;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 4:
pDist += 0.9;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 5:
pDist += 1.8;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 6:
pDist += 3.3;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 7:
pDist += 3.6;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 8:
pDist += 3.5;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
case 9:
pDist += 3.5;
orbCirc(p,pDist);
glRotatef(pRevolveRate, 0.0, 1.0, 0.0 );
/*
if(drwAr == true)
{
drawArrows(pDist,pDist,pDist,pDist);
}
*/
glTranslatef(pDist, 0.0, 0.0);
moonDraw(moonRotateRate,p,i);
glRotatef(tilt,0.0,0.0,1.0);
glRotatef(pRotateRate, 0.0, 1.0, 0.0 );
break;
}

glPopMatrix(); //end nine planet draw

glPopMatrix(); //end drawing of solar system

} //end big for loop

/* swap buffers */
glutSwapBuffers();
}

These are the main, mainDisplay, and of course the display function.

Display()
This where I go through the vector of planets (just a vector of structs with definitions for planet/moon tilt,incline,rotation rates, revolve rates etc.) and I draw each planet and it's subsequent moons a certain distance away from the sun. Notice in mainDisplay I rotate the whole scene as well with the mouse, so I have to take that into account when drawing the arrow.

This is a huge question and I am throwing it out on the forums in hopes that someone reads and hopefully can help...I have little hope, but figured I can't know unless I try. If you require additional info let me know. Thanks in advance to those who might venture to help me. The core question here really is. HOW DO I GET X,Y,Z OF AN OBJECT AFTER ROTATIONS AND TRANSLATIONS HAVE CHANGED IT ALL OVER THE PLACE?

=============
Hello ...,

I have seen in your code and I am heading to drawing the arrow. Can you send me its code via SNIP. I love your help.

You could just use glPush/Pop Matrix. Makes it much easier.

1) draw sun say at (0,0,0)
2) draw earth at the same position as the sun
3) Translate earth to some radius
4) Now using glRotate on the earth would rotate the earth around
the sun.
5) use glPushMatrix() /** draw moon**/ glPopMatrix()
6) Moon should be draw at earth coodinate
7) Now translate moon to the center and back.
8) Now using glRotate, the moon would rotate around its center
which is at the earth position
9) Translate moon at radius r from earth position
10) Now using glRotate the moon rotates around the earth.
11) Use the same process for other planets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.