Game Development Reference

In-Depth Information

There is a whole range of code optimizations that can be applied to the source code

on the CD. I have deliberately avoided making the code more complex by trying to

wring additional performance from it.

This section is intended to give some general pointers. The advice is based on the

commercial engine I developed and on which
cyclone
is based. Before you embark

on any optimization effort, I would strongly advise you to get a good profiler (I use

Intel's VTune for PC work) and only optimize areas of the software that you can prove

are causing performance problems.

Caching Contact Data

A relatively simple optimization is to retain the calculations performed during contact

resolution as data in the
contact
class. When resolving one contact several times, we

currently recalculate its
deltaVelocity
and other values. These can instead be stored

in the contact data structure and only calculated when first needed.

This is a tradeoff of memory against speed. If you have a large number of contacts

that are only likely to be considered once, then it may be better to leave the algorithm

as is.

Vectorizing Mathematics

The next optimization takes advantage of the mathematical hardware on PCs and

most consoles. This hardware is capable of processing more than one floating-point

number at the same time. Rather than performing all our vector and matrix manip-

ulation as a series of floating-point operations, we can have it process a whole vector

at a time.

For single-precision builds of the engine (things get considerably more complex

for double precision, so we'll ignore that) on a 32-bit PC, we can fit a whole vector

into one of the SSE registers. Using SSE mathematics we can perform a matrix trans-

form of a vector in only four operations. Vector cross products, additions, and other

manipulations are equally accelerated. Most consoles (older hand-helds being the ex-

ception) provide the same facilities. On the Sony PlayStation 2, for example, there is

a dedicated vector mathematics unit you can use for the same effect.

I'm not going to dive into detail about vectorizing mathematics. There is rea-

sonable documentation available with Visual Studio for the Windows PC and many

excellent introductions to the subject online. For serious PC development I would

recommend Intel's
Software Optimization Cookbook
[Gerber, 2002] (whether or not

you are targeting Intel processors).

Twizzling Rigid-Body Data

The vector mathematics hardware on PCs is optimized to run the same program on

multiple bits of data at the same time. Rather than go through one algorithm per rigid

body, it would be better to run the same algorithm for a group of bodies at the same

time.