Game Development Reference
In-Depth Information
Listing 10-8. Excerpt from HierarchyTest.java; Implementing a Simple Hierarchical System
class HierarchyScreen extends GLScreen {
Vertices3 cube;
Texture texture;
HierarchicalObject sun;
We only added a single new member to the class, called sun . It represents the root of the object
hierarchy. Since all other objects are stored as children inside this sun object, we don't need to
store them explicitly.
public HierarchyScreen(Game game) {
super (game);
cube = createCube();
texture = new Texture(glGame, "crate.png");
sun = new HierarchicalObject(cube, false );
sun.z = −5;
HierarchicalObject planet = new HierarchicalObject(cube, true );
planet.x = 3;
planet.scale = 0.2f;
sun.children.add(planet);
HierarchicalObject moon = new HierarchicalObject(cube, true );
moon.x = 1;
moon.scale = 0.1f;
planet.children.add(moon);
}
In the constructor, we set up the hierarchical system. First, we load the texture and create the
cube mesh to be used by all the objects. Next, we create the sun crate. It does not have a
parent, and it is located at (0,0,-5) relative to the world's origin (where the virtual camera sits).
Next, we create the planet crate orbiting the sun. It's located at (0,0,3) relative to the sun, and
it has a scale of 0.2. Since the planet crate has a side length of 1 in model space, this scaling
factor will make it render with a side length of 0.2 units. The crucial step here is that we add
the planet crate to the sun crate as a child. For the moon crate, we do something similar. It is
located at (0,0,1) relative to the planet crate, and it has a scale of 0.1 units. We also add it as
a child to the planet crate. Refer to Figure 10-15 , which uses the same unit system, to get a
picture of the setup.
@Override
public void update( float deltaTime) {
sun.update(deltaTime);
}
In the update() method, we simply tell the sun crate to update itself. It will recursively call the
same methods of all its children, which in turn call the same methods of all their children, and so
on. This will update the rotation angles of all objects in the hierarchy.
 
Search WWH ::




Custom Search