Game Development Reference
In-Depth Information
Figure 8-13. Calculating a normal for a vertex (purple) as the average of faces normals (green).
If the mesh contains a large variation of triangle sizes, a better approximation is achieved by weighing the
average by the area of the triangles.
The resulting average normal would not generally be a length of 1.0, so another division by length is needed.
Up to this point, all this normal calculation work is done in a pre-processing stage. With a normal for every
vertex calculated in advance, it is now possible to quickly calculate a smooth normal for every point on the
surface as a linear combination of the normals of the surrounding vertices. We are, however, only
interested in normals on the edges of the grid. For example, if the bike is situated a third of the way on the
edge between vertices A and B, the normal we'll use for the coordinate base transformation is:
N p = 0.33 * N A + 0.66 * N B
Where N A and N B are the normals of vertex A and vertex B, respectively.
Finding the forward direction of the bike is easier than finding the normal. The bike movement is always
constrained to be between two vertices that are adjacent in the original quad mesh of the grid model.
When the bike travels from vertex A to vertex B, a good approximation of the forward direction is the
direction vector between A and B. This vector is calculated as the subtraction of the coordinates of the two
vertices:
F' = B - A
Similar to normals, we refer to this as an approximation—since the desired movement on the surface
should be as smooth as possible, while the edges and triangles are straight and flat. This curvature is
expressed by the smooth normals we calculated, so this approximation of the forward vector is not
guaranteed to be exactly perpendicular to the calculated normal. Slight distortions in the movement may
be noticeable if we'll use F' from the formula. To calculate the exact forward direction, and simultaneously
the R vector, we perform two cross product calculations:
R = normalize(N × F')
F = R × N
 
Search WWH ::




Custom Search