|
How the Engine
works
The 3D engine was one of the few projects where
I actually had to contemplate the mathematical
complexities of the engine before I sat down and
programmed. My usual practise in these matters
is to lay down as much code as I can, and then
to fiddle, pretty much at random, until something
clicks and the engine starts to work. This was
my main strategy for Trooper II, where the engine
could be contructed in pieces, and has led to
a very polished and refined finishing product;
however, designing a 3D engine requires much more
thought, since the rules of perspective are fixed;
unlike the imaginary conditions I imposed on the
fictional physical world of Trooper II. The pain
the engine has caused me; writing reams of code,
only to find the perspective just doesn't look
right; despit my calculations; was quite intense,
and I thought that to spare anyone else undergoing
the same ordeal, I would construct a simple guide
to the design of a general 3D engine. It's also
an oportunity to explore my own thought process
as I desgined the things, and analyssis of your
own code is never a bad thing.
First things first; how are objects in a 3D world
represented? Well, in the purest sense, objects
in 2D can be contructed by vectors. You can also,
of course, use sprites, but there is no room for
processing there: you can plonk an image anywhere
you like, but it always looks the same. Good for
a simple engine, but no good if we want stuff
to move in 3D. A real object in 2D can be represented
by a line; just one vector. In 3D, a plane must
be used to represent a real thing; how many strings
of infinite fineness do you see in real life?
These planes are best modelled as polygons, flat
shapes that exist as planes in 3D space. It's
easy to store a polygon, using 3D cartesian coordinates:
all the polys used in my engine are four sided
(optimum speed whilst easy to draw), and I suggest
you do the same, so store all your polys in an
array with 4 entries for each vertex (corner),
each of those represented by an x, a y and a z
value. In my world, the z-axis extends into the
monitor, which may vary from other treatments
of cartesian coords. Now we have a series of planes
suspended in 3D space. If the correct transformations
are applied to these, then they can be moved about
as if they are real 3D objects. When the time
comes to display your world on the monitor, the
3D objects must be reduced onto the 2D screen;
and to make this realistic the correct perspective
algorithm must be used.
Next page | Back
to 3D projects
|