/// <summary> /// Subtracts two matrices. /// </summary> /// <param name="value1">The first matrix.</param> /// <param name="value2">The second matrix.</param> /// <returns>The difference of both values.</returns> #region public static JMatrix operator -(JMatrix value1, JMatrix value2) public static TSMatrix operator -(TSMatrix value1, TSMatrix value2) { TSMatrix result; TSMatrix.Multiply(ref value2, -FP.One, out value2); TSMatrix.Add(ref value1, ref value2, out result); return(result); }
/// <summary> /// Matrices are added. /// </summary> /// <param name="matrix1">The first matrix.</param> /// <param name="matrix2">The second matrix.</param> /// <returns>The sum of both matrices.</returns> #region public static JMatrix Add(JMatrix matrix1, JMatrix matrix2) public static TSMatrix Add(TSMatrix matrix1, TSMatrix matrix2) { TSMatrix result; TSMatrix.Add(ref matrix1, ref matrix2, out result); return(result); }
/// <summary> /// Calculates the inertia of the shape relative to the center of mass. /// </summary> /// <param name="shape"></param> /// <param name="centerOfMass"></param> /// <param name="inertia">Returns the inertia relative to the center of mass, not to the origin</param> /// <returns></returns> #region public static FP CalculateMassInertia(Shape shape, out JVector centerOfMass, out JMatrix inertia) public static FP CalculateMassInertia(Shape shape, out TSVector centerOfMass, out TSMatrix inertia) { FP mass = FP.Zero; centerOfMass = TSVector.zero; inertia = TSMatrix.Zero; if (shape is Multishape) { throw new ArgumentException("Can't calculate inertia of multishapes.", "shape"); } // build a triangle hull around the shape List <TSVector> hullTriangles = new List <TSVector>(); shape.MakeHull(ref hullTriangles, 3); // create inertia of tetrahedron with vertices at // (0,0,0) (1,0,0) (0,1,0) (0,0,1) FP a = FP.One / (60 * FP.One), b = FP.One / (120 * FP.One); TSMatrix C = new TSMatrix(a, b, b, b, a, b, b, b, a); for (int i = 0; i < hullTriangles.Count; i += 3) { TSVector column0 = hullTriangles[i + 0]; TSVector column1 = hullTriangles[i + 1]; TSVector column2 = hullTriangles[i + 2]; TSMatrix A = new TSMatrix(column0.x, column1.x, column2.x, column0.y, column1.y, column2.y, column0.z, column1.z, column2.z); FP detA = A.Determinant(); // now transform this canonical tetrahedron to the target tetrahedron // inertia by a linear transformation A TSMatrix tetrahedronInertia = TSMatrix.Multiply(A * C * TSMatrix.Transpose(A), detA); TSVector tetrahedronCOM = (FP.One / (4 * FP.One)) * (hullTriangles[i + 0] + hullTriangles[i + 1] + hullTriangles[i + 2]); FP tetrahedronMass = (FP.One / (6 * FP.One)) * detA; inertia += tetrahedronInertia; centerOfMass += tetrahedronMass * tetrahedronCOM; mass += tetrahedronMass; } inertia = TSMatrix.Multiply(TSMatrix.Identity, inertia.Trace()) - inertia; centerOfMass = centerOfMass * (FP.One / mass); FP x = centerOfMass.x; FP y = centerOfMass.y; FP z = centerOfMass.z; // now translate the inertia by the center of mass TSMatrix t = new TSMatrix( -mass * (y * y + z * z), mass * x * y, mass * x * z, mass * y * x, -mass * (z * z + x * x), mass * y * z, mass * z * x, mass * z * y, -mass * (x * x + y * y)); TSMatrix.Add(ref inertia, ref t, out inertia); return(mass); }
/// <summary> /// Adds two matrices. /// </summary> /// <param name="value1">The first matrix.</param> /// <param name="value2">The second matrix.</param> /// <returns>The sum of both values.</returns> #region public static JMatrix operator +(JMatrix value1, JMatrix value2) public static TSMatrix operator +(TSMatrix value1, TSMatrix value2) { TSMatrix result; TSMatrix.Add(ref value1, ref value2, out result); return(result); }