# Getting absolute position/rotation/scale in a 2D scene graph

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

## Recommended Posts

I have a simple scene graph system. Entities can have child entities, and every update on an entity pushes a matrix, applies the transforms, draws that entity and then updates the child entities, before popping the matrix again. Here's the code using OpenGL:

        glPushMatrix();
glTranslatef(_position.X, _position.Y, 0);
glRotatef(_angle, 0.0f, 0.0f, 1.0f);
glScalef(_scale.X, _scale.Y, 0);

Draw();

for (size_t e = 0; e < _entities.size(); e++)
_entities[e]->Update();

glPopMatrix();


Now I want to be able to access an entity's absolute transformations. The caveat here is that I need to be able to do it outside the push/pop statements, so the matrices will no longer be correct. Entities have access to their parent so you can access those, but that means that every time I want to check an absolute value, I'd have to recursively go up the parents.

Does anyone have any solutions to this?

##### Share on other sites

1) Store the absolute transform with the entity

2) Use the fact that matrix multiplication of transforms e.g. (ABCD)-1 = D-1C-1B-1A-1 where X-1 is the inverse transform for X (which is trivial for scale, rotations and translations). This works for any number of composite matrix multiplications (EDIT: accuracy may be a problem though), I just used 4 as an example. Option 1 is probably best.

##### Share on other sites

In case you need it:

float matrix[16]

glMultMatrixf(   matrix) - apply your own transformation to the current matrix

glLoadMatrixf(  matrix) - set the current matrix for OpenGL

glGetFloatv(     GL_MODELVIEW_MATRIX, matrix) - get the current matrix from OpenGL

So you dont have to go through the push-pops if you have your matrices saved.

• 21
• 12
• 9
• 17
• 13