Object position after rotation
I have a sphere orbiting another sphere, (say a moon orbiting a planet) using glRotate. How do I determine the xyz positon of the moon at any particular point in its orbit? I need this info for frustum culling and lighting.
the glRotate man page tells you how the rotation matrix is constructed. Multiply your modeling transformation matrices by the object's position to find its final world position.
OneSadCookie Wrote:the glRotate man page tells you how the rotation matrix is constructed. Multiply your modeling transformation matrices by the object's position to find its final world position.
Thanks, OSC, you've pointed me in the right direction. Haven't quite gotten my arms around matrices just yet, but I'll keep digging. I was hoping you wouldn't use 'object's position' in your reply, because that's the unknown piece that I'm looking for.
Best regards,
charon
sorry, the object's original position.
basically, you need to reproduce yourself the steps that OpenGL will take in transforming your object.
basically, you need to reproduce yourself the steps that OpenGL will take in transforming your object.
check out Matricies can be your Friends
http://sjbaker.org/steve/omniv/matrices_...iends.html
And you can see that m[12], m[13], m[14] hold the x, y, z of the translation part. So you could do your rotations and translations normally  then read back the matrix from OpenGL (this is slow!)  and then read m1214 to find out where the planet is.
// This is typed in Safari
// Its not real code
// so it could be wrong
// but I imagine doing something like
GLfloat transform_matrix[16];
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//
// do some transformations  glRotate() and glTranslate() etc...
//
glGetFloatv (GL_MODELVIEW_MATRIX, transform_matrix); //slow!!
GLfloat planetXLocation = transform_matrix[12];
After you get this working  you can substitute your own transformation calls that mirror the OpenGL ones so you no longer have to pull the matrix back from the graphics card if its too slow.
hth,
Codemattic
http://sjbaker.org/steve/omniv/matrices_...iends.html
And you can see that m[12], m[13], m[14] hold the x, y, z of the translation part. So you could do your rotations and translations normally  then read back the matrix from OpenGL (this is slow!)  and then read m1214 to find out where the planet is.
// This is typed in Safari
// Its not real code
// so it could be wrong
// but I imagine doing something like
GLfloat transform_matrix[16];
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//
// do some transformations  glRotate() and glTranslate() etc...
//
glGetFloatv (GL_MODELVIEW_MATRIX, transform_matrix); //slow!!
GLfloat planetXLocation = transform_matrix[12];
After you get this working  you can substitute your own transformation calls that mirror the OpenGL ones so you no longer have to pull the matrix back from the graphics card if its too slow.
hth,
Codemattic
codemattic Wrote:check out Matricies can be your Friends
http://sjbaker.org/steve/omniv/matrices_...iends.html
And you can see that m[12], m[13], m[14] hold the x, y, z of the translation part. So you could do your rotations and translations normally  then read back the matrix from OpenGL (this is slow!)  and then read m1214 to find out where the planet is.
// This is typed in Safari
// Its not real code
// so it could be wrong
// but I imagine doing something like
GLfloat transform_matrix[16];
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//
// do some transformations  glRotate() and glTranslate() etc...
//
glGetFloatv (GL_MODELVIEW_MATRIX, transform_matrix); //slow!!
GLfloat planetXLocation = transform_matrix[12];
After you get this working  you can substitute your own transformation calls that mirror the OpenGL ones so you no longer have to pull the matrix back from the graphics card if its too slow.
hth,
Codemattic
Hi, Codemattic.
For some reason, m[12]>[14] isn't correct after the glGetFloatv. It looks like the camera matrix is factored in. I'm guessing the x,y,z of the orbit should be near 40, 0, 9300. but the orbit matrix shows z at 2600. Adding in the camera matrix[14] value (6776) gets closer to correct, but when i rotate the camera, those values go crazy.
I'm orbiting around 0,0,0.
I'm kinda doing what you have there (I leveraged some code from graphics gems that does postconcatenate of a yaxis rotation (??)  no too happy with the twodim array  will fix later):
...
glTranslate(0,0,9380)
glRotatef(orbit, 0.0f, 1.0f, 0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
cosTheta = cos(DEG_TO_RAD(orbit));
sinTheta = sin(DEG_TO_RAD(orbit));
for(i = 0; i < 4; i++){
t = matrix[i][0];
matrix[i][0] = t * cosTheta + matrix[i][2] * sinTheta;
matrix[i][2] = matrix[i][2] * cosTheta  t * sinTheta;
}
...
after glGetFloatv:
matrix[12] = 21 (makes sense  orbit started out x = 0, except the negative value makes me nervous)
matrix[13] = 0
matrix[14] = 2604 (should be around 9300)
after for loop:
matrix[12] = 358
matrix[13] = 0
matrix[14] = 2579
Am I on the right track or off into the weeds?
thanks,
charon
Quote:It looks like the camera matrix is factored indid you glMatrixMode(GL_MODELVIEW); and glLoadIdentity(); before you translate and rotate?
If you provide a link to a small project which shows the problem Ill be happy to take a closer look at it.
Im going to search my code and see if I can find a better example for you.
codemattic Wrote:did you glMatrixMode(GL_MODELVIEW); and glLoadIdentity(); before you translate and rotate?
If you provide a link to a small project which shows the problem Ill be happy to take a closer look at it.
Im going to search my code and see if I can find a better example for you.
codemattic:
I'm not sure if I can make a small project or clean this up to be legible. I know I'm not breaking any new ground here. As I mentioned earlier  lighting, frustum culling and also collision detection. It would seem to me that finding the position of a rotated body would have been done a gazillion times. But I'm a newbie, what do i know. Thanks for the help.
Here is pretty much the front end:
====
glClear(GL_COLOR_BUFFER_BIT  GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(isViewFront == YES){
glScissor(0, 16, (GLsizei)sceneBounds.size.width, (GLsizei)sceneBounds.size.height);
glEnable(GL_SCISSOR_TEST);
}
glRotatef((float)viewAngle, 0.0, 1.0, 0.0);
// call camera rotate/xlate routine
[[self ship] handleHelm];
glGetFloatv(GL_MODELVIEW_MATRIX, camMatrix);
[[self camera] calculateViewFrustum];
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glColor3f(1.0f, 1.0f, 1.0f);
glPushMatrix();
[[self starSphere] drawStarSphere];
if([[self camera] sphereInFrustum:0.0f y:0.0f z:0.0f
rad:[[self skySphere] radius]] == YES){
[[self planet] drawTexturedSphere];
[[self skySphere] drawSkySphere];
}
glPopMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glPushMatrix();
glRotatef([[self moon] rotate], 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, DISTANCE);
glRotatef([[self moon] orbit], 0.0f, 1.0f, 0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, moonMatrix);
// previously posted code is here
[[self moon] drawMoon];
glPopMatrix();
====
kberg Wrote:This thread might be useful:
http://www.idevgames.com/forum/showthread.php?t=6466
Well, that's certainly something to chew on. THANKS!!!
charon Wrote:I have a sphere orbiting another sphere, (say a moon orbiting a planet) using glRotate. How do I determine the xyz positon of the moon at any particular point in its orbit? I need this info for frustum culling and lighting.
Boy is my face red!! My bad. I seem to have wandered down a path that was totally unnecessary. I've figured out that if the orbit radius is the hypotenuse and the orbit angle is theta, then a little sin/cos acton produces the necessary x, z coordinates. At least I learned a tad more about matrices.
My sincere apologies to this forum and to the helpful contributors for wasting their time. I shall make a modest contribution to this site in the form of a "stupid tax."
Kindest regards,
charon
if your calculating the coordinates yourself after openGL does glRotatef your effectivly doing the same calculations twice... You might as well do the matrix calculations on your coordinates and just drop glRotatef.
If for no other reason Matrix math is essential to 3d programing and this is the perfect excuse to learn it.
If for no other reason Matrix math is essential to 3d programing and this is the perfect excuse to learn it.
There was a long silence...
'I claim them all,' said the Savage at last.
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
SDL. Current music position  e40pud  0  3,141 
Mar 9, 2010 08:46 AM Last Post: e40pud 

Retrieving new object coordinates after translation/rotation  chris69m  4  8,115 
Apr 13, 2008 06:47 PM Last Post: chris69m 

Put a Autocam in Top Position  alert  5  5,100 
Dec 15, 2004 05:37 AM Last Post: Fenris 

Mouse Position In Fullscreen...  thaeez  6  5,332 
Nov 5, 2004 02:38 PM Last Post: thaeez 

Getting position after transformation without drawing?  hyn  19  9,051 
Aug 6, 2004 02:02 PM Last Post: kberg 