• Create Account

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

2 replies to this topic

### #1Zotoaster  Members   -  Reputation: 127

Like
0Likes
Like

Posted 29 October 2013 - 06:29 PM

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?

### #2Paradigm Shifter  Crossbones+   -  Reputation: 4064

Like
0Likes
Like

Posted 29 October 2013 - 07:02 PM

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.

Edited by Paradigm Shifter, 29 October 2013 - 07:06 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #3Aliii  Members   -  Reputation: 679

Like
0Likes
Like

Posted 30 October 2013 - 02:47 AM

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.

PARTNERS