## Hierarchical Systems with the Matrix Stack

What's a hierarchical system? Our solar system is an example of one. In the center we have the sun. Around the sun are the planets orbiting it at certain distances. Around some planets we find moons that orbit the planet itself. And the sun, the planets, and the moons each 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 it rotate around itself.

The planets have a position relative to the sun and rotate around themselves, as well as around the sun. Rotating the planet around itself can be done via glRotatef(), and rotating it around the sun can be done using glTranslatef() and glRotatef (). Letting the planet move with the sun can be done by an additional glTranslatef().

The moons have a position relative to the planet they orbit and rotate around themselves, as well as around their planet. Rotating the moon around itself can be done via glRotatef(), and rotating it around the planet can be done by glTranslatef() and glRotatef(). Letting the moon move with the planet can be done by glTranslatef(). And since the planet moves with the sun, the moon also has to 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 moon. 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, and a moon is a child of a planet, so its position is given relative to the planet. You can think of each parent's center being the origin of the coordinate system that we specify a parent's children in.

The self-rotation of each of the objects in our 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. We have one crate in the center of the system located at (0,0,5) in our world's coordinate system. Around this "sun" crate, we want to have "planet" crate orbiting that sun at a distance of 3 units. The planet crate should also be smaller than the sun crate; let's say we 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. All the objects rotate around their respective parent in the x-z plane and also around their own y-axes. Figure 10-14 shows the basic setup of our scene.

 \ n • c ß n V»1.1 u V 1 *
0 0