Second of all, I took the time to write this little tutorial so I would expect a little bit of credit if you find this useful. This, and the source code, is copyrighted by me and Code[sup]x[/sup] software. I will allow reproduction of this document as long as the original copyright stays intact. Other than that, have fun and don't rip me off!
If your looking for a really good tutorial on 3D check out the documentation for the Zed 3D engine. That's where I got a lot of my information from. The VLA and Asphixia demo trainers are also good sources of information. Unfortunately, I don't have URLs for any of the above
Let's skip all the introductions and get right down and dirty. A basic 3D engine consists of three abilities:
- rotation
- translation
- projection
[size="5"]Rotation
Now, before I go to describe rotation, let me first do a quick intro to trigonometry. Trig. is basically the relations of a right angle triangle. The only functions you need to be aware of are sine and cosine: Your basic right angle triangle describes the following:
sine = (O/H)
cosine = (A/H)
Those who already know trig. will notice that I am using angle o to calculate these ratios. This is the only angle you really need to be concerned with in 3D rotation. You should also note the O is the opposite side of angle o and A is the adjacent side of angle o. These are just common trigonometry terms.
Now, consider the below situation. You want to rotate the point S (source) to point D (destination). You can do this simply doing the following:
D.x = cos(a+b) * r
D.y = sin(a+b) * r
Now, simple trigonometry dictates that:
sin(a+b) = sin(a) * cos(b) + cos(a) * sin(b)
cos(a+b) = cos(a) * cos(b) - sin(a) * sin(b)
D.x = r * cos(a) * cos(b) - r * sin(a) * sin(b)
D.y = r * sin(a) * cos(b) + r * cos(a) * sin(b)
r * sin(a) = y
r * cos(a) = x
Now we finally get the below equations:
D.x = x * cos(b) - y * sin(b)
D.y = y * cos(b) + x * sin(b)
It is important to realize that you must store your rotated object and the original object in different locations. You do not want to overwrite the original 3D object because it is used in this equation. It is your S (source) value, or x and y in the above equation.
Here's some pseudo-code to put it all into perspective:
rotated.x = orig.x;
rotated.y = cos(x)*orig.y - sin(x)*orig.z;
rotated.z = sin(x)*orig.y + cos(x)*orig.z;
rotated.x = cos(y)*rotated.x - sin(y)*rotated.y;
rotated.y = sin(y)*rotated.x + cos(y)*rotated.y;
rotated.z = rotated.z;
rotated.x = cos(z)*rotated.x - sin(z)*rotated.z;
rotated.y = rotated.y;
rotated.z = sin(z)*rotated.x + cos(z)*rotated.z;
radian = degree * PI / 180
[size="5"]Translation
Translation is the process of moving your 3D object in the direction of the x,y or z axis. This is done, very simply, by adding values to your rotated x,y and z values. It's really that simple:
translated_x = rotated_x + translation_value;
translated_y = rotated_y + translation_value;
translated_z = rotated_z + translation_value;
[size="5"]Projection
But now that we've rotated an object, how do we display it on a 2D screen? This is where projection comes in. The simplest way of projecting a 3D object is to simply divide your rotated x and y values by your rotated z value. Then plotting these x and y values:
screen_x = x / z;
screen_y = y / z;
d=(screen_width/2) / tan(FOV/2)
d=320 / tan30
d=554
screen_x = (dx * x) / z;
screen_y = (dy * y) / z;
screen_x = (dx * x) / z + (screen_width / 2);
screen_y = (dy * y) / z + (screen_height / 2);
(C)1996 Jeff Weeks and Code[sup]x[/sup] software