{- Matrix Manipulation Code. The key to doing rotations and movement in a 3D system is to use matrix math. Since there are going to be no translations in this engine, i'm dumping the 4th row and col in the matrix math. -} module Mat3d where import Vec3d {- X Y Z W -} type Matrix3 = (Float, Float, Float, Float, Float, Float, Float, Float, Float) ident :: Matrix3 ident = (1, 0, 0, 0, 1, 0, 0, 0, 1) vecMultMat :: Vector3 -> Matrix3 -> Vector3 vecMultMat (x, y, z) m = (x', y', z') where (xx,xy,xz, yx,yy,yz, zx,zy,zz) = m x' = x*xx + y*yx + z*zx y' = x*xy + y*yy + z*zy z' = x*xz + y*yz + z*zz {- These functions are used to generate a the rotation matricies -} rotX :: Float -> Matrix3 rotX theta = (1, 0, 0, 0, cx, sx, 0, nsx, cx) where cx = cos theta sx = sin theta nsx = -sx rotY :: Float -> Matrix3 rotY theta = (cy, 0, nsy, 0, 1, 0, sy, 0, cy) where cy = cos theta sy = sin theta nsy = -sy rotZ :: Float -> Matrix3 rotZ theta = (cz, sz, 0, -sz, cz, 0, 0, 0, 1) where cz = cos theta sz = sin theta {- Scaling. Not as important -} scale x y z = (x, 0, 0, 0, y, 0, 0, 0, z) rotTest1 = (0,0,0) `vecMultMat` (rotY pi) rotTest2 = (1,0,0) `vecMultMat` (rotY pi) rotTest3 = (0,1,0) `vecMultMat` (rotY pi) rotTest4 = (0,0,1) `vecMultMat` (rotY pi)