I am trying to create a scene graph for my 3D game in which every object's transformations are relative to it's parent. Each node of the graph has a Rotation, Scaling and Translation vector.
What is the correct way of combining the relative transformation matrices to get the absolute transformation of an object? I would be glad if you could also explain your solution.
Here is an example of to do it WRONG:
This actually turned out to be the solution:
Matrix GetAbsoluteTransformation()
{
if (!this.IsRoot())
{
return this.Transformation * this.Parent.GetAbsoluteTransformation();
}
else
{
return this.Transformation;
}
}
In this case, when the parent node is rotated, scaled or moved, the child is transformed the same amount, which is a correct behaviour!
But the child will only rotate around its own origin and does not move around the parent's origin.
Applications:
There is a car model with four wheels. The wheels are relative positioned around the car's origin. The wheels can rotate just like real wheels do. If I now rotate the car, the wheels should at all time stay attached to it. In this case the car is the root and the wheels are the child nodes.
Another example would be a model of the solar system. Planets rotate around their own axis, move around the sun, and moons move around planets.