Compare commits

..

2 Commits
master ... 3D

Author SHA1 Message Date
cosmonaut 2f341aaeae corners method
continuous-integration/drone/push Build is passing Details
2020-09-16 12:59:10 -07:00
cosmonaut 6bda2e5ab4 specifying 2D and adds AABB3D
continuous-integration/drone/push Build is passing Details
2020-09-06 19:38:51 -07:00
25 changed files with 295 additions and 158 deletions

View File

@ -8,7 +8,7 @@ namespace MoonTools.Bonk
/// <summary>
/// Axis-aligned bounding box.
/// </summary>
public struct AABB : IEquatable<AABB>
public struct AABB2D : IEquatable<AABB2D>
{
/// <summary>
/// The top-left position of the AABB.
@ -39,13 +39,13 @@ namespace MoonTools.Bonk
/// <value></value>
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
/// <param name="aabb"></param>
/// <param name="transform"></param>
/// <returns></returns>
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);
}
/// <summary>
@ -84,7 +84,7 @@ namespace MoonTools.Bonk
/// </summary>
/// <param name="vertices"></param>
/// <returns></returns>
public static AABB FromVertices(IEnumerable<Position2D> vertices)
public static AABB2D FromVertices(IEnumerable<Position2D> 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);
}

79
Bonk/AABB3D.cs Normal file
View File

@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using MoonTools.Structs;
namespace MoonTools.Bonk
{
/// <summary>
/// Axis-aligned bounding box in 3 dimensions.
/// </summary>
public struct AABB3D : IEquatable<AABB3D>
{
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 IEnumerable<Vector3> AABBCorners()
{
yield return Min;
yield return new Vector3(Min.X, Min.Y, Max.Z);
yield return new Vector3(Min.X, Max.Y, Min.Z);
yield return new Vector3(Max.X, Min.Y, Min.Z);
yield return new Vector3(Min.X, Max.Y, Max.Z);
yield return new Vector3(Max.X, Min.Y, Max.Z);
yield return new Vector3(Max.X, Max.Y, Min.Z);
yield return 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;
}
}
}

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>8.0.1</Version>
<Version>8.0.0</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>.NET Standard High Performance Collision Detection</Description>
<PackageId>MoonTools.Bonk</PackageId>
@ -14,16 +14,15 @@
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageProjectUrl>https://gitea.moonside.games/MoonsideGames/MoonTools.Bonk</PackageProjectUrl>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>x64</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Collections.Immutable" Version="1.7.1" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.0" />
<PackageReference Include="MoonTools.Structs" Version="3.0.1" />
<PackageReference Include="System.Collections.Immutable" Version="1.7.0"/>
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0"/>
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.0"/>
<PackageReference Include="MoonTools.Structs" Version="3.0.1"/>
</ItemGroup>
</Project>

View File

@ -9,7 +9,7 @@ namespace MoonTools.Bonk
/// Used to quickly check if two shapes are potentially overlapping.
/// </summary>
/// <typeparam name="T">The type that will be used to uniquely identify shape-transform pairs.</typeparam>
public class SpatialHash<T> where T : IEquatable<T>
public class SpatialHash2D<T> where T : IEquatable<T>
{
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
/// </summary>
/// <param name="aabb">A transformed AABB.</param>
/// <returns></returns>
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);
}

View File

@ -4,13 +4,13 @@ namespace MoonTools.Bonk
{
public interface IHasAABB2D
{
AABB AABB { get; }
AABB2D AABB { get; }
/// <summary>
/// Returns a bounding box based on the shape.
/// </summary>
/// <param name="transform">A Transform for transforming the shape vertices.</param>
/// <returns>Returns a bounding box based on the shape.</returns>
AABB TransformedAABB(Transform2D transform);
AABB2D TransformedAABB(Transform2D transform);
}
}

View File

@ -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);
}
/// <summary>
@ -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);
}
}
}

View File

@ -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;

View File

@ -16,17 +16,17 @@ namespace MoonTools.Bonk
/// </summary>
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
/// <param name="shape"></param>
/// <param name="shapeTransform"></param>
/// <returns></returns>
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
/// <param name="shape"></param>
/// <param name="shapeTransform"></param>
/// <returns></returns>
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
/// <param name="multiShapeB"></param>
/// <param name="transformB"></param>
/// <returns></returns>
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)

View File

@ -10,12 +10,12 @@ namespace MoonTools.Bonk
public struct Circle : IShape2D, IEquatable<Circle>
{
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)

View File

@ -13,7 +13,7 @@ namespace MoonTools.Bonk
private Position2D _v0;
private Position2D _v1;
public AABB AABB { get; }
public AABB2D AABB { get; }
public IEnumerable<Position2D> 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)

View File

@ -9,11 +9,11 @@ namespace MoonTools.Bonk
/// </summary>
public struct Point : IShape2D, IEquatable<Point>
{
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)

View File

@ -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.
/// </summary>
public struct Polygon : IShape2D, IEquatable<Polygon>
public struct Polygon2D : IShape2D, IEquatable<Polygon2D>
{
public ImmutableArray<Position2D> 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<Position2D> vertices)
public Polygon2D(IEnumerable<Position2D> vertices)
{
Vertices = vertices.ToImmutableArray();
AABB = AABB.FromVertices(vertices);
AABB = AABB2D.FromVertices(vertices);
}
public Polygon(ImmutableArray<Position2D> vertices)
public Polygon2D(ImmutableArray<Position2D> 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);
}

View File

@ -9,7 +9,7 @@ namespace MoonTools.Bonk
/// </summary>
public struct Rectangle : IShape2D, IEquatable<Rectangle>
{
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);
}

View File

@ -15,7 +15,7 @@ namespace MoonTools.Bonk
/// <param name="transform">A transform by which to transform the IHasAABB2D.</param>
/// <param name="ray">Given in world-space.</param>
/// <returns></returns>
public static SweepResult<T> Test<T>(SpatialHash<T> spatialHash, Rectangle rectangle, Transform2D transform, Vector2 ray) where T : IEquatable<T>
public static SweepResult<T> Test<T>(SpatialHash2D<T> spatialHash, Rectangle rectangle, Transform2D transform, Vector2 ray) where T : IEquatable<T>
{
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<Rectangle>())
else if (shape is MultiShape2D multiShape && multiShape.IsSingleShape<Rectangle>())
{
Transform2D rectangleOffset;
(otherRectangle, rectangleOffset) = multiShape.ShapeTransformPair<Rectangle>();
@ -116,14 +116,14 @@ namespace MoonTools.Bonk
}
}
public static SweepResult<T> Test<T>(SpatialHash<T> spatialHash, Point point, Transform2D transform, Vector2 ray) where T : IEquatable<T>
public static SweepResult<T> Test<T>(SpatialHash2D<T> spatialHash, Point point, Transform2D transform, Vector2 ray) where T : IEquatable<T>
{
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),

View File

@ -1,7 +1,7 @@
# MoonTools.Bonk
[![NuGet Badge](https://buildstats.info/nuget/MoonTools.Bonk)](https://www.nuget.org/packages/MoonTools.Bonk/)
[![Build Status](https://gitea.drone.moonside.games/api/badges/MoonsideGames/MoonTools.Bonk/status.svg)](https://gitea.drone.moonside.games/MoonsideGames/MoonTools.Bonk)
[![Build Status](https://drone.moonside.games/api/badges/MoonsideGames/MoonTools.Bonk/status.svg)](https://drone.moonside.games/MoonsideGames/MoonTools.Bonk)
Bonk is a fast and modular collision detection system for .NET that is part of the MoonTools suite. It can be used with any .NET-based game engine.

42
Test/AABB2DTest.cs Normal file
View File

@ -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)));
}
}
}

42
Test/AABB3DTest.cs Normal file
View File

@ -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)));
}
}
}

View File

@ -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();
}
}
}

View File

@ -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();
}

View File

@ -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))),

View File

@ -12,7 +12,7 @@ namespace Tests
[Test]
public void InsertAndRetrieve()
{
var spatialHash = new SpatialHash<int>(16);
var spatialHash = new SpatialHash2D<int>(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<int>(16);
var spatialHash = new SpatialHash2D<int>(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<int>(16);
var spatialHash = new SpatialHash2D<int>(16);
var rectA = new Rectangle(-2, -2, 4, 4);
var rectATransform = new Transform2D(new Vector2(-8, -8));

View File

@ -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<int>(16);
var spatialHash = new SpatialHash2D<int>(16);
spatialHash.Insert(1, otherRectangle, otherTransform);
spatialHash.Insert(2, farthestRectangle, farthestTransform);
spatialHash.Insert(3, downRectangle, downTransform);

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
@ -12,4 +12,4 @@
<ItemGroup>
<ProjectReference Include="..\Bonk\Bonk.csproj" />
</ItemGroup>
</Project>
</Project>