Equations

Imagine you are trying to program a game using vector graphics. There is a problem, though. How do you display 3D points on a 2D screen? You need to know the equations to do that in order to go on programming your 3D game.

Putting a 3D point onto a 2D screen is called 3D projection. The equation for 3D projection is:

x=px/pz*n+hw
y=py/pz*n+hh

Where px is the difference between the veiwer's x coordinate and the point's x coordinate, py is the difference between the veiwer's y coordinate and the point's y coordinate, pz is the difference between the z coordinates, n is a number to make it so that everything's not scrunched together (numbers between 128 and 256 recommended), hw is half the screen's width, and hh is half the screen's height.

So now you have something that displays 3D, but you want to be able to rotate things around you. You would put this before your 3D projection code:

z=pz*cosine(ang)-px*sine(ang)
x=pz*sine(ang)+px*cosine(ang)

Where px and pz are the same as in the 3D projection and ang is the angle that you are facing in radians. Radians are what computers use for angles, where pi(π) is 180°. To convert from angles to radians, multiply the angle by π/180. This equation is for rotation around the y axis. If you would like to rotate around the z axis, change z ang pz to x and px and change x and px to y and py. if you want to rotate around the x azis, replace z and pz with y and py and replace x and px with z and pz. To rotate around any axis, alter the coordinates so that the axis you want to rotate around now passes through the origin, perform the rotation, then reverse the changes you made before the rotation.