Chapter 28. Trees That Grow

The last two chapters have been about creating landscapes: the fractal terrain in Chapter 26 was empty, but the Terragen-generated landscape in Chapter 27 was decorated with 3D models and ground cover (2D images that always face the user). This mix of models and images is enough in most situations but still lacking in one area: the scenery doesn’t change. For example, the trees don’t sway in the wind, and the sagebrush doesn’t tumble.

As the title of this chapter suggests, this chapter focuses on making trees grow. Each tree starts from a tiny sampling, then young green shoots turn brown, grow, branch, and sprout leaves. However, the underlying aim is to describe a rule-based approach to animation, which can be applied to many kinds of 3D shapes. This approach has the following characteristics:

Use of if-then rules

Each rule is a Java if-then statement. If all the conditions of a rule evaluate to true, then the rule’s actions are carried out.

Tip

I’m not using a rule-based language such as Prolog, or a Java rules engine such as JESS. There’s no need to step beyond standard Java.

Time-based sequential evaluation

The rules are evaluated at regular intervals during the program’s execution. They’re executed in a fixed, sequential order, defined by their textual order in the applyRules() method in the GrowthBehavior class.

Each rule is applied to every tree limb

When it’s time for a rule to be evaluated, it’s applied to every tree limb (tree branch) in the scene. ...

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.