Game Development Reference

In-Depth Information

Step 6.
Play. The terrain will have random heights across its surface.

Note that the Mesh Collider is added after the heights have been set.

This will ensure that the FPC does not fall through.

After the mesh vertices are modified in the
y
direction for height, the

bounding volume and normals are recreated. The bounding volume

is used by the game engine to improve the processing of geometrical

operations such as collision detection and overlap. The reason being

that in the first instance, computing overlapping volumes is easier than

lower level collision detection. Before exact collisions are calculated,

determining if there might be a collision between two objects is more

efficient with bounding volumes.

In addition, the plane's normals are also recalculated. Because the

plane's mesh has changed shape, the normals must be adjusted for

the new vertex values for the mesh to be shaded and shadowed

correctly.

Using a random function to determine height is not a realistic way to

produce rise and fall in a terrain, as the height can change erratically with

each vertex. At small height values it might look all right, but try changing

the random range from 0 to 50 and the surface of the plane will become

very jagged.

Step 7.
To create smoother rises and falls in a surface, a mathematical

curve such as Sine or Cosine can be used. Modify
makeTerrain.js
to

reflect the changes shown in
Listing 7.2
.

Listing 7.2 Using a Sine Function to Set the Terrain Height

. . .

for (var v
=
0; v
<
vertices.Length; v++)

{

vertices[v].y
=
Mathf.Sin(vertices[v].x * 10);

}

. . .

Step 8.
Play. The shape of the sine wave will be visually evident.

The terrain will be smooth, but too uniform to replicate a natural

landscape.

Another way to create a landscape procedurally is to use Perlin Noise, a

pseudo-random mathematical algorithm that provides smooth gradients

between points. This is the method used for landscape generation in

Minecraft
.