///<summary> /// Constructs a triangle shape. /// The vertices will be recentered. If the center is needed, use the other constructor. ///</summary> ///<param name="vA">First vertex in the triangle.</param> ///<param name="vB">Second vertex in the triangle.</param> ///<param name="vC">Third vertex in the triangle.</param> public TriangleShape(Vector3 vA, Vector3 vB, Vector3 vC) { //Recenter. Convexes should contain the origin. Vector3 center; vA.Add2( ref vB, ref vC, out center ); center.Mult( 1 / 3, out center ); //= ( vA + vB + vC) / 3; vA.Sub( ref center, out this.vA ); vB.Sub( ref center, out this.vB ); vC.Sub( ref center, out this.vC ); UpdateConvexShapeInfo( ComputeDescription(vA, vB, vC, collisionMargin)); }
/// <summary> /// Computes the volume distribution of the triangle. /// The volume distribution can be used to compute inertia tensors when /// paired with mass and other tuning factors. /// </summary> ///<param name="vA">First vertex in the triangle.</param> ///<param name="vB">Second vertex in the triangle.</param> ///<param name="vC">Third vertex in the triangle.</param> /// <returns>Volume distribution of the shape.</returns> public static Matrix3x3 ComputeVolumeDistribution(Vector3 vA, Vector3 vB, Vector3 vC) { Vector3 center;// = (vA + vB + vC) * (1 / 3f); vA.Add2( ref vB, ref vC, out center ); center.Mult( 1 / 3, out center ); //Calculate distribution of mass. const float massPerPoint = .333333333f; //Subtract the position from the distribution, moving into a 'body space' relative to itself. // [ (j * j + z * z) (-j * j) (-j * z) ] //I = I + [ (-j * j) (j * j + z * z) (-j * z) ] // [ (-j * z) (-j * z) (j * j + j * j) ] float i = vA.X - center.X; float j = vA.Y - center.Y; float k = vA.Z - center.Z; //localInertiaTensor += new Matrix(j * j + k * k, -j * j, -j * k, 0, -j * j, j * j + k * k, -j * k, 0, -j * k, -j * k, j * j + j * j, 0, 0, 0, 0, 0); //No mass per point. var volumeDistribution = new Matrix3x3(massPerPoint * (j * j + k * k), massPerPoint * (-i * j), massPerPoint * (-i * k), massPerPoint * (-i * j), massPerPoint * (i * i + k * k), massPerPoint * (-j * k), massPerPoint * (-i * k), massPerPoint * (-j * k), massPerPoint * (i * i + j * j)); i = vB.X - center.X; j = vB.Y - center.Y; k = vB.Z - center.Z; var pointContribution = new Matrix3x3(massPerPoint * (j * j + k * k), massPerPoint * (-i * j), massPerPoint * (-i * k), massPerPoint * (-i * j), massPerPoint * (i * i + k * k), massPerPoint * (-j * k), massPerPoint * (-i * k), massPerPoint * (-j * k), massPerPoint * (i * i + j * j)); Matrix3x3.Add(ref volumeDistribution, ref pointContribution, out volumeDistribution); i = vC.X - center.X; j = vC.Y - center.Y; k = vC.Z - center.Z; pointContribution = new Matrix3x3(massPerPoint * (j * j + k * k), massPerPoint * (-i * j), massPerPoint * (-i * k), massPerPoint * (-i * j), massPerPoint * (i * i + k * k), massPerPoint * (-j * k), massPerPoint * (-i * k), massPerPoint * (-j * k), massPerPoint * (i * i + j * j)); Matrix3x3.Add(ref volumeDistribution, ref pointContribution, out volumeDistribution); return volumeDistribution; }