/// <summary> //Can only be done in Matrix4X4 b/c it uses the 3rd row /// Build a world space to camera space matrix - Based on OpenTK.Matrix4.LookAt /// </summary> /// <param name="eye">Eye (camera) position in world space</param> /// <param name="target">Target position in world space</param> /// <param name="unitUpVector">Up vector in world space (should not be parallel to the camera direction, that is target - eye)</param> public void SetLookAt(Vector3H1 eye, Vector3H1 target, Vector3H1 unitUpVector) { //unitUpVector.SetUnitVector() Vector3H1 z = eye - target; z.SetUnitVector(); Vector3H1 x = Vector3H1.CrossProduct(unitUpVector, z); x.SetUnitVector(); Vector3H1 y = Vector3H1.CrossProduct(z, x); y.SetUnitVector(); Data[0, 0] = x.X; Data[0, 1] = y.X; Data[0, 2] = z.X; Data[0, 3] = 0; Data[1, 0] = x.Y; Data[1, 1] = y.Y; Data[1, 2] = z.Y; Data[1, 3] = 0; Data[2, 0] = x.Z; Data[2, 1] = y.Z; Data[2, 2] = z.Z; Data[2, 3] = 0; Data[3, 0] = -((x.X * eye.X) + (x.Y * eye.Y) + (x.Z * eye.Z)); Data[3, 1] = -((y.X * eye.X) + (y.Y * eye.Y) + (y.Z * eye.Z)); Data[3, 2] = -((z.X * eye.X) + (z.Y * eye.Y) + (z.Z * eye.Z)); Data[3, 3] = 1; }
/// <summary> /// QuickSolve3D /// Solves matrix equation [A] * x = N for x based on Cramer's Rule /// where A is a Matrix, N is a vector (usually a normal) /// ----------------------------------------------------------------- /// Marix A is typed as IMatrix so that Matrix3X3, Matrix3H4 or Matrix4X4 can be used /// extra data is ignored, but Matrix2X2 would cause an error (but it doesn't exist yet) /// </summary> /// <param name="A">IMatrix</param> /// <param name="N">IVector3H1</param> /// <param name="bUnitize">bool</param> /// <returns></returns> public static Vector3H1 QuickSolve3D(IMatrix A, IVector3H1 N, bool bUnitize = true) { float determinate = Determinate3D(A); //if the determinate comes out to zero, it's probably b/c this is actually a small if (determinate == 0) //2X2 matrix in a larger object w/ the rest zeros, so just { determinate = Determinate2D(A); //go ahead & take the 2X2 determinate A.Data[2, 2] = 1; //but you aldo have to set the Z-identity } //this works, but it it good to change the matrix???????? Vector3H1 V = new Vector3H1(); V.X = ((A.Data[2, 2] * A.Data[1, 1] - A.Data[1, 2] * A.Data[2, 1]) * N.X + (A.Data[2, 1] * A.Data[0, 2] - A.Data[0, 1] * A.Data[2, 2]) * N.Y + (A.Data[0, 1] * A.Data[1, 2] - A.Data[1, 1] * A.Data[0, 2]) * N.Z) / determinate; V.Y = ((A.Data[1, 2] * A.Data[2, 0] - A.Data[2, 2] * A.Data[1, 0]) * N.X + (A.Data[0, 0] * A.Data[2, 2] - A.Data[2, 0] * A.Data[0, 2]) * N.Y + (A.Data[1, 0] * A.Data[0, 2] - A.Data[0, 0] * A.Data[1, 2]) * N.Z) / determinate; V.Z = ((A.Data[2, 1] * A.Data[1, 0] - A.Data[1, 1] * A.Data[2, 0]) * N.X + (A.Data[0, 1] * A.Data[2, 0] - A.Data[0, 0] * A.Data[2, 1]) * N.Y + (A.Data[0, 0] * A.Data[1, 1] - A.Data[0, 1] * A.Data[1, 0]) * N.Z) / determinate; if (bUnitize) { V.SetUnitVector(); } return(V); }