fixed BoundingBox Intersects Plane

fixed BoundingBox Contains BoundingBox
fixed BoundingBox Intersects BoundingBox
fixed BoundingBox Contains BoundingSphere
fixed BoundingBox Intersects BoundingSphere

BoundingBox tests are all green now
This commit is contained in:
Glatzemann 2011-11-23 09:35:45 +00:00
parent 689b796ff4
commit 2672ebeabf
2 changed files with 63 additions and 103 deletions

View File

@ -79,24 +79,25 @@ namespace ANX.Framework
public void Contains(ref BoundingBox box, out ContainmentType result) public void Contains(ref BoundingBox box, out ContainmentType result)
{ {
result = ContainmentType.Disjoint; if ((this.Max.X < box.Min.X || this.Min.X > box.Max.X) ||
(this.Max.Y < box.Min.Y || this.Min.Y > box.Max.Y) ||
if (box.Min.X >= this.Min.X && box.Min.X <= this.Max.X && (this.Max.Z < box.Min.Z || this.Min.Z > box.Max.Z)
box.Min.Y >= this.Min.Y && box.Min.Y <= this.Max.Y && )
box.Min.Z >= this.Min.Z && box.Min.Z <= this.Max.Z)
{ {
result = ContainmentType.Intersects; result = ContainmentType.Disjoint;
return;
} }
if (box.Max.X >= this.Min.X && box.Max.X <= this.Max.X && if ((this.Min.X <= box.Min.X && box.Max.X <= this.Max.X) &&
box.Max.Y >= this.Min.Y && box.Max.Y <= this.Max.Y && (this.Min.Y <= box.Min.Y && box.Max.Y <= this.Max.Y) &&
box.Max.Z >= this.Min.Z && box.Max.Z <= this.Max.Z) (this.Min.Z <= box.Min.Z && box.Max.Z <= this.Max.Z)
)
{ {
if (result == ContainmentType.Intersects) result = ContainmentType.Contains;
result = ContainmentType.Contains; return;
else
result = ContainmentType.Intersects;
} }
result = ContainmentType.Intersects;
} }
public ContainmentType Contains(BoundingFrustum frustum) public ContainmentType Contains(BoundingFrustum frustum)
@ -137,40 +138,30 @@ namespace ANX.Framework
return result; return result;
} }
//source: monoxna
public void Contains(ref BoundingSphere sphere, out ContainmentType result) public void Contains(ref BoundingSphere sphere, out ContainmentType result)
{ {
//TODO: Find an other way, this one often is wrong! Vector3 clampedSphereCenter;
if (sphere.Center.X - Min.X > sphere.Radius clampedSphereCenter.X = MathHelper.Clamp(sphere.Center.X, this.Min.X, this.Max.X);
&& sphere.Center.Y - Min.Y > sphere.Radius clampedSphereCenter.Y = MathHelper.Clamp(sphere.Center.Y, this.Min.Y, this.Max.Y);
&& sphere.Center.Z - Min.Z > sphere.Radius clampedSphereCenter.Z = MathHelper.Clamp(sphere.Center.Z, this.Min.Z, this.Max.Z);
&& Max.X - sphere.Center.X > sphere.Radius
&& Max.Y - sphere.Center.Y > sphere.Radius if (Vector3.DistanceSquared(sphere.Center, clampedSphereCenter) > (sphere.Radius * sphere.Radius))
&& Max.Z - sphere.Center.Z > sphere.Radius) {
result = ContainmentType.Disjoint;
}
else if ((this.Min.X + sphere.Radius <= sphere.Center.X && sphere.Center.X <= this.Max.X - sphere.Radius) &&
(this.Max.X - this.Min.X > sphere.Radius && this.Min.Y + sphere.Radius <= sphere.Center.Y) &&
(sphere.Center.Y <= this.Max.Y - sphere.Radius && this.Max.Y - this.Min.Y > sphere.Radius) &&
(this.Min.Z + sphere.Radius <= sphere.Center.Z && sphere.Center.Z <= this.Max.Z - sphere.Radius) &&
(this.Max.X - this.Min.X > sphere.Radius)
)
{ {
result = ContainmentType.Contains; result = ContainmentType.Contains;
return;
} }
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);
if (dmin <= sphere.Radius * sphere.Radius)
result = ContainmentType.Intersects;
else else
result = ContainmentType.Disjoint; {
result = ContainmentType.Intersects;
}
} }
public ContainmentType Contains(Vector3 point) public ContainmentType Contains(Vector3 point)
@ -364,22 +355,16 @@ namespace ANX.Framework
public void Intersects(ref BoundingBox box, out bool result) public void Intersects(ref BoundingBox box, out bool result)
{ {
result = false; if ((this.Max.X < box.Min.X || this.Min.X > box.Max.X) ||
(this.Max.Y < box.Min.Y || this.Min.Y > box.Max.Y) ||
if (box.Min.X >= this.Min.X && box.Min.X <= this.Max.X && (this.Max.Z < box.Min.Z || this.Min.Z > box.Max.Z)
box.Min.Y >= this.Min.Y && box.Min.Y <= this.Max.Y && )
box.Min.Z >= this.Min.Z && box.Min.Z <= this.Max.Z)
{ {
result = true; result = false;
return;
} }
else
if (box.Max.X >= this.Min.X && box.Max.X <= this.Max.X &&
box.Max.Y >= this.Min.Y && box.Max.Y <= this.Max.Y &&
box.Max.Z >= this.Min.Z && box.Max.Z <= this.Max.Z)
{ {
result = true; result = true;
return;
} }
} }
@ -414,40 +399,9 @@ namespace ANX.Framework
return result; return result;
} }
//source: monoxna
public void Intersects(ref BoundingSphere sphere, out bool result) public void Intersects(ref BoundingSphere sphere, out bool result)
{ {
//TODO: Find an other way, this one often is wrong! result = Vector3.DistanceSquared(sphere.Center, Vector3.Clamp(sphere.Center, this.Min, this.Max)) <= sphere.Radius * 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)
{
result = true;
return;
}
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);
if (dmin <= sphere.Radius * sphere.Radius)
result = true;
else
result = false;
} }
public PlaneIntersectionType Intersects(Plane plane) public PlaneIntersectionType Intersects(Plane plane)
@ -459,27 +413,33 @@ namespace ANX.Framework
public void Intersects(ref Plane plane, out PlaneIntersectionType result) public void Intersects(ref Plane plane, out PlaneIntersectionType result)
{ {
Vector3 p = this.Min; Vector3 p;
if (plane.Normal.X >= 0) p.X = plane.Normal.X >= 0f ? this.Min.X : this.Max.X;
p.X = this.Max.X; p.Y = plane.Normal.Y >= 0f ? this.Min.Y : this.Max.X;
if (plane.Normal.Y >= 0) p.Z = plane.Normal.Z >= 0f ? this.Min.Z : this.Max.X;
p.Y = this.Max.Y;
if (plane.Normal.Z < 0)
p.Z = this.Max.Z;
float distance; float dot = plane.Normal.X * p.X + plane.Normal.Y * p.Y + plane.Normal.Z * p.Z;
Vector3 planeNormal = -plane.Normal;
Vector3.Dot(ref planeNormal, ref p, out distance); if (dot + plane.D > 0f)
distance -= plane.D; {
if (distance < 0)
result = PlaneIntersectionType.Front; result = PlaneIntersectionType.Front;
else if (distance > 0) return;
}
p.X = plane.Normal.X >= 0f ? this.Max.X : this.Min.X;
p.Y = plane.Normal.Y >= 0f ? this.Max.Y : this.Min.X;
p.Z = plane.Normal.Z >= 0f ? this.Max.Z : this.Min.X;
dot = plane.Normal.X * p.X + plane.Normal.Y * p.Y + plane.Normal.Z * p.Z;
if (dot + plane.D < 0f)
{
result = PlaneIntersectionType.Back; result = PlaneIntersectionType.Back;
else return;
result = PlaneIntersectionType.Intersecting; }
result = PlaneIntersectionType.Intersecting;
} }
public Nullable<float> Intersects(Ray ray) public Nullable<float> Intersects(Ray ray)

View File

@ -31,8 +31,8 @@ using System.Runtime.InteropServices;
// //
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben: // übernehmen, indem Sie "*" eingeben:
[assembly: AssemblyVersion("0.4.24.*")] [assembly: AssemblyVersion("0.4.25.*")]
[assembly: AssemblyFileVersion("0.4.24.0")] [assembly: AssemblyFileVersion("0.4.25.0")]
[assembly:InternalsVisibleTo("ANX.Framework.Windows.DX10")] [assembly:InternalsVisibleTo("ANX.Framework.Windows.DX10")]
[assembly:InternalsVisibleTo("ANX.RenderSystem.Windows.DX11")] [assembly:InternalsVisibleTo("ANX.RenderSystem.Windows.DX11")]