Building a Tree Limb

Each tree limb is represented by a TreeLimb object, which internally builds a subgraph such as the one in Figure 28-4.

The TreeLimb subgraph

Figure 28-4. The TreeLimb subgraph

Several of the TransformGroup nodes (those with "TG" in their name) are used to support translation, rotation, and scaling effects, leaving the basic geometry (a cylinder) unaffected. This means that I could avoid using the complicated GeometryUpdater interface.

The startBG BranchGroup is linked to a parent limb via the parent's endLimbTG node. Since the link is made at execution time, Java 3D requires the use of a BranchGroup.

orientTG holds the orientation of the limb (its initial axis of rotation and angle to that axis). Once set, this cannot change during execution. This restriction is in place to simplify the class's implementation.scaleTG manages the scaling of the x-, y-, and z-dimensions of the cylinder. The x- and z-values are kept the same (they represent the radius); the y-axis is for the length.

The center point for Java 3D's Cylinder is its middle, but I want the origin to be its base at the place where the cylinder connects to the parent limb. baseTG moves the cylinder up the y-axis by length/2 to achieve it. The capabilities for the Cylinder's Material node component are set so that its color can be adjusted at runtime.

Child limbs or leaves are attached to this limb through endLimbTG. The transform ...

Get Killer Game Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.