Game Development Reference
In-Depth Information
Hierarchical Systems with the Matrix Stack
What's a hierarchical system? Our solar system is an example of one. In the center is the sun.
Around the sun are the planets orbiting it at certain distances. Around some planets are one or
more moons that orbit the planet itself. The sun, the planets, and the moons all rotate around
their own centers (sort of). We can build such a system with the matrix stack.
The sun has a position in our world and rotates around itself. All planets move with the sun, so if
the sun changes position, the planets must change position as well. We can use glTranslatef()
to position the sun and glRotatef() to let the sun rotate around itself.
Each planet has a position relative to the sun and rotates around itself as well as around the sun.
Rotating a planet around itself can be done via glRotatef() , and rotating it around the sun can
be done by using glTranslatef() and glRotatef() . Letting the planet move with the sun can be
done by using an additional glTranslatef() .
Each moon has a position relative to the planet it orbits, and rotates around itself as well as
around its planet. Rotating a moon around itself can be done via glRotatef() , and rotating it
around its planet can be done by using glTranslatef() and glRotatef() . Letting the moon
move with its planet can be done by using glTranslatef() . Since the planet moves with the sun,
the moon must also move with the sun, which can again be done via a call to glTranslatef() .
We have so-called parent-child relationships here. The sun is a parent of each planet, and each
planet is a parent of each of its moons. Each planet is a child of the sun, and each moon is a
child of its planet. This means that the position of a child is always given relative to its parent,
not relative to the world's origin.
The sun has no parent, so its position is indeed given relative to the world's origin. A planet is a
child of the sun, so its position is given relative to the sun. A moon is a child of a planet, so its
position is given relative to its planet. We can think of each parent's center as being the origin of
the coordinate system in which we specify that parent's children.
The self-rotation of each of the objects in the system is independent of its parent. The same
would be true if we wanted to scale an object. These things are given relative to their center. This
is essentially the same as the model space.
A Simple Crate Solar System
Let's create a little example, a very simple crate solar system. There is one crate in the center of
the system located at (0,0,5) in the world's coordinate system. Around this “sun� crate, we want
to have a “planet� crate orbiting that sun at a distance of 3 units. The planet crate should also
be smaller than the sun crate, so we'll scale it down to 0.2 units. Around the planet crate, we
want to have a “moon� crate. The distance between the planet crate and the moon crate should
be 1 unit, and the moon crate will also be scaled down, say to 0.1 units. The planet crate and
the moon crate rotate around their respective parent in the x-z plane, and all the objects rotate
around their own y axis. Figure 10-15 shows the basic setup of our scene.
 
Search WWH ::




Custom Search