/// <summary> /// Test whether this sphere penetrates another, and by how far. /// </summary> /// <param name="other"></param> /// <returns>The distance by which my sphere penetrates the other, or -1 if they don't touch</returns> public float PenetrationBy(Sphere other) { // dist between cell centres (squared) float distSq = Vector3.LengthSq(this.Centre - other.Centre); // sum of the two radii float radii = this.Radius + other.Radius; // If the dist from centre to centre is smaller than the sum of the radii we are in contact if (distSq < (radii * radii)) { // return the penetration distance return (radii - (float)Math.Sqrt(distSq)); } return -1; }
/// <summary> /// Combine two bounding spheres to create a single sphere enclosing both. /// Overload accepting a Sphere as argument /// </summary> public void CombineBounds(Sphere add) { CombineBounds(add.Centre, add.Radius); }
/// <summary> /// Test whether this sphere penetrates another. /// Faster than PenetrationBy() but doesn't return penetration distance (use if dist not needed) /// </summary> /// <param name="other"></param> /// <returns>The distance by which my sphere penetrates the other, or -1 if they don't touch</returns> public bool IsPenetrating(Sphere other) { // dist between cell centres (squared) float distSq = Vector3.LengthSq(this.Centre - other.Centre); // sum of the two radii float radii = this.Radius + other.Radius; // If the dist from centre to centre is smaller than the sum of the radii we are in contact if (distSq < (radii * radii)) return true; return false; }
/// <summary> /// Return the SQUARE of the distance between this sphere's centre and another's /// </summary> /// <param name="other"></param> /// <returns></returns> public float CentreToCentreSq(Sphere other) { return Vector3.LengthSq(this.Centre - other.Centre); }