protected override TriangleCollidable GetOpposingCollidable(int index)
{
//Construct a TriangleCollidable from the static mesh.
var toReturn = PhysicsResources.GetTriangleCollidable();
Vector3 terrainUp = new Vector3(mesh.worldTransform.LinearTransform.M21, mesh.worldTransform.LinearTransform.M22, mesh.worldTransform.LinearTransform.M23);
float dot;
Vector3 AB, AC, normal;
var shape = toReturn.Shape;
mesh.Shape.GetTriangle(index, ref mesh.worldTransform, out shape.vA, out shape.vB, out shape.vC);
Vector3 center;
Vector3.Add(ref shape.vA, ref shape.vB, out center);
Vector3.Add(ref center, ref shape.vC, out center);
Vector3.Multiply(ref center, 1 / 3f, out center);
Vector3.Subtract(ref shape.vA, ref center, out shape.vA);
Vector3.Subtract(ref shape.vB, ref center, out shape.vB);
Vector3.Subtract(ref shape.vC, ref center, out shape.vC);
//The bounding box doesn't update by itself.
toReturn.worldTransform.Position = center;
toReturn.worldTransform.Orientation = Quaternion.Identity;
toReturn.UpdateBoundingBoxInternal(0);
Vector3.Subtract(ref shape.vB, ref shape.vA, out AB);
Vector3.Subtract(ref shape.vC, ref shape.vA, out AC);
Vector3.Cross(ref AB, ref AC, out normal);
Vector3.Dot(ref terrainUp, ref normal, out dot);
if (dot > 0)
{
shape.sidedness = TriangleSidedness.Clockwise;
}
else
{
shape.sidedness = TriangleSidedness.Counterclockwise;
}
shape.collisionMargin = mobileMesh.Shape.MeshCollisionMargin;
return toReturn;
}