# Calculate Position of an Object after Performing Tranlations (OpenGL)

Basically I have written a Cube class. It has a member variable that determines the location it should be drawn in. I set the coordinates then call its Draw method. This Pushes the current matrix onto the stack, moves to correct position (glTranslatef) draws the cube, then pops the matrix back off the stack.

This all works fine, but elsewhere in my code I have done other translations (a rotate) and I could like to  calculate what the co-ordinate of the cube would be after the translation.

So..:
1. Am i doing this the wrong way? Should I not be using glTranslatef and manuallycalculate the position of the cube using a custom matrix and redraw it?
2. Could I multiply the coordintes of the cube by somthing to get its new position? Is there a function for this.

I'm terribly confused! Hope i have explained things OK.
translation.gif
LVL 2
Asked:
###### Who is Participating?

Commented:
hey Jamsb,

1. >> Am i doing this the wrong way? Should I not be using glTranslatef and manuallycalculate the position of the cube using a custom matrix and redraw it?

not necessarily, depends on how you work with your scene

2. >> Could I multiply the coordintes of the cube by somthing to get its new position? Is there a function for this.
yes, there is an opengl-function to retreive the current modelview-matrix (the matrix which moves the model from modelspace to worldspace, with other words the top modelview-matrix on the stack). you can get that matrix with:

GLfloat matrix[16];
glGetFloatv( GL_MODELVIEW_MATRIX, matrix);

now multiply that matrix with the points in objectspace of your model, then you have its points in worldspace

ike
0

Author Commented:
That meant to say "should I not be using a glRotatef".
0

Author Commented:
ikework:

Thankyou I was wondering if that was what I was supposed to do!
0

Commented:
you're welcome .. :)  but be aware that retreiving that matrix from the graphics card can be slow, since it has to be transfered from the graphics-card-pipeline back to mainmemory .. it could be more efficient to store all the previous transformations in a cache and get the current matrix from there. but that depends on your application

good luck :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Already a member? Login.

All Courses

From novice to tech pro — start learning today.