I'm currently working with nodes in a hierarchical scene graph and I'm having difficulty correctly translating/rotating a node relative to a specific transformation space (e.g. a parent node).
How do I properly translate/rotate a node relative to its parent node in a scene graph?
The Problem
Consider the following water molecule diagram (without the connecting lines) for the parent/child structure of the scene nodes, with the Oxygen atom being the parent node and the 2 Hydrogen atoms being the child nodes.
Translation Issue
If you grab the parent Oxygen atom and translate the structure, you expect the Hydrogen children to follow and stay at the same relative position from their parent. If you grab a child H atom instead and translate that, then only the child would be affected. This is generally how it currently works. When O atoms are translated, H atoms automatically move with it, as expected from a hierarchical graph.
However, the when translating the parent, children also end up accumulating an additional translation, which essentially causes the children to 'translate twice' in the same direction and move away from their parent instead of staying at the same relative distance.
Rotation Issue
If you grab the parent O node and rotate it, you expect the children H nodes to also rotate, but in an orbit, because the rotation is being performed by the parent. This works as intended.
However, if you grab a child H node and tell it to rotate relative to its parent, I expected only the child would end up orbiting around its parent in the same way, but this doesn't happen. Instead, the child rotates on its own axis at a faster rate (e.g. twice as fast as rotating relative to its own local space) in its current position.
I really hope this description is fair enough, but let me know if it isn't and I'll clarify as needed.
The Math
I'm using 4x4 column-major matrices (i.e. Matrix4
) and column vectors (i.e. Vector3
, Vector4
).
The incorrect logic below is the closest I've come to the correct behavior. Note that I've chosen to use a Java-like syntax, with operator overloading to make the math easier to read here. I've tried different things when I thought I had figured it out, but I really hadn't.
Current Translation Logic
translate(Vector3 tv /* translation vector */, TransformSpace relativeTo):
switch (relativeTo):
case LOCAL:
localTranslation = localTranslation * TranslationMatrix4(tv);
break;
case PARENT:
if parentNode != null:
localTranslation = parentNode.worldTranslation * localTranslation * TranslationMatrix4(tv);
else:
localTranslation = localTranslation * TranslationMatrix4(tv);
break;
case WORLD:
localTranslation = localTranslation * TranslationMatrix4(tv);
break;
Current Rotation Logic
rotate(Angle angle, Vector3 axis, TransformSpace relativeTo):
switch (relativeTo):
case LOCAL:
localRotation = localRotation * RotationMatrix4(angle, axis);
break;
case PARENT:
if parentNode != null:
localRotation = parentNode.worldRotation * localRotation * RotationMatrix4(angle, axis);
else:
localRotation = localRotation * RotationMatrix4(angle, axis);
break;
case WORLD:
localRotation = localRotation * RotationMatrix4(angle, axis);
break;
Calculating World-Space Transforms
For the sake of completeness, the world transforms for this
node are calculated as follows:
if parentNode != null:
worldTranslation = parent.worldTranslation * localTranslation;
worldRotation = parent.worldRotation * localRotation;
worldScale = parent.worldScale * localScale;
else:
worldTranslation = localTranslation;
worldRotation = localRotation;
worldScale = localScale;
Also, a Node's full/accumulated transformation for this
is:
Matrix4 fullTransform():
Matrix4 localXform = worldTranslation * worldRotation * worldScale;
if parentNode != null:
return parent.fullTransform * localXform;
return localXform;
When a node's transformation is requested to be sent to the OpenGL shader uniform, the fullTransform
matrix is used.