Game Development Reference
In-Depth Information
public boolean hasParent;
public final List<HierarchicalObject> children = new ArrayList<HierarchicalObject>();
public final Vertices3 mesh;
The first three members encode the position of the object relative to its parent (or relative
to the world's origin if the object has no parent). The next member stores the scale of the object.
The rotationY member stores the rotation of the object around itself, and the rotationParent
member stores the rotation angle around the parent's center. The hasParent member indicates
whether this object has a parent or not. If it doesn't, then we don't have to apply the rotation
around the parent. This is true for the “sun� in our system. Finally, we have a list of children,
followed by a reference to a Vertices3 instance, which holds the mesh of the cube we use to
render each object.
public HierarchicalObject(Vertices3 mesh, boolean hasParent) {
this .mesh = mesh;
this .hasParent = hasParent;
}
The constructor just takes a Vertices3 instance and a Boolean indicating whether this object
has a parent or not.
public void update( float deltaTime) {
rotationY += 45 * deltaTime;
rotationParent += 20 * deltaTime;
int len = children.size();
for ( int i = 0; i < len; i++) {
children.get(i).update(deltaTime);
}
}
In the update() method, we first update the rotationY and rotationParent members. Each
object will rotate by 45 degrees per second around itself and by 20 degrees per second around
its parent. We also call the update() method recursively for each child of the object.
public void render(GL10 gl) {
gl.glPushMatrix();
if (hasParent)
gl.glRotatef(rotationParent, 0, 1, 0);
gl.glTranslatef(x, y, z);
gl.glPushMatrix();
gl.glRotatef(rotationY, 0, 1, 0);
gl.glScalef(scale, scale, scale);
mesh.draw(GL10. GL_TRIANGLES , 0, 36);
gl.glPopMatrix();
int len = children.size();
for ( int i = 0; i < len; i++) {
children.get(i).render(gl);
}
gl.glPopMatrix();
}
}
Search WWH ::




Custom Search