Permalink
Browse files

Merge pull request #996 from csulmone/develop3d

BoundingBox Intersection with BoundingSphere Fix
  • Loading branch information...
2 parents 13adca9 + c3b5dd0 commit 3414b3570f52832e464c473ac6df0a7165a02ff7 @KonajuGames KonajuGames committed Feb 13, 2013
Showing with 76 additions and 24 deletions.
  1. +76 −24 MonoGame.Framework/BoundingBox.cs
@@ -37,16 +37,16 @@ Olivier Dufour (Duff)
namespace Microsoft.Xna.Framework
{
- #if WINRT
+#if WINRT
[DataContract]
- #else
+#else
[Serializable]
- #endif
+#endif
public struct BoundingBox : IEquatable<BoundingBox>
{
#region Public Fields
-
+
#if WINRT
[DataMember]
#endif
@@ -143,28 +143,81 @@ public ContainmentType Contains(BoundingFrustum frustum)
public ContainmentType Contains(BoundingSphere sphere)
{
- if (sphere.Center.X - Min.X > sphere.Radius
- && sphere.Center.Y - Min.Y > sphere.Radius
- && sphere.Center.Z - Min.Z > sphere.Radius
- && Max.X - sphere.Center.X > sphere.Radius
- && Max.Y - sphere.Center.Y > sphere.Radius
- && Max.Z - sphere.Center.Z > sphere.Radius)
+ if (sphere.Center.X - Min.X >= sphere.Radius
+ && sphere.Center.Y - Min.Y >= sphere.Radius
+ && sphere.Center.Z - Min.Z >= sphere.Radius
+ && Max.X - sphere.Center.X >= sphere.Radius
+ && Max.Y - sphere.Center.Y >= sphere.Radius
+ && Max.Z - sphere.Center.Z >= sphere.Radius)
return ContainmentType.Contains;
double dmin = 0;
- if (sphere.Center.X - Min.X <= sphere.Radius)
- dmin += (sphere.Center.X - Min.X) * (sphere.Center.X - Min.X);
- else if (Max.X - sphere.Center.X <= sphere.Radius)
- dmin += (sphere.Center.X - Max.X) * (sphere.Center.X - Max.X);
- if (sphere.Center.Y - Min.Y <= sphere.Radius)
- dmin += (sphere.Center.Y - Min.Y) * (sphere.Center.Y - Min.Y);
- else if (Max.Y - sphere.Center.Y <= sphere.Radius)
- dmin += (sphere.Center.Y - Max.Y) * (sphere.Center.Y - Max.Y);
- if (sphere.Center.Z - Min.Z <= sphere.Radius)
- dmin += (sphere.Center.Z - Min.Z) * (sphere.Center.Z - Min.Z);
- else if (Max.Z - sphere.Center.Z <= sphere.Radius)
- dmin += (sphere.Center.Z - Max.Z) * (sphere.Center.Z - Max.Z);
+ double e = sphere.Center.X - Min.X;
+ if (e < 0)
+ {
+ if (e < -sphere.Radius)
+ {
+ return ContainmentType.Disjoint;
+ }
+ dmin += e * e;
+ }
+ else
+ {
+ e = sphere.Center.X - Max.X;
+ if (e > 0)
+ {
+ if (e > sphere.Radius)
+ {
+ return ContainmentType.Disjoint;
+ }
+ dmin += e * e;
+ }
+ }
+
+ e = sphere.Center.Y - Min.Y;
+ if (e < 0)
+ {
+ if (e < -sphere.Radius)
+ {
+ return ContainmentType.Disjoint;
+ }
+ dmin += e * e;
+ }
+ else
+ {
+ e = sphere.Center.Y - Max.Y;
+ if (e > 0)
+ {
+ if (e > sphere.Radius)
+ {
+ return ContainmentType.Disjoint;
+ }
+ dmin += e * e;
+ }
+ }
+
+ e = sphere.Center.Z - Min.Z;
+ if (e < 0)
+ {
+ if (e < -sphere.Radius)
+ {
+ return ContainmentType.Disjoint;
+ }
+ dmin += e * e;
+ }
+ else
+ {
+ e = sphere.Center.Z - Max.Z;
+ if (e > 0)
+ {
+ if (e > sphere.Radius)
+ {
+ return ContainmentType.Disjoint;
+ }
+ dmin += e * e;
+ }
+ }
if (dmin <= sphere.Radius * sphere.Radius)
return ContainmentType.Intersects;
@@ -445,7 +498,7 @@ public void Intersects(ref Plane plane, out PlaneIntersectionType result)
result = PlaneIntersectionType.Back;
return;
}
-
+
result = PlaneIntersectionType.Intersecting;
}
@@ -477,4 +530,3 @@ public override string ToString()
#endregion Public Methods
}
}
-

0 comments on commit 3414b35

Please sign in to comment.