Plotting 3D Points onto a 2D screen
This is a very useful and quick way to draw 3D point onto your screen.
screen.x = x / z * zoom screen.y = y / z * zoom |
This will produce the screen coordinates of the 3D point. 
zoom is the is the distance beetween the origin and the 45° mark. By altering the zoom, or changing it independantly for the x and y lines, effects like fish eye can be produced.
x, y and z are the 3D coordinates of the point, screen.x and screen.y are the 2D coordinates of the point as it would appear on the screen.
This rountine assumes that the centre of your screen is the origin. So that coordinates (0, 0) are in the centre of your screen. However, practically, you will never be in a situation with a PC, where the origin is in the centre of the screen, it is always at the top right hand corner. So to compensate, half the screen's width and height must be added. That is that if you have a screen resulution of 640x480, your centre will be (320, 240). So that must be added to the routine.
screen.x = x / z * zoom + centre.x screen.y = y / z * zoom + centre.y |
The camera in this routine is looking straight down the z axis with x for left and right, and y for up and down. If z is negative, the point will be behind the camera, and so will not be visible, so this must be added to the routine.
if z > 0 then screen.x = x / z * zoom + centre.x screen.y = y / z * zoom + centre.y end if |
If the camera has to move about, and change angle, every coordinate entering this routine must be transformed. I will use position for the position of the camera, and pan for the camera panning, and new just for temporary calculations.

These are the axises for pan
procedure 3Dto2D (x, y, z, pan, centre, position)
x = x + position.x y = y + position.y z = z + position.z
new.x = x*cos(pan.x) - z*sin(pan.x) new.z = x*sin(pan.x) + z*cos(pan.x) new.y = y*cos(pan.y) - new.z*sin(pan.y) z = new.y*cos(pan.y) - new.z*sin(pan.y) x = new.x*cos(pan.z) - new.y*sin(pan.z) y = new.x*sin(pan.z) + new.y*cos(pan.z)
if z > 0 then screen.x = x / z * zoom + centre.x screen.y = y / z * zoom + centre.y end if |
To create 3D triangles, quads and polygons all you need do is produce several of these points, and join them together.
Return to the Good Looking Textured Light Sourced Bouncy Fun Smart and Stretchy Page.