The 3D Story
Your guide to the third dimension.

Projection

Well, how does this 3D stuff work, you ask your self. Just read on and I'll show you the basics first.
The first thing we need to know is how things are ordered in 3D space. I've directions defined it like this : X=horisontal, Y=vertical, Z=depth. This sorted out we can produce a 2D image. We'll do this by using triangles.



This picture needs some explanations. The point marked V is your eye and the line marked S is the screen. The horisontal line going from the V and to the right is the center of the screen. The dist value is the distance (in pixels) from the viewer's eye to the screen, the Z3 represents how far "into" the screen the point is. The Y3 is how high above the middle of the screen the point is. The Y2 is the 2D projection of the pixel.
From this we can develop a formula ...

Y2/Dist = Y3/(Dist+Z3)

And the part we're interested in is Y2 ...

Y2 = Y3*Dist/(Z3+Dist)

This formula can be applied to the X values like this :

X2 = X3*Dist/(Z3+Dist)

By adding appropriate values to the results we can move the origo from the top-left corner of the screen to the point where we want it. We'll call these value XMiddle and YMiddle. Here are the resulting formulas :

X2 = X3*Dist/(Z3+Dist)+XMiddle
Y2 = Y3*Dist/(Z3+Dist)+YMiddle

Yes! Now we've got the projection formulas working. Before you start coding, a tip, check that Z3+Dist doesn't equal to zero.
This is all you need to actually display a 3D world. But if you want movement you'll have to read on.



[ Previous | Main Page | Next ]
Copyright©1997 Johan E. Thelin