Commonwealth Network 3D Graphics Programming Introduction: Well... so here I am... well not really... I was probably here a while ago and am not here any longer... but thats besides the point... either way I have just decided that I might as well write a few articles rather than explaining things more than once... so I'll start from the beginning... It will help ( tho, it may not be 100% neccessary ) to have some fundamental trigonometry under your belt before hand... but I'll try and cover as much as I can... also you *must* understand matrix math... if you have trouble understanding anything in this article or want further information on something just e-mail me at bertha@magicnet.net Thanx... FrameMan ( IRC EfNet #C ) 3D Basics: To understand 3D you must first understand 2D. Because all 3D is derived directly from 2D ( *hint*: 3D is just two planes of 2D ). So lets start out simply: Rotation of a point in 2D. So here we go... ok... draw a point, now draw the triangle that the point forms with the center of the axies. Here we have the infamous 90º triangle and its wonderful properties. So let's rotate this thing. To prove the rotation identities we must draw the rotated triangle. And it should kick in any second ( I got bored and did an Animated Gif, everyone always says I have too much time on my hands :). Ok so if you look at that one we know have x,x',y,y',and r ( which remains constant ). So if you recall your trig identities ( if you don't just take my word for it. sin(theta) = y/r cos(theta) = x/r So if we solve for x and y instead (those are our unknowns) we get y' = sin(a+b)r x' = cos(a+b)r So now we use another trig identity... this is: sin(a+b) = sin(a)cos(b)+sin(b)cos(a) cos(a+b) = cos(a)cos(b)-sin(a)sin(b) and with a bit more math ( substitute rsin(a)=y and rcos(a)=x ) we get... y' = ycos(b)+xsin(b) x' = xcos(b)-ysin(b) And this is just what we needed! 'Cause we need everything based on angle B (the angle of rotation). Ok so there we go that's 2D in a few steps. But! We need to 3D space :( D'oh!. And to do this we get to deal with Matrices! D'oh! D'oh!. Ok from this point on your pretty much screwed as to knowing how rotation is done but you can cheat and steal the eqns from the end. (Ha! That's it I'm only putting the matrix form at the end :P so if you want the equations 'cause you never learned your matrices you can e-mail me -- maybe I'll finally get some e-mail! ). Ok... if you recall the evil matrices have something called the "Identity Matrix" which is the matrix version of "1." Thus if you plug anything into an identity matrix it's like multiplying by 1 and so it doesnt alter anything in the equation. So here we go... all of our matrices will be 3x3 so our indetity matrix will be ( notation is like the TI-85 form :) [[1 0 0] [0 1 0] [0 0 1]] Ok... so if we think of the matrix in terms of a system of eqns... Ax+By+Cz we would then put our original equation in matrix form like... [[cos(b) -sin(b) 0] [sin(b) cos(b) 0] [0 0 1]] This is the Rotation Matrix for rotation across the Z-Axis!. Rotation about the Z-Axis is the same as 2D rotation ( think about it... the Z-Axis goes into the screen! ). So now we need to find the X and Y matrices... this is pretty easy actually. All you have to do is plug the equation into the variable components that you will be altering. So here is what we get: [[1 0 0 ] [0 cos(b) -sin(b)] [0 sin(b) cos(b) ]] X Axis [[cos(b) 0 -sin(b)] [0 1 0 ] [sin(b) 0 cos(b) ]] Y Axis So there you go... ( don't yell at me your the one who wanted to know this :) Now we need to precalculate this m0f0 so that it takes a few less clock ticks to pass a point through. Precalculating with Xa, Ya, and Za as the rotation angles we get as our final Rotation Matrix [[cos(Ya)*cos(Za) cos(Ya)*sin(Za) -1*sin(Ya) ] [sin(Xa)*sin(Ya)*cos(Za)-cos(Xa)*sin(Za) sin(Xa)*sin(Ya)*sin(Za)+cos(Xa)*cos(Za) sin(Xa)*cos(Ya)] [cos(Xa)*sin(Ya)*cos(Za)+sin(Xa)*sin(Za) cos(Xa)*sin(Ya)*sin(Za)-sin(Xa)*cos(Za) cos(Xa)*cos(Ya)]] well there it is... the spawn of satan himself... but how do I use it you ask? I'll tell you how... ok for each point you have... you just run it through like this... (BTW, it's been a hard day's night so at this point I'm just giving it to you in terms of that matrix being stored as Trans_MTX[3][3]; okie :) Orig would be your original point... temp = orig; new.x = temp.x*Trans_MTX[0][0]+temp.y*Trans_MTX[1][0]+temp.z*Trans_MTX[2][0]; new.y = temp.x*Trans_MTX[0][1]+temp.y*Trans_MTX[1][1]+temp.z*Trans_MTX[2][1]; new.z = temp.x*Trans_MTX[0][2]+temp.y*Trans_MTX[1][2]+temp.z*Trans_MTX[2][2]; There we have it folks... you can now rotate a point in 3d all by yourself... :) In case your wondering this is all based on the origin of (0,0,0) Enjoy... and e-mail me gawd damnit :) Now get some work done... Hanee Patenaude (a.k.a FrameMan : IRC EfNet #C ) --------------------------------------------------------------------------- The Game Programming MegaSite The Entire Site ©1996,1997,1998 Matt Reiferson. Any Questions/Comments, Feel Free To E-Mail Me.