// test collision between two spheres public static bool TestSphereSphere(Transform a, Transform b) { // if the distance between their centers is less than the sum of their radii // return true (i.e. is colliding) // else return false (i.e. is not colliding) return ((a.Position - b.Position).Length() < (a.Scale + b.Scale).Y); }
// Default Constructor public Transform() { Position = Vector3.Zero; Scale = Vector3.One; Rotation = Quaternion.Identity; Children = new List<Transform>(); parent = null; }
public static bool TestSphereTriangle(Transform sphere, Vector3 a, Vector3 b, Vector3 c, Vector3 normal) { // Find the separation distance between the sphere center and the triangle plane float separation = Vector3.Dot(sphere.Position - a, normal); // If the separation value is out of bounds, quit if ((separation > sphere.Scale.Y) || (separation < 0)) { return false; } else // Otherwise, there is collision with the plane. Find the nearest point { Vector3 pointOnPlane = sphere.Position - normal * separation; // Find the barycentric coordinates float area1 = Vector3.Dot(Vector3.Cross(b - a, pointOnPlane - a), normal); float area2 = Vector3.Dot(Vector3.Cross(c - b, pointOnPlane - b), normal); float area3 = Vector3.Dot(Vector3.Cross(a - c, pointOnPlane - c), normal); return !((area1 < 0) || (area2 < 0) || (area3 < 0)); } }
// test collision between a sphere and a triangle given as three vectors (i.e. lines) public static bool TestSphereTriangle(Transform sphere, Vector3 a, Vector3 b, Vector3 c) { Vector3 normal = Vector3.Normalize(Vector3.Cross(b - a, c - a)); return TestSphereTriangle(sphere, a, b, c, normal); }
// test collision between a sphere and a triangle public static bool TestSphereTriangle(Transform sphere, Triangle triangle) { return TestSphereTriangle(sphere, triangle.a, triangle.b, triangle.c); }