diff --git a/Bonk/AABB.cs b/Bonk/AABB2D.cs similarity index 80% rename from Bonk/AABB.cs rename to Bonk/AABB2D.cs index 0dd4b7c..c86e25d 100644 --- a/Bonk/AABB.cs +++ b/Bonk/AABB2D.cs @@ -8,7 +8,7 @@ namespace MoonTools.Bonk /// /// Axis-aligned bounding box. /// - public struct AABB : IEquatable + public struct AABB2D : IEquatable { /// /// The top-left position of the AABB. @@ -39,13 +39,13 @@ namespace MoonTools.Bonk /// public float Bottom { get { return Max.Y; } } - public AABB(float minX, float minY, float maxX, float maxY) + public AABB2D(float minX, float minY, float maxX, float maxY) { Min = new Vector2(minX, minY); Max = new Vector2(maxX, maxY); } - public AABB(Vector2 min, Vector2 max) + public AABB2D(Vector2 min, Vector2 max) { Min = min; Max = max; @@ -67,7 +67,7 @@ namespace MoonTools.Bonk /// /// /// - public static AABB Transformed(AABB aabb, Transform2D transform) + public static AABB2D Transformed(AABB2D aabb, Transform2D transform) { var center = (aabb.Min + aabb.Max) / 2f; var extent = (aabb.Max - aabb.Min) / 2f; @@ -75,7 +75,7 @@ namespace MoonTools.Bonk var newCenter = Vector2.Transform(center, transform.TransformMatrix); var newExtent = Vector2.TransformNormal(extent, AbsoluteMatrix(transform.TransformMatrix)); - return new AABB(newCenter - newExtent, newCenter + newExtent); + return new AABB2D(newCenter - newExtent, newCenter + newExtent); } /// @@ -84,7 +84,7 @@ namespace MoonTools.Bonk /// /// /// - public static AABB FromVertices(IEnumerable vertices) + public static AABB2D FromVertices(IEnumerable vertices) { var minX = float.MaxValue; var minY = float.MaxValue; @@ -111,20 +111,28 @@ namespace MoonTools.Bonk } } - return new AABB(minX, minY, maxX, maxY); + return new AABB2D(minX, minY, maxX, maxY); } - public static bool TestOverlap(AABB a, AABB b) + public static bool TestOverlap(AABB2D a, AABB2D b) { return a.Left <= b.Right && a.Right >= b.Left && a.Top <= b.Bottom && a.Bottom >= b.Top; } - public override bool Equals(object obj) + public AABB2D Merge(AABB2D other) { - return obj is AABB aabb && Equals(aabb); + return new AABB2D( + Vector2.Min(Min, other.Min), + Vector2.Max(Max, other.Max) + ); } - public bool Equals(AABB other) + public override bool Equals(object obj) + { + return obj is AABB2D aabb && Equals(aabb); + } + + public bool Equals(AABB2D other) { return Min == other.Min && Max == other.Max; @@ -135,12 +143,12 @@ namespace MoonTools.Bonk return HashCode.Combine(Min, Max); } - public static bool operator ==(AABB left, AABB right) + public static bool operator ==(AABB2D left, AABB2D right) { return left.Equals(right); } - public static bool operator !=(AABB left, AABB right) + public static bool operator !=(AABB2D left, AABB2D right) { return !(left == right); } diff --git a/Bonk/AABB3D.cs b/Bonk/AABB3D.cs new file mode 100644 index 0000000..90d0e03 --- /dev/null +++ b/Bonk/AABB3D.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Numerics; +using MoonTools.Structs; + +namespace MoonTools.Bonk +{ + /// + /// Axis-aligned bounding box in 3 dimensions. + /// + public struct AABB3D : IEquatable + { + public Vector3 Min { get; private set; } + + public Vector3 Max { get; private set; } + + public float Width { get { return Max.X - Min.X; } } + public float Height { get { return Max.Y - Min.Y; } } + public float Depth { get { return Max.Z - Min.Z; } } + + public AABB3D(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) + { + Min = new Vector3(minX, minY, minZ); + Max = new Vector3(maxX, maxY, maxZ); + } + + public AABB3D(Vector3 min, Vector3 max) + { + Min = min; + Max = max; + } + + public static bool TestOverlap(AABB3D a, AABB3D b) + { + return + a.Min.X <= b.Max.X && + a.Max.X >= b.Min.X && + a.Min.Y <= b.Max.Y && + a.Max.Y >= b.Min.Y && + a.Min.Z <= b.Max.Z && + a.Max.Z >= b.Min.Z; + } + + public AABB3D Merge(AABB3D other) + { + return new AABB3D( + Vector3.Min(Min, other.Min), + Vector3.Max(Max, other.Max) + ); + } + + public override bool Equals(object obj) + { + return obj is AABB3D d && Equals(d); + } + + public override int GetHashCode() + { + return HashCode.Combine(Min, Max); + } + + public bool Equals(AABB3D other) + { + return Min == other.Min && Max == other.Max; + } + } +} diff --git a/Bonk/BroadPhase/SpatialHash.cs b/Bonk/BroadPhase/SpatialHash2D.cs similarity index 93% rename from Bonk/BroadPhase/SpatialHash.cs rename to Bonk/BroadPhase/SpatialHash2D.cs index 354a009..cdef78b 100644 --- a/Bonk/BroadPhase/SpatialHash.cs +++ b/Bonk/BroadPhase/SpatialHash2D.cs @@ -9,7 +9,7 @@ namespace MoonTools.Bonk /// Used to quickly check if two shapes are potentially overlapping. /// /// The type that will be used to uniquely identify shape-transform pairs. - public class SpatialHash where T : IEquatable + public class SpatialHash2D where T : IEquatable { private readonly int cellSize; @@ -22,7 +22,7 @@ namespace MoonTools.Bonk public int MaxY { get; private set; } = 0; - public SpatialHash(int cellSize) + public SpatialHash2D(int cellSize) { this.cellSize = cellSize; } @@ -89,7 +89,7 @@ namespace MoonTools.Bonk foreach (var t in hashDictionary[key]) { var (otherShape, otherTransform) = IDLookup[t]; - if (!id.Equals(t) && AABB.TestOverlap(box, otherShape.TransformedAABB(otherTransform))) + if (!id.Equals(t) && AABB2D.TestOverlap(box, otherShape.TransformedAABB(otherTransform))) { yield return (t, otherShape, otherTransform); } @@ -105,7 +105,7 @@ namespace MoonTools.Bonk /// /// A transformed AABB. /// - public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(AABB aabb) + public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(AABB2D aabb) { var (minX, minY) = Hash(aabb.Min); var (maxX, maxY) = Hash(aabb.Max); @@ -125,7 +125,7 @@ namespace MoonTools.Bonk foreach (var t in hashDictionary[key]) { var (otherShape, otherTransform) = IDLookup[t]; - if (AABB.TestOverlap(aabb, otherShape.TransformedAABB(otherTransform))) + if (AABB2D.TestOverlap(aabb, otherShape.TransformedAABB(otherTransform))) { yield return (t, otherShape, otherTransform); } diff --git a/Bonk/IHasAABB2D.cs b/Bonk/IHasAABB2D.cs index e59edda..ce78dec 100644 --- a/Bonk/IHasAABB2D.cs +++ b/Bonk/IHasAABB2D.cs @@ -4,13 +4,13 @@ namespace MoonTools.Bonk { public interface IHasAABB2D { - AABB AABB { get; } + AABB2D AABB { get; } /// /// Returns a bounding box based on the shape. /// /// A Transform for transforming the shape vertices. /// Returns a bounding box based on the shape. - AABB TransformedAABB(Transform2D transform); + AABB2D TransformedAABB(Transform2D transform); } } diff --git a/Bonk/MinkowskiDifference.cs b/Bonk/MinkowskiDifference2D.cs similarity index 100% rename from Bonk/MinkowskiDifference.cs rename to Bonk/MinkowskiDifference2D.cs diff --git a/Bonk/MultiShape.cs b/Bonk/MultiShape2D.cs similarity index 83% rename from Bonk/MultiShape.cs rename to Bonk/MultiShape2D.cs index 6ece10b..b17108a 100644 --- a/Bonk/MultiShape.cs +++ b/Bonk/MultiShape2D.cs @@ -5,22 +5,22 @@ using MoonTools.Structs; namespace MoonTools.Bonk { - public struct MultiShape : IHasAABB2D + public struct MultiShape2D : IHasAABB2D { public ImmutableArray<(IShape2D, Transform2D)> ShapeTransformPairs { get; } - public AABB AABB { get; } + public AABB2D AABB { get; } - public MultiShape(ImmutableArray<(IShape2D, Transform2D)> shapeTransformPairs) + public MultiShape2D(ImmutableArray<(IShape2D, Transform2D)> shapeTransformPairs) { ShapeTransformPairs = shapeTransformPairs; AABB = AABBFromShapes(shapeTransformPairs); } - public AABB TransformedAABB(Transform2D transform) + public AABB2D TransformedAABB(Transform2D transform) { - return AABB.Transformed(AABB, transform); + return AABB2D.Transformed(AABB, transform); } /// @@ -47,7 +47,7 @@ namespace MoonTools.Bonk return ((T, Transform2D))ShapeTransformPairs[0]; } - private static AABB AABBFromShapes(IEnumerable<(IShape2D, Transform2D)> shapeTransforms) + private static AABB2D AABBFromShapes(IEnumerable<(IShape2D, Transform2D)> shapeTransforms) { var minX = float.MaxValue; var minY = float.MaxValue; @@ -76,7 +76,7 @@ namespace MoonTools.Bonk } } - return new AABB(minX, minY, maxX, maxY); + return new AABB2D(minX, minY, maxX, maxY); } } } diff --git a/Bonk/NarrowPhase/Edge.cs b/Bonk/NarrowPhase/Edge2D.cs similarity index 75% rename from Bonk/NarrowPhase/Edge.cs rename to Bonk/NarrowPhase/Edge2D.cs index faf9704..1c3409b 100644 --- a/Bonk/NarrowPhase/Edge.cs +++ b/Bonk/NarrowPhase/Edge2D.cs @@ -2,13 +2,13 @@ namespace MoonTools.Bonk { - internal struct Edge + internal struct Edge2D { public float distance; public Vector2 normal; public int index; - public Edge(float distance, Vector2 normal, int index) + public Edge2D(float distance, Vector2 normal, int index) { this.distance = distance; this.normal = normal; diff --git a/Bonk/NarrowPhase/NarrowPhase.cs b/Bonk/NarrowPhase/NarrowPhase.cs index 1288e01..83e3792 100644 --- a/Bonk/NarrowPhase/NarrowPhase.cs +++ b/Bonk/NarrowPhase/NarrowPhase.cs @@ -16,17 +16,17 @@ namespace MoonTools.Bonk /// public static bool TestCollision(IHasAABB2D hasBoundingBoxA, Transform2D transformA, IHasAABB2D hasBoundingBoxB, Transform2D transformB) { - if (hasBoundingBoxA is MultiShape && hasBoundingBoxB is MultiShape) + if (hasBoundingBoxA is MultiShape2D && hasBoundingBoxB is MultiShape2D) { - return TestCollision((MultiShape)hasBoundingBoxA, transformA, (MultiShape)hasBoundingBoxB, transformB); + return TestCollision((MultiShape2D)hasBoundingBoxA, transformA, (MultiShape2D)hasBoundingBoxB, transformB); } - else if (hasBoundingBoxA is MultiShape && hasBoundingBoxB is IShape2D) + else if (hasBoundingBoxA is MultiShape2D && hasBoundingBoxB is IShape2D) { - return TestCollision((MultiShape)hasBoundingBoxA, transformA, (IShape2D)hasBoundingBoxB, transformB); + return TestCollision((MultiShape2D)hasBoundingBoxA, transformA, (IShape2D)hasBoundingBoxB, transformB); } - else if (hasBoundingBoxA is IShape2D && hasBoundingBoxB is MultiShape) + else if (hasBoundingBoxA is IShape2D && hasBoundingBoxB is MultiShape2D) { - return TestCollision((IShape2D)hasBoundingBoxA, transformA, (MultiShape)hasBoundingBoxB, transformB); + return TestCollision((IShape2D)hasBoundingBoxA, transformA, (MultiShape2D)hasBoundingBoxB, transformB); } else if (hasBoundingBoxA is IShape2D && hasBoundingBoxB is IShape2D) { @@ -68,7 +68,7 @@ namespace MoonTools.Bonk /// /// /// - public static bool TestCollision(MultiShape multiShape, Transform2D multiShapeTransform, IShape2D shape, Transform2D shapeTransform) + public static bool TestCollision(MultiShape2D multiShape, Transform2D multiShapeTransform, IShape2D shape, Transform2D shapeTransform) { foreach (var (otherShape, otherTransform) in multiShape.TransformShapesUsingOffset(multiShapeTransform)) { @@ -86,7 +86,7 @@ namespace MoonTools.Bonk /// /// /// - public static bool TestCollision(IShape2D shape, Transform2D shapeTransform, MultiShape multiShape, Transform2D multiShapeTransform) + public static bool TestCollision(IShape2D shape, Transform2D shapeTransform, MultiShape2D multiShape, Transform2D multiShapeTransform) { foreach (var (otherShape, otherTransform) in multiShape.TransformShapesUsingOffset(multiShapeTransform)) { @@ -104,7 +104,7 @@ namespace MoonTools.Bonk /// /// /// - public static bool TestCollision(MultiShape multiShapeA, Transform2D transformA, MultiShape multiShapeB, Transform2D transformB) + public static bool TestCollision(MultiShape2D multiShapeA, Transform2D transformA, MultiShape2D multiShapeB, Transform2D transformB) { foreach (var (shapeA, shapeTransformA) in multiShapeA.TransformShapesUsingOffset(transformA)) { @@ -226,7 +226,7 @@ namespace MoonTools.Bonk return intersection; } - private static Edge FindClosestEdge(PolygonWinding winding, SimplexVertexBuffer simplexVertices) + private static Edge2D FindClosestEdge(PolygonWinding winding, SimplexVertexBuffer simplexVertices) { var closestDistance = float.PositiveInfinity; var closestNormal = Vector2.Zero; @@ -258,7 +258,7 @@ namespace MoonTools.Bonk } } - return new Edge(closestDistance, closestNormal, closestIndex); + return new Edge2D(closestDistance, closestNormal, closestIndex); } private static Vector2 CalculateSupport(IShape2D shapeA, Transform2D Transform2DA, IShape2D shapeB, Transform2D Transform2DB, Vector2 direction) diff --git a/Bonk/Shapes/Circle.cs b/Bonk/Shapes/2D/Circle.cs similarity index 85% rename from Bonk/Shapes/Circle.cs rename to Bonk/Shapes/2D/Circle.cs index cb6a3f2..f00fd9d 100644 --- a/Bonk/Shapes/Circle.cs +++ b/Bonk/Shapes/2D/Circle.cs @@ -10,12 +10,12 @@ namespace MoonTools.Bonk public struct Circle : IShape2D, IEquatable { public int Radius { get; } - public AABB AABB { get; } + public AABB2D AABB { get; } public Circle(int radius) { Radius = radius; - AABB = new AABB(-Radius, -Radius, Radius, Radius); + AABB = new AABB2D(-Radius, -Radius, Radius, Radius); } public Vector2 Support(Vector2 direction, Transform2D transform) @@ -23,9 +23,9 @@ namespace MoonTools.Bonk return Vector2.Transform(Vector2.Normalize(direction) * Radius, transform.TransformMatrix); } - public AABB TransformedAABB(Transform2D transform2D) + public AABB2D TransformedAABB(Transform2D transform2D) { - return AABB.Transformed(AABB, transform2D); + return AABB2D.Transformed(AABB, transform2D); } public override bool Equals(object obj) diff --git a/Bonk/Shapes/Line.cs b/Bonk/Shapes/2D/Line.cs similarity index 86% rename from Bonk/Shapes/Line.cs rename to Bonk/Shapes/2D/Line.cs index 7ac0d55..5f9625e 100644 --- a/Bonk/Shapes/Line.cs +++ b/Bonk/Shapes/2D/Line.cs @@ -13,7 +13,7 @@ namespace MoonTools.Bonk private Position2D _v0; private Position2D _v1; - public AABB AABB { get; } + public AABB2D AABB { get; } public IEnumerable Vertices { @@ -29,7 +29,7 @@ namespace MoonTools.Bonk _v0 = start; _v1 = end; - AABB = new AABB(Math.Min(_v0.X, _v1.X), Math.Min(_v0.Y, _v1.Y), Math.Max(_v0.X, _v1.X), Math.Max(_v0.Y, _v1.Y)); + AABB = new AABB2D(Math.Min(_v0.X, _v1.X), Math.Min(_v0.Y, _v1.Y), Math.Max(_v0.X, _v1.X), Math.Max(_v0.Y, _v1.Y)); } public Vector2 Support(Vector2 direction, Transform2D transform) @@ -41,9 +41,9 @@ namespace MoonTools.Bonk transformedEnd; } - public AABB TransformedAABB(Transform2D transform) + public AABB2D TransformedAABB(Transform2D transform) { - return AABB.Transformed(AABB, transform); + return AABB2D.Transformed(AABB, transform); } public override bool Equals(object obj) diff --git a/Bonk/Shapes/Point.cs b/Bonk/Shapes/2D/Point.cs similarity index 87% rename from Bonk/Shapes/Point.cs rename to Bonk/Shapes/2D/Point.cs index 5efe53d..2cfb69f 100644 --- a/Bonk/Shapes/Point.cs +++ b/Bonk/Shapes/2D/Point.cs @@ -9,11 +9,11 @@ namespace MoonTools.Bonk /// public struct Point : IShape2D, IEquatable { - public AABB AABB { get; } + public AABB2D AABB { get; } - public AABB TransformedAABB(Transform2D transform) + public AABB2D TransformedAABB(Transform2D transform) { - return AABB.Transformed(AABB, transform); + return AABB2D.Transformed(AABB, transform); } public Vector2 Support(Vector2 direction, Transform2D transform) diff --git a/Bonk/Shapes/Polygon.cs b/Bonk/Shapes/2D/Polygon2D.cs similarity index 74% rename from Bonk/Shapes/Polygon.cs rename to Bonk/Shapes/2D/Polygon2D.cs index 0fa2970..8970e0b 100644 --- a/Bonk/Shapes/Polygon.cs +++ b/Bonk/Shapes/2D/Polygon2D.cs @@ -10,24 +10,24 @@ namespace MoonTools.Bonk /// A Shape defined by an arbitrary collection of vertices. /// NOTE: A Polygon must be defined in clockwise order, have more than 2 vertices, be convex, and have no duplicate vertices. /// - public struct Polygon : IShape2D, IEquatable + public struct Polygon2D : IShape2D, IEquatable { public ImmutableArray Vertices { get; private set; } - public AABB AABB { get; } + public AABB2D AABB { get; } public int VertexCount { get { return Vertices.Length; } } // vertices are local to the origin - public Polygon(IEnumerable vertices) + public Polygon2D(IEnumerable vertices) { Vertices = vertices.ToImmutableArray(); - AABB = AABB.FromVertices(vertices); + AABB = AABB2D.FromVertices(vertices); } - public Polygon(ImmutableArray vertices) + public Polygon2D(ImmutableArray vertices) { Vertices = vertices; - AABB = AABB.FromVertices(vertices); + AABB = AABB2D.FromVertices(vertices); } public Vector2 Support(Vector2 direction, Transform2D transform) @@ -47,9 +47,9 @@ namespace MoonTools.Bonk return maxVertex; } - public AABB TransformedAABB(Transform2D transform) + public AABB2D TransformedAABB(Transform2D transform) { - return AABB.Transformed(AABB, transform); + return AABB2D.Transformed(AABB, transform); } public override bool Equals(object obj) @@ -59,10 +59,10 @@ namespace MoonTools.Bonk public bool Equals(IShape2D other) { - return other is Polygon otherPolygon && Equals(otherPolygon); + return other is Polygon2D otherPolygon && Equals(otherPolygon); } - public bool Equals(Polygon other) + public bool Equals(Polygon2D other) { if (VertexCount != other.VertexCount) { return false; } @@ -87,22 +87,22 @@ namespace MoonTools.Bonk return HashCode.Combine(Vertices); } - public static bool operator ==(Polygon a, Polygon b) + public static bool operator ==(Polygon2D a, Polygon2D b) { return a.Equals(b); } - public static bool operator !=(Polygon a, Polygon b) + public static bool operator !=(Polygon2D a, Polygon2D b) { return !(a == b); } - public static bool operator ==(Polygon a, Rectangle b) + public static bool operator ==(Polygon2D a, Rectangle b) { return a.Equals(b); } - public static bool operator !=(Polygon a, Rectangle b) + public static bool operator !=(Polygon2D a, Rectangle b) { return !(a == b); } diff --git a/Bonk/Shapes/Rectangle.cs b/Bonk/Shapes/2D/Rectangle.cs similarity index 90% rename from Bonk/Shapes/Rectangle.cs rename to Bonk/Shapes/2D/Rectangle.cs index 0dedfec..b01c0d6 100644 --- a/Bonk/Shapes/Rectangle.cs +++ b/Bonk/Shapes/2D/Rectangle.cs @@ -9,7 +9,7 @@ namespace MoonTools.Bonk /// public struct Rectangle : IShape2D, IEquatable { - public AABB AABB { get; } + public AABB2D AABB { get; } public int Width { get; } public int Height { get; } @@ -31,7 +31,7 @@ namespace MoonTools.Bonk Right = left + width; Top = top; Bottom = top + height; - AABB = new AABB(left, top, Right, Bottom); + AABB = new AABB2D(left, top, Right, Bottom); BottomLeft = new Vector2(Left, Bottom); TopRight = new Vector2(Top, Right); Min = AABB.Min; @@ -70,9 +70,9 @@ namespace MoonTools.Bonk return Vector2.Transform(Support(inverseDirection), transform.TransformMatrix); } - public AABB TransformedAABB(Transform2D transform) + public AABB2D TransformedAABB(Transform2D transform) { - return AABB.Transformed(AABB, transform); + return AABB2D.Transformed(AABB, transform); } public override bool Equals(object obj) @@ -105,12 +105,12 @@ namespace MoonTools.Bonk return !(a == b); } - public static bool operator ==(Rectangle a, Polygon b) + public static bool operator ==(Rectangle a, Polygon2D b) { return a.Equals(b); } - public static bool operator !=(Rectangle a, Polygon b) + public static bool operator !=(Rectangle a, Polygon2D b) { return !(a == b); } diff --git a/Bonk/Simplex.cs b/Bonk/Simplex2D.cs similarity index 100% rename from Bonk/Simplex.cs rename to Bonk/Simplex2D.cs diff --git a/Bonk/SweepTest/SweepTest.cs b/Bonk/SweepTest/SweepTest.cs index da2b9cc..1153f88 100644 --- a/Bonk/SweepTest/SweepTest.cs +++ b/Bonk/SweepTest/SweepTest.cs @@ -15,7 +15,7 @@ namespace MoonTools.Bonk /// A transform by which to transform the IHasAABB2D. /// Given in world-space. /// - public static SweepResult Test(SpatialHash spatialHash, Rectangle rectangle, Transform2D transform, Vector2 ray) where T : IEquatable + public static SweepResult Test(SpatialHash2D spatialHash, Rectangle rectangle, Transform2D transform, Vector2 ray) where T : IEquatable { var transformedAABB = rectangle.TransformedAABB(transform); var sweepBox = SweepBox(transformedAABB, ray); @@ -29,14 +29,14 @@ namespace MoonTools.Bonk { Rectangle otherRectangle; Transform2D otherTransform; - AABB otherTransformedAABB; + AABB2D otherTransformedAABB; if (shape is Rectangle) { otherRectangle = (Rectangle)shape; otherTransformedAABB = shape.TransformedAABB(shapeTransform); otherTransform = shapeTransform; } - else if (shape is MultiShape multiShape && multiShape.IsSingleShape()) + else if (shape is MultiShape2D multiShape && multiShape.IsSingleShape()) { Transform2D rectangleOffset; (otherRectangle, rectangleOffset) = multiShape.ShapeTransformPair(); @@ -116,14 +116,14 @@ namespace MoonTools.Bonk } } - public static SweepResult Test(SpatialHash spatialHash, Point point, Transform2D transform, Vector2 ray) where T : IEquatable + public static SweepResult Test(SpatialHash2D spatialHash, Point point, Transform2D transform, Vector2 ray) where T : IEquatable { return Test(spatialHash, new Rectangle(0, 0, 0, 0), transform, ray); } - private static AABB SweepBox(AABB aabb, Vector2 ray) + private static AABB2D SweepBox(AABB2D aabb, Vector2 ray) { - return new AABB( + return new AABB2D( Math.Min(aabb.Min.X, aabb.Min.X + ray.X), Math.Min(aabb.Min.Y, aabb.Min.Y + ray.Y), Math.Max(aabb.Max.X, aabb.Max.X + ray.X), diff --git a/Test/AABB2DTest.cs b/Test/AABB2DTest.cs new file mode 100644 index 0000000..94fc38e --- /dev/null +++ b/Test/AABB2DTest.cs @@ -0,0 +1,42 @@ +using NUnit.Framework; +using FluentAssertions; +using MoonTools.Bonk; +using System.Numerics; + +namespace Tests +{ + public class AABB2DTest + { + [Test] + public void Overlapping() + { + var a = new AABB2D(new Vector2(-1, -1), new Vector2(1, 1)); + var b = new AABB2D(new Vector2(0, 0), new Vector2(2, 2)); + + AABB2D.TestOverlap(a, b).Should().BeTrue(); + + var c = new AABB2D(-2, -2, 2, 1); + var d = new AABB2D(-2, -2, 2, 2); + + AABB2D.TestOverlap(c, d).Should().BeTrue(); + } + + [Test] + public void NotOverlapping() + { + var a = new AABB2D(new Vector2(-1, -1), new Vector2(1, 1)); + var b = new AABB2D(new Vector2(-3, -3), new Vector2(-2, -2)); + + AABB2D.TestOverlap(a, b).Should().BeFalse(); + } + + [Test] + public void Merge() + { + var a = new AABB2D(new Vector2(-1, -2), new Vector2(2, 1)); + var b = new AABB2D(new Vector2(-3, -1), new Vector2(3, 1)); + + a.Merge(b).Should().Be(new AABB2D(new Vector2(-3, -2), new Vector2(3, 1))); + } + } +} diff --git a/Test/AABB3DTest.cs b/Test/AABB3DTest.cs new file mode 100644 index 0000000..dc2f84f --- /dev/null +++ b/Test/AABB3DTest.cs @@ -0,0 +1,42 @@ +using NUnit.Framework; +using FluentAssertions; +using MoonTools.Bonk; +using System.Numerics; + +namespace Tests +{ + public class AABB3DTest + { + [Test] + public void Overlapping() + { + var a = new AABB3D(new Vector3(-1, -1, -1), new Vector3(1, 1, 1)); + var b = new AABB3D(new Vector3(0, 0, 0), new Vector3(2, 2, 2)); + + AABB3D.TestOverlap(a, b).Should().BeTrue(); + + var c = new AABB3D(-2, -2, 2, 2, 1, 2); + var d = new AABB3D(-2, -2, 2, 2, 2, 2); + + AABB3D.TestOverlap(c, d).Should().BeTrue(); + } + + [Test] + public void NotOverlapping() + { + var a = new AABB3D(new Vector3(-1, -1, -1), new Vector3(1, 1, 1)); + var b = new AABB3D(new Vector3(-3, -3, -3), new Vector3(-2, -2, -1)); + + AABB3D.TestOverlap(a, b).Should().BeFalse(); + } + + [Test] + public void Merge() + { + var a = new AABB3D(new Vector3(-1, -1, -1), new Vector3(2, 2, 2)); + var b = new AABB3D(new Vector3(-2, -2, -1), new Vector3(3, 1, 3)); + + a.Merge(b).Should().Be(new AABB3D(new Vector3(-2, -2, -1), new Vector3(3, 2, 3))); + } + } +} diff --git a/Test/AABBTest.cs b/Test/AABBTest.cs deleted file mode 100644 index e841c15..0000000 --- a/Test/AABBTest.cs +++ /dev/null @@ -1,33 +0,0 @@ -using NUnit.Framework; -using FluentAssertions; -using MoonTools.Bonk; -using System.Numerics; - -namespace Tests -{ - public class AABBTest - { - [Test] - public void Overlapping() - { - var a = new AABB(new Vector2(-1, -1), new Vector2(1, 1)); - var b = new AABB(new Vector2(0, 0), new Vector2(2, 2)); - - AABB.TestOverlap(a, b).Should().BeTrue(); - - var c = new AABB(-2, -2, 2, 1); - var d = new AABB(-2, -2, 2, 2); - - AABB.TestOverlap(c, d).Should().BeTrue(); - } - - [Test] - public void NotOverlapping() - { - var a = new AABB(new Vector2(-1, -1), new Vector2(1, 1)); - var b = new AABB(new Vector2(-3, -3), new Vector2(-2, -2)); - - AABB.TestOverlap(a, b).Should().BeFalse(); - } - } -} diff --git a/Test/Equality.cs b/Test/Equality.cs index 62665b4..7f7f4f0 100644 --- a/Test/Equality.cs +++ b/Test/Equality.cs @@ -179,13 +179,13 @@ namespace Tests [Test] public void PolygonEqual() { - var a = new Polygon(ImmutableArray.Create( + var a = new Polygon2D(ImmutableArray.Create( new Position2D(0, 1), new Position2D(1, 2), new Position2D(-1, -1) )); - var b = new Polygon(ImmutableArray.Create( + var b = new Polygon2D(ImmutableArray.Create( new Position2D(0, 1), new Position2D(1, 2), new Position2D(-1, -1) @@ -197,13 +197,13 @@ namespace Tests [Test] public void PolygonEqualOperator() { - var a = new Polygon(ImmutableArray.Create( + var a = new Polygon2D(ImmutableArray.Create( new Position2D(0, 1), new Position2D(1, 2), new Position2D(-1, -1) )); - var b = new Polygon(ImmutableArray.Create( + var b = new Polygon2D(ImmutableArray.Create( new Position2D(0, 1), new Position2D(1, 2), new Position2D(-1, -1) @@ -215,13 +215,13 @@ namespace Tests [Test] public void PolygonDifferentOrderEqual() { - var a = new Polygon(ImmutableArray.Create( + var a = new Polygon2D(ImmutableArray.Create( new Position2D(0, 1), new Position2D(1, 2), new Position2D(-1, -1) )); - var b = new Polygon(ImmutableArray.Create( + var b = new Polygon2D(ImmutableArray.Create( new Position2D(1, 2), new Position2D(-1, -1), new Position2D(0, 1) @@ -233,13 +233,13 @@ namespace Tests [Test] public void PolygonDifferentOrderEqualOperator() { - var a = new Polygon(ImmutableArray.Create( + var a = new Polygon2D(ImmutableArray.Create( new Position2D(0, 1), new Position2D(1, 2), new Position2D(-1, -1) )); - var b = new Polygon(ImmutableArray.Create( + var b = new Polygon2D(ImmutableArray.Create( new Position2D(1, 2), new Position2D(-1, -1), new Position2D(0, 1) @@ -251,13 +251,13 @@ namespace Tests [Test] public void PolygonNotEqual() { - var a = new Polygon(ImmutableArray.Create( + var a = new Polygon2D(ImmutableArray.Create( new Position2D(0, 1), new Position2D(1, 2), new Position2D(-1, -1) )); - var b = new Polygon(ImmutableArray.Create( + var b = new Polygon2D(ImmutableArray.Create( new Position2D(1, 0), new Position2D(2, 1), new Position2D(-1, -1) @@ -269,13 +269,13 @@ namespace Tests [Test] public void PolygonNotEqualOperator() { - var a = new Polygon(ImmutableArray.Create( + var a = new Polygon2D(ImmutableArray.Create( new Position2D(0, 1), new Position2D(1, 2), new Position2D(-1, -1) )); - var b = new Polygon(ImmutableArray.Create( + var b = new Polygon2D(ImmutableArray.Create( new Position2D(1, 0), new Position2D(2, 1), new Position2D(-1, -1) @@ -465,8 +465,8 @@ namespace Tests [Test] public void Equal() { - var aabb = new AABB(0, 0, 3, 3); - var other = new AABB(0, 0, 3, 3); + var aabb = new AABB2D(0, 0, 3, 3); + var other = new AABB2D(0, 0, 3, 3); (aabb == other).Should().BeTrue(); } @@ -474,8 +474,8 @@ namespace Tests [Test] public void NotEqual() { - var aabb = new AABB(0, 0, 3, 3); - var other = new AABB(0, 0, 6, 6); + var aabb = new AABB2D(0, 0, 3, 3); + var other = new AABB2D(0, 0, 6, 6); (aabb != other).Should().BeTrue(); } diff --git a/Test/NarrowPhaseTest.cs b/Test/NarrowPhaseTest.cs index ed90965..9f3c6d9 100644 --- a/Test/NarrowPhaseTest.cs +++ b/Test/NarrowPhaseTest.cs @@ -75,7 +75,7 @@ namespace Tests { var point = new Point(); var pointTransform = new Transform2D(new Position2D(1, 1)); - var polygon = new Polygon(ImmutableArray.Create( + var polygon = new Polygon2D(ImmutableArray.Create( new Position2D(-2, -2), new Position2D(-3, 2), new Position2D(3, 2), @@ -90,7 +90,7 @@ namespace Tests { var point = new Point(); var pointTransform = new Transform2D(new Position2D(5, 5)); - var polygon = new Polygon(ImmutableArray.Create( + var polygon = new Polygon2D(ImmutableArray.Create( new Position2D(-2, -2), new Position2D(-3, 2), new Position2D(3, 2), @@ -187,14 +187,14 @@ namespace Tests [Test] public void PolygonPolygonOverlapping() { - var shapeA = new Polygon(ImmutableArray.Create( + var shapeA = new Polygon2D(ImmutableArray.Create( new Position2D(-1, 1), new Position2D(1, 1), new Position2D(-1, -1), new Position2D(1, -1) )); var transformA = Transform2D.DefaultTransform; - var shapeB = new Polygon(ImmutableArray.Create( + var shapeB = new Polygon2D(ImmutableArray.Create( new Position2D(-1, 1), new Position2D(1, 1), new Position2D(-1, -1), new Position2D(1, -1) )); @@ -207,14 +207,14 @@ namespace Tests [Test] public void ScaledPolygonsOverlapping() { - var shapeA = new Polygon(ImmutableArray.Create( + var shapeA = new Polygon2D(ImmutableArray.Create( new Position2D(-1, 1), new Position2D(1, 1), new Position2D(-1, -1), new Position2D(1, -1) )); var transformA = Transform2D.DefaultTransform; - var shapeB = new Polygon(ImmutableArray.Create( + var shapeB = new Polygon2D(ImmutableArray.Create( new Position2D(-1, 1), new Position2D(1, 1), new Position2D(-1, -1), new Position2D(1, -1) )); @@ -227,14 +227,14 @@ namespace Tests [Test] public void PolygonPolygonNotOverlapping() { - var shapeA = new Polygon(ImmutableArray.Create( + var shapeA = new Polygon2D(ImmutableArray.Create( new Position2D(-1, 1), new Position2D(1, 1), new Position2D(-1, -1), new Position2D(1, -1) )); var transformA = Transform2D.DefaultTransform; - var shapeB = new Polygon(ImmutableArray.Create( + var shapeB = new Polygon2D(ImmutableArray.Create( new Position2D(-1, 1), new Position2D(1, 1), new Position2D(-1, -1), new Position2D(1, -1) )); @@ -247,14 +247,14 @@ namespace Tests [Test] public void ScaledPolygonsNotOverlapping() { - var shapeA = new Polygon(ImmutableArray.Create( + var shapeA = new Polygon2D(ImmutableArray.Create( new Position2D(-1, 1), new Position2D(1, 1), new Position2D(-1, -1), new Position2D(1, -1) )); var transformA = Transform2D.DefaultTransform; - var shapeB = new Polygon(ImmutableArray.Create( + var shapeB = new Polygon2D(ImmutableArray.Create( new Position2D(-2, 2), new Position2D(2, 2), new Position2D(-2, -2), new Position2D(2, -2) )); @@ -271,7 +271,7 @@ namespace Tests var transformA = Transform2D.DefaultTransform; - var polygon = new Polygon(ImmutableArray.Create( + var polygon = new Polygon2D(ImmutableArray.Create( new Position2D(-1, -1), new Position2D(1, -1), new Position2D(1, 1), new Position2D(-1, 1) )); @@ -288,7 +288,7 @@ namespace Tests var transformA = Transform2D.DefaultTransform; - var polygon = new Polygon(ImmutableArray.Create( + var polygon = new Polygon2D(ImmutableArray.Create( new Position2D(-1, -1), new Position2D(1, -1), new Position2D(1, 1), new Position2D(-1, 1) )); @@ -326,7 +326,7 @@ namespace Tests var circle = new Circle(1); var transformA = new Transform2D(new Vector2(0.25f, 0)); - var square = new Polygon(ImmutableArray.Create( + var square = new Polygon2D(ImmutableArray.Create( new Position2D(-1, -1), new Position2D(1, -1), new Position2D(1, 1), new Position2D(-1, 1) )); @@ -342,7 +342,7 @@ namespace Tests var circle = new Circle(1); var circleTransform = new Transform2D(new Vector2(5, 0)); - var square = new Polygon(ImmutableArray.Create( + var square = new Polygon2D(ImmutableArray.Create( new Position2D(-1, -1), new Position2D(1, -1), new Position2D(1, 1), new Position2D(-1, 1) )); @@ -426,7 +426,7 @@ namespace Tests [Test] public void MultiRectanglesOverlapping() { - var multiRectangleA = new MultiShape( + var multiRectangleA = new MultiShape2D( ImmutableArray.Create<(IShape2D, Transform2D)>( (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 0))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 1))), @@ -435,7 +435,7 @@ namespace Tests ); var transformA = new Transform2D(new Position2D(5, 0)); - var multiRectangleB = new MultiShape( + var multiRectangleB = new MultiShape2D( ImmutableArray.Create<(IShape2D, Transform2D)>( (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, -1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 0))), @@ -450,7 +450,7 @@ namespace Tests [Test] public void MultiRectanglesNotOverlapping() { - var multiRectangleA = new MultiShape( + var multiRectangleA = new MultiShape2D( ImmutableArray.Create<(IShape2D, Transform2D)>( (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 0))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 1))), @@ -459,7 +459,7 @@ namespace Tests ); var transformA = new Transform2D(new Position2D(5, 0)); - var multiRectangleB = new MultiShape( + var multiRectangleB = new MultiShape2D( ImmutableArray.Create<(IShape2D, Transform2D)>( (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, -1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 0))), diff --git a/Test/SpatialHashTest.cs b/Test/SpatialHashTest.cs index 11e9a98..0acd2de 100644 --- a/Test/SpatialHashTest.cs +++ b/Test/SpatialHashTest.cs @@ -12,7 +12,7 @@ namespace Tests [Test] public void InsertAndRetrieve() { - var spatialHash = new SpatialHash(16); + var spatialHash = new SpatialHash2D(16); var rectA = new Rectangle(-2, -2, 4, 4); var rectATransform = new Transform2D(new Vector2(-8, -8)); @@ -38,7 +38,7 @@ namespace Tests var point = new Point(); var pointTransform = new Transform2D(new Position2D(8, 8)); - var multiRectangle = new MultiShape( + var multiRectangle = new MultiShape2D( ImmutableArray.Create<(IShape2D, Transform2D)>( (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-2, -2))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-2, -1))), @@ -76,7 +76,7 @@ namespace Tests [Test] public void InsertAndRetrieveSameValues() { - var spatialHash = new SpatialHash(16); + var spatialHash = new SpatialHash2D(16); var rectA = new Rectangle(-2, -2, 4, 4); var rectATransform = new Transform2D(new Vector2(-8, -8)); @@ -97,7 +97,7 @@ namespace Tests [Test] public void Clear() { - var spatialHash = new SpatialHash(16); + var spatialHash = new SpatialHash2D(16); var rectA = new Rectangle(-2, -2, 4, 4); var rectATransform = new Transform2D(new Vector2(-8, -8)); diff --git a/Test/SweepTestTest.cs b/Test/SweepTestTest.cs index d2b94a1..239a76a 100644 --- a/Test/SweepTestTest.cs +++ b/Test/SweepTestTest.cs @@ -23,7 +23,7 @@ namespace Tests var downRectangle = new Rectangle(-6, -2, 12, 4); var downTransform = new Transform2D(new Position2D(-6, 20)); - var spatialHash = new SpatialHash(16); + var spatialHash = new SpatialHash2D(16); spatialHash.Insert(1, otherRectangle, otherTransform); spatialHash.Insert(2, farthestRectangle, farthestTransform); spatialHash.Insert(3, downRectangle, downTransform);