//public void Intersects(ref BoundingBox box, out float? result) //{ // result = Intersects(box); //} //public float? Intersects(BoundingFrustum frustum) //{ // throw new NotImplementedException(); //} //public float? Intersects(BoundingSphere sphere) //{ // float? result; // Intersects(ref sphere, out result); // return result; //} //public float? Intersects(Plane plane) //{ // throw new NotImplementedException(); //} //public void Intersects(ref Plane plane, out float? result) //{ // throw new NotImplementedException(); //} public static float?Intersects(this Ray ray, BoundingSphere sphere) { // Find the vector between where the ray starts the the sphere's centre Vector3 difference = sphere.Center - ray.origin; float differenceLengthSquared = difference.sqrMagnitude; float sphereRadiusSquared = sphere.Radius * sphere.Radius; float distanceAlongRay; // If the distance between the ray start and the sphere's centre is less than // the radius of the sphere, it means we've intersected. N.B. checking the LengthSquared is faster. if (differenceLengthSquared < sphereRadiusSquared) { return(0.0f); } //Vector3Helper.Dot(ref this.Direction, ref difference, out distanceAlongRay); distanceAlongRay = Vector3Helper.Dot(ray.direction, difference); // If the ray is pointing away from the sphere then we don't ever intersect if (distanceAlongRay < 0) { return(null); } // Next we kinda use Pythagoras to check if we are within the bounds of the sphere // if x = radius of sphere // if y = distance between ray position and sphere centre // if z = the distance we've travelled along the ray // if x^2 + z^2 - y^2 < 0, we do not intersect float dist = sphereRadiusSquared + distanceAlongRay * distanceAlongRay - differenceLengthSquared; return((dist < 0) ? null : distanceAlongRay - (float?)Math.Sqrt(dist)); }
public BoundingSphere Transform(Matrix matrix) { BoundingSphere sphere = new BoundingSphere(); sphere.Center = Vector3Helper.Transform(ref this.Center, matrix); sphere.Radius = this.Radius * ((float)Math.Sqrt((double)Math.Max(((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12)) + (matrix.M13 * matrix.M13), Math.Max(((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22)) + (matrix.M23 * matrix.M23), ((matrix.M31 * matrix.M31) + (matrix.M32 * matrix.M32)) + (matrix.M33 * matrix.M33))))); return(sphere); }
public void Transform(ref Matrix matrix, out BoundingSphere result) { result.Center = Vector3Helper.Transform(ref this.Center, matrix); result.Radius = this.Radius * ((float)Math.Sqrt((double)Math.Max(((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12)) + (matrix.M13 * matrix.M13), Math.Max(((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22)) + (matrix.M23 * matrix.M23), ((matrix.M31 * matrix.M31) + (matrix.M32 * matrix.M32)) + (matrix.M33 * matrix.M33))))); }