# OpenGL Detachable hierarchical objects

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

## Recommended Posts

I'm assuming only rotations and translations are used in this. The easiest way to have a hierarchical object is to just store local transforms in the hierarchy, and in rendering the usual OpenGL transforms can be applied then popped when going up the hierarchy. But what if I want to have objects be able to detach and reattach to/from the hierarchy? When detaching, the object should be in world coordinates, with a rotation (quaternion) and translation. How do I extract that from the combined transforms of the hierarchy? Reattaching seems even more complex to me, as in I'm not at all clear how to approach the issue. Suggestions are greatly appreciated.

##### Share on other sites
Quote:
 Original post by PruneHow do I extract that from the combined transforms of the hierarchy?

Just start at the root and multiply the matrices until you get to the leaf (just like OpenGL does). Then store the result in the object's orientation matrix.

To attach just set your local matrix to be = identity * attachPointOffsetMatrix.

-me

##### Share on other sites
Quote:
 Original post by PalidineJust start at the root and multiply the matrices until you get to the leaf (just like OpenGL does). Then store the result in the object's orientation matrix.

How would I extract from that matrix a single translation and single rotation to represent it, when that matrix has resulted from a composition of rotations around different points and directions? I need to extract this since I need to do physical simulation on the detached object.

Quote:
 To attach just set your local matrix to be = identity * attachPointOffsetMatrix.

But, as above, how would I find the local rotation that, when transformed by the sequence of transforms of the hierarchy, makes the object have the same orientation in world coordinate basis as when it was detached?

If hierarchy and global are something like this
R1*T1*R2*T2*R3*T3 <--> R0*T0
First case is how do I decompose the multiplied matrices from the hierarchy into R0 and T0.
Second case is, how do I find R3 and T3... OK the translation seems easy, but what about R3?

##### Share on other sites
Quote:
 Original post by PruneHow would I extract from that matrix a single translation and single rotation to represent it, when that matrix has resulted from a composition of rotations around different points and directions? I need to extract this since I need to do physical simulation on the detached object.

I'm not sure I see the problem. If you can render it you have the necessary matrices. Just multiply them together starting from the root and you'll end up with the appropriate world-relative transform for the attached object

Quote:
 Original post by PruneBut, as above, how would I find the local rotation that, when transformed by the sequence of transforms of the hierarchy, makes the object have the same orientation in world coordinate basis as when it was detached?

This, I suppose is a bit more complex

First, to back up let's make sure we've got the correct architecture:

Root - world-relative matrix (orientation plus translation
|
|
Child - Root-relative orientation matrix (includes root relative translation)
|
|
Child of Child - Child-relative orientation matrix (includes child-relative translation)

So to detach Child of Child:

matrix = rootMatrix * childMatrix * childOfChildMatrix
this now contains the proper world-relative orientation and translation. this is how you're already rendering it so it shouldn't look confusing.

To attach child of child back (now I'm unsure of this [smile]):

matrix = invRootMatrix * invChildMatrix * childOfChildMatrix;
If I'm not mistaken this will transform Child of Child back into Child relative object space (which is what you want).

[EDIT: a little shaky on the math but what you want is inverse(rootMatrix * childMatrix) * childOfChildMatrix. I believe that what I wrote above is equivalent but my matrix math is a tad weak [smile]. If that doesn't work, what you want to do conceptually is: when detaching transform CoC from child relative space into world-relative space; when attaching transform CoC from world-relative space into child-relative space]

-me

##### Share on other sites
OK, that makes sense.
From the composed matrix resulting from the multiplications, I know translation is just entries 13, 14, and 15, but I also need to extract the rotation. I assume that if a transform matrix was composed of only translation and rotation matrix multiplications, I can use the usual matrix to axis angle conversion on the 3x3 submatrix?

##### Share on other sites
Quote:
 Original post by PruneOK, that makes sense.From the composed matrix resulting from the multiplications, I know translation is just entries 13, 14, and 15, but I also need to extract the rotation. I assume that if a transform matrix was composed of only translation and rotation matrix multiplications, I can use the usual matrix to axis angle conversion on the 3x3 submatrix?

I guess... I mean you'll always need to work with the world-relative matrix representation to extract the axis-angle or euler angles.

But, why do you need to extract anything? You'll have the complete orientation matrix, that's all you need to render or perform game logic. You can directly load a matrix in both OpenGL and DirectX for rendering, you don't need axis-angle representations.

-me

##### Share on other sites
I need it for some simulation and animation stuff where it's most convenient.

##### Share on other sites
Quote:
 Original post by PruneOK, that makes sense.From the composed matrix resulting from the multiplications, I know translation is just entries 13, 14, and 15, but I also need to extract the rotation...

You're determined to think in terms of translations and rotations. Stop it. For this sort of thing, all that matters is the 4x4 affine transformation matrix. Translations and rotations are merely an artifact of the transformation. Multiply two transformations, and you get the composition of the two transformations. If you like, you can extract translation and rotation from that, though for most purposes it's not necessary.

##### Share on other sites
It may not matter for rendering, but when specifying a keyframe in an animation to/from which to interpolate it's very useful. It's easier for me to specify, be at such and such angle at this point, then interpolate the rotation to get there from a previous one; likewise for translation. I'm also mixing such animation with simplified simulation in other parts of the timeline where again an orientation conceptually represented in terms of the object's axes which are aligned with the object in physically meaningful ways is useful.

##### Share on other sites
Quote:
 Original post by PruneIt may not matter for rendering, but when specifying a keyframe in an animation to/from which to interpolate it's very useful. It's easier for me to specify, be at such and such angle at this point, then interpolate the rotation to get there from a previous one; likewise for translation. I'm also mixing such animation with simplified simulation in other parts of the timeline where again an orientation conceptually represented in terms of the object's axes which are aligned with the object in physically meaningful ways is useful.

But all you're doing is complicating the math that is more easily done with matrices. What Sneftel is saying is that you already have the perfect tool for this job: a matrix. That you don't yet know the math is irrelivant. Better to learn the appropriate tool than whittling down your square peg so it can go in the round hole. =)

For interpolation you may find quaternions more elegant; but they are easily generated from that same matrix you already have.

A corollary would be trying to determine the angle between a unit's facing orientation and it's target using trigonometry instead of just the dot product of the two vectors. Sure it'll work, but it gets confusing fast and is not easy to implement.

-me

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011524
• ### Who's Online (See full list)

There are no registered users currently online

×