Compare commits

...

20 Commits

Author SHA1 Message Date
cosmonaut c7fa0932d6 update drone
continuous-integration/drone/push Build is passing Details
2021-01-04 23:41:19 -08:00
cosmonaut e57bae4c44 bump immutable collections
continuous-integration/drone/pr Build is passing Details
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2020-12-21 14:30:40 -08:00
cosmonaut 094cf4b3d2 specify x64
continuous-integration/drone/push Build is passing Details
2020-11-22 16:54:28 -08:00
Evan Hemsley e6bab0de5e some minor cleanups
continuous-integration/drone/push Build is passing Details
2020-07-18 13:19:36 -07:00
Evan Hemsley b152755481 8.0.0
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2020-02-21 00:21:54 -08:00
Evan Hemsley 401f827b69 integrate namespace changes
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is failing Details
2020-02-21 00:03:47 -08:00
Evan Hemsley bcc0d0abc2 Merge remote-tracking branch 'github/multishape' 2020-02-21 00:01:02 -08:00
Evan Hemsley 137f0a90b5 add droneCI badge
continuous-integration/drone/push Build is passing Details
2020-02-20 18:39:41 -08:00
Evan Hemsley 29ce203452 7.0.1
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2020-02-20 18:32:10 -08:00
Evan Hemsley fa42ae77be forgot reference to core
continuous-integration/drone/push Build is passing Details
2020-02-20 18:30:00 -08:00
Evan Hemsley a46dd831d3 7.0.0
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2020-02-20 18:14:49 -08:00
Evan Hemsley 777af13171 change to droneCI
continuous-integration/drone/push Build is passing Details
2020-02-20 18:13:29 -08:00
Evan Hemsley d0b72d1749 change namespace 2020-02-20 18:07:59 -08:00
Evan Hemsley 108e0573c2 spatial hash optimization 2020-01-17 02:52:38 -08:00
Evan Hemsley 1fb768e7b5 rectangles now define top-left-width-height so that we dont have subpixel bounds 2020-01-06 20:06:21 -08:00
Evan Hemsley 20df172027 fix sweep test test 2020-01-06 01:38:13 -08:00
Evan Hemsley 7875342da8 change how multishape shapes are transformed and add point sweep test 2020-01-05 21:36:31 -08:00
Evan Hemsley 17084154a2 change multishape test transform behavior 2020-01-05 13:35:49 -08:00
Evan Hemsley 38f5180a45 fix sweep test correction sign 2020-01-05 13:14:06 -08:00
Evan Hemsley 354912d674 fixing sweep test 2020-01-05 13:10:52 -08:00
29 changed files with 330 additions and 246 deletions

View File

@ -1,46 +0,0 @@
version: 2.1
defaults: &defaults
working_directory: ~/repo
docker:
- image: mcr.microsoft.com/dotnet/core/sdk:3.0
environment:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
jobs:
test:
<<: *defaults
steps:
- checkout
- run: dotnet restore
- run: dotnet build -c Release
- run: dotnet test -c Release
- persist_to_workspace:
root: .
paths: ./Bonk/bin
deploy:
<<: *defaults
steps:
- checkout
- attach_workspace:
at: .
- run: dotnet nuget push ./Bonk/bin/Release/MoonTools.Core.Bonk.*.nupkg -k $API_KEY -s https://api.nuget.org/v3/index.json
workflows:
version: 2
test_and_deploy:
jobs:
- test:
filters:
tags:
only: /.*/
- deploy:
requires:
- test
filters:
branches:
ignore: /.*/
tags:
only: /^\d+\.\d+\.\d+(-preview\d*)?$/

24
.drone.yml Normal file
View File

@ -0,0 +1,24 @@
kind: pipeline
type: docker
name: default
workspace:
path: /build
steps:
- name: test
image: mcr.microsoft.com/dotnet/core/sdk:3.1
commands:
- dotnet build -c Release
- dotnet test -c Release
- name: deploy
image: mcr.microsoft.com/dotnet/core/sdk:3.1
environment:
API_KEY:
from_secret: API_KEY
commands:
- dotnet nuget push /build/Bonk/bin/Release/MoonTools.Bonk.*.nupkg -s https://api.nuget.org/v3/index.json -k $API_KEY
when:
ref:
- refs/tags/*.*.*

View File

@ -1,9 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
/// <summary> /// <summary>
/// Axis-aligned bounding box. /// Axis-aligned bounding box.
@ -26,11 +26,13 @@ namespace MoonTools.Core.Bonk
public float Right { get { return Max.X; } } public float Right { get { return Max.X; } }
public float Left { get { return Min.X; } } public float Left { get { return Min.X; } }
/// <summary> /// <summary>
/// The top of the AABB. Assumes a downward-aligned Y axis, so this value will be smaller than Bottom. /// The top of the AABB. Assumes a downward-aligned Y axis, so this value will be smaller than Bottom.
/// </summary> /// </summary>
/// <value></value> /// <value></value>
public float Top { get { return Min.Y; } } public float Top { get { return Min.Y; } }
/// <summary> /// <summary>
/// The bottom of the AABB. Assumes a downward-aligned Y axis, so this value will be larger than Top. /// The bottom of the AABB. Assumes a downward-aligned Y axis, so this value will be larger than Top.
/// </summary> /// </summary>

View File

@ -1,28 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<Version>6.0.0</Version> <Version>8.0.1</Version>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<Description>.NET Core High Performance Collision Detection</Description> <Description>.NET Standard High Performance Collision Detection</Description>
<PackageId>MoonTools.Core.Bonk</PackageId> <PackageId>MoonTools.Bonk</PackageId>
<RootNamespace>MoonTools.Core.Bonk</RootNamespace> <RootNamespace>MoonTools.Bonk</RootNamespace>
<Company>Moonside Games</Company> <Company>Moonside Games</Company>
<Authors>Evan Hemsley</Authors> <Authors>Evan Hemsley</Authors>
<Copyright>Evan Hemsley 2019</Copyright> <Copyright>Evan Hemsley 2019</Copyright>
<Product>MoonTools.Core.Bonk</Product> <Product>MoonTools.Bonk</Product>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyName>MoonTools.Core.Bonk</AssemblyName> <AssemblyName>MoonTools.Bonk</AssemblyName>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression> <PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/MoonsideGames/MoonTools.Core.Bonk</PackageProjectUrl> <PackageProjectUrl>https://gitea.moonside.games/MoonsideGames/MoonTools.Bonk</PackageProjectUrl>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>x64</Platforms>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8"> <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="MoonTools.Core.Structs" Version="3.0.0" /> <PackageReference Include="System.Collections.Immutable" Version="1.7.1" />
<PackageReference Include="System.Collections.Immutable" Version="1.7.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" /> <PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.0" /> <PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.0" />
<PackageReference Include="MoonTools.Structs" Version="3.0.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,9 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
/// <summary> /// <summary>
/// Used to quickly check if two shapes are potentially overlapping. /// Used to quickly check if two shapes are potentially overlapping.
@ -16,6 +16,12 @@ namespace MoonTools.Core.Bonk
private readonly Dictionary<long, HashSet<T>> hashDictionary = new Dictionary<long, HashSet<T>>(); private readonly Dictionary<long, HashSet<T>> hashDictionary = new Dictionary<long, HashSet<T>>();
private readonly Dictionary<T, (IHasAABB2D, Transform2D)> IDLookup = new Dictionary<T, (IHasAABB2D, Transform2D)>(); private readonly Dictionary<T, (IHasAABB2D, Transform2D)> IDLookup = new Dictionary<T, (IHasAABB2D, Transform2D)>();
public int MinX { get; private set; } = 0;
public int MaxX { get; private set; } = 0;
public int MinY { get; private set; } = 0;
public int MaxY { get; private set; } = 0;
public SpatialHash(int cellSize) public SpatialHash(int cellSize)
{ {
this.cellSize = cellSize; this.cellSize = cellSize;
@ -52,6 +58,11 @@ namespace MoonTools.Core.Bonk
IDLookup[id] = (shape, transform2D); IDLookup[id] = (shape, transform2D);
} }
} }
MinX = Math.Min(MinX, minHash.Item1);
MinY = Math.Min(MinY, minHash.Item2);
MaxX = Math.Max(MaxX, maxHash.Item1);
MaxY = Math.Max(MaxY, maxHash.Item2);
} }
/// <summary> /// <summary>
@ -60,12 +71,17 @@ namespace MoonTools.Core.Bonk
public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(T id, IHasAABB2D shape, Transform2D transform2D) public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(T id, IHasAABB2D shape, Transform2D transform2D)
{ {
var box = shape.TransformedAABB(transform2D); var box = shape.TransformedAABB(transform2D);
var minHash = Hash(box.Min); var (minX, minY) = Hash(box.Min);
var maxHash = Hash(box.Max); var (maxX, maxY) = Hash(box.Max);
for (var i = minHash.Item1; i <= maxHash.Item1; i++) if (minX < MinX) { minX = MinX; }
if (maxX > MaxX) { maxX = MaxX; }
if (minY < MinY) { minY = MinY; }
if (maxY > MaxY) { maxY = MaxY; }
for (var i = minX; i <= maxX; i++)
{ {
for (var j = minHash.Item2; j <= maxHash.Item2; j++) for (var j = minY; j <= maxY; j++)
{ {
var key = MakeLong(i, j); var key = MakeLong(i, j);
if (hashDictionary.ContainsKey(key)) if (hashDictionary.ContainsKey(key))
@ -73,7 +89,43 @@ namespace MoonTools.Core.Bonk
foreach (var t in hashDictionary[key]) foreach (var t in hashDictionary[key])
{ {
var (otherShape, otherTransform) = IDLookup[t]; var (otherShape, otherTransform) = IDLookup[t];
if (!id.Equals(t) && AABB.TestOverlap(shape.TransformedAABB(transform2D), otherShape.TransformedAABB(otherTransform))) if (!id.Equals(t) && AABB.TestOverlap(box, otherShape.TransformedAABB(otherTransform)))
{
yield return (t, otherShape, otherTransform);
}
}
}
}
}
}
/// <summary>
/// Retrieves objects based on a pre-transformed AABB.
/// </summary>
/// <param name="aabb">A transformed AABB.</param>
/// <returns></returns>
public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(AABB aabb)
{
var (minX, minY) = Hash(aabb.Min);
var (maxX, maxY) = Hash(aabb.Max);
if (minX < MinX) { minX = MinX; }
if (maxX > MaxX) { maxX = MaxX; }
if (minY < MinY) { minY = MinY; }
if (maxY > MaxY) { maxY = MaxY; }
for (var i = minX; i <= maxX; i++)
{
for (var j = minY; j <= maxY; j++)
{
var key = MakeLong(i, j);
if (hashDictionary.ContainsKey(key))
{
foreach (var t in hashDictionary[key])
{
var (otherShape, otherTransform) = IDLookup[t];
if (AABB.TestOverlap(aabb, otherShape.TransformedAABB(otherTransform)))
{ {
yield return (t, otherShape, otherTransform); yield return (t, otherShape, otherTransform);
} }

View File

@ -1,7 +1,6 @@
using System; using MoonTools.Structs;
using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
public interface IHasAABB2D public interface IHasAABB2D
{ {

View File

@ -1,8 +1,8 @@
using System; using System;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
public interface IShape2D : IHasAABB2D, IEquatable<IShape2D> public interface IShape2D : IHasAABB2D, IEquatable<IShape2D>
{ {

View File

@ -1,8 +1,8 @@
using System; using System;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
/// <summary> /// <summary>
/// A Minkowski difference between two shapes. /// A Minkowski difference between two shapes.

View File

@ -1,8 +1,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using MoonTools.Core.Structs; using System.Numerics;
using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
public struct MultiShape : IHasAABB2D public struct MultiShape : IHasAABB2D
{ {
@ -22,14 +23,30 @@ namespace MoonTools.Core.Bonk
return AABB.Transformed(AABB, transform); return AABB.Transformed(AABB, transform);
} }
public IEnumerable<(IShape2D, Transform2D)> TransformedShapeTransforms(Transform2D transform) /// <summary>
/// Moves the shapes by pivoting with an offset transform.
/// </summary>
/// <param name="offsetTransform"></param>
/// <returns></returns>
public IEnumerable<(IShape2D, Transform2D)> TransformShapesUsingOffset(Transform2D offsetTransform)
{ {
foreach (var (shape, shapeTransform) in ShapeTransformPairs) foreach (var (shape, shapeTransform) in ShapeTransformPairs)
{ {
yield return (shape, transform.Compose(shapeTransform)); var newTransform = new Transform2D(Vector2.Transform(shapeTransform.Position, offsetTransform.TransformMatrix), offsetTransform.Rotation, offsetTransform.Scale);
yield return (shape, newTransform);
} }
} }
public bool IsSingleShape<T>() where T : struct, IShape2D
{
return ShapeTransformPairs.Length == 1 && ShapeTransformPairs[0].Item1 is T;
}
public (T, Transform2D) ShapeTransformPair<T>() where T : struct, IShape2D
{
return ((T, Transform2D))ShapeTransformPairs[0];
}
private static AABB AABBFromShapes(IEnumerable<(IShape2D, Transform2D)> shapeTransforms) private static AABB AABBFromShapes(IEnumerable<(IShape2D, Transform2D)> shapeTransforms)
{ {
var minX = float.MaxValue; var minX = float.MaxValue;

View File

@ -1,6 +1,6 @@
using System.Numerics; using System.Numerics;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
internal struct Edge internal struct Edge
{ {

View File

@ -1,7 +1,7 @@
using MoonTools.Core.Structs; using MoonTools.Structs;
using System.Numerics; using System.Numerics;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
public static class NarrowPhase public static class NarrowPhase
{ {
@ -60,7 +60,7 @@ namespace MoonTools.Core.Bonk
} }
/// <summary> /// <summary>
/// Tests if a multishape-transform and shape-transform pair are overlapping. /// Tests if a multishape-transform and shape-transform pair are overlapping.
/// Note that this must perform pairwise comparison so the worst-case performance of this method will vary inversely with the amount of shapes in the multishape. /// Note that this must perform pairwise comparison so the worst-case performance of this method will vary inversely with the amount of shapes in the multishape.
/// </summary> /// </summary>
/// <param name="multiShape"></param> /// <param name="multiShape"></param>
@ -70,15 +70,15 @@ namespace MoonTools.Core.Bonk
/// <returns></returns> /// <returns></returns>
public static bool TestCollision(MultiShape multiShape, Transform2D multiShapeTransform, IShape2D shape, Transform2D shapeTransform) public static bool TestCollision(MultiShape multiShape, Transform2D multiShapeTransform, IShape2D shape, Transform2D shapeTransform)
{ {
foreach (var (otherShape, otherTransform) in multiShape.ShapeTransformPairs) foreach (var (otherShape, otherTransform) in multiShape.TransformShapesUsingOffset(multiShapeTransform))
{ {
if (TestCollision(shape, shapeTransform, otherShape, multiShapeTransform.Compose(otherTransform))) { return true; } if (TestCollision(shape, shapeTransform, otherShape, otherTransform)) { return true; }
} }
return false; return false;
} }
/// <summary> /// <summary>
/// Tests if a multishape-transform and shape-transform pair are overlapping. /// Tests if a multishape-transform and shape-transform pair are overlapping.
/// Note that this must perform pairwise comparison so the worst-case performance of this method will vary inversely with the amount of shapes in the multishape. /// Note that this must perform pairwise comparison so the worst-case performance of this method will vary inversely with the amount of shapes in the multishape.
/// </summary> /// </summary>
/// <param name="multiShape"></param> /// <param name="multiShape"></param>
@ -88,15 +88,15 @@ namespace MoonTools.Core.Bonk
/// <returns></returns> /// <returns></returns>
public static bool TestCollision(IShape2D shape, Transform2D shapeTransform, MultiShape multiShape, Transform2D multiShapeTransform) public static bool TestCollision(IShape2D shape, Transform2D shapeTransform, MultiShape multiShape, Transform2D multiShapeTransform)
{ {
foreach (var (otherShape, otherTransform) in multiShape.ShapeTransformPairs) foreach (var (otherShape, otherTransform) in multiShape.TransformShapesUsingOffset(multiShapeTransform))
{ {
if (TestCollision(shape, shapeTransform, otherShape, multiShapeTransform.Compose(otherTransform))) { return true; } if (TestCollision(shape, shapeTransform, otherShape, otherTransform)) { return true; }
} }
return false; return false;
} }
/// <summary> /// <summary>
/// Tests if two multishape-transform pairs are overlapping. /// Tests if two multishape-transform pairs are overlapping.
/// Note that this must perform pairwise comparison so the worst-case performance of this method will vary inversely with the amount of shapes in the multishapes. /// Note that this must perform pairwise comparison so the worst-case performance of this method will vary inversely with the amount of shapes in the multishapes.
/// </summary> /// </summary>
/// <param name="multiShapeA"></param> /// <param name="multiShapeA"></param>
@ -106,11 +106,11 @@ namespace MoonTools.Core.Bonk
/// <returns></returns> /// <returns></returns>
public static bool TestCollision(MultiShape multiShapeA, Transform2D transformA, MultiShape multiShapeB, Transform2D transformB) public static bool TestCollision(MultiShape multiShapeA, Transform2D transformA, MultiShape multiShapeB, Transform2D transformB)
{ {
foreach (var (shapeA, shapeTransformA) in multiShapeA.ShapeTransformPairs) foreach (var (shapeA, shapeTransformA) in multiShapeA.TransformShapesUsingOffset(transformA))
{ {
foreach (var (shapeB, shapeTransformB) in multiShapeB.ShapeTransformPairs) foreach (var (shapeB, shapeTransformB) in multiShapeB.TransformShapesUsingOffset(transformB))
{ {
if (TestCollision(shapeA, transformA.Compose(shapeTransformA), shapeB, transformB.Compose(shapeTransformB))) { return true; } if (TestCollision(shapeA, shapeTransformA, shapeB, shapeTransformB)) { return true; }
} }
} }
return false; return false;

View File

@ -1,8 +1,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
internal unsafe struct SimplexVertexBuffer internal unsafe struct SimplexVertexBuffer
{ {

View File

@ -1,8 +1,8 @@
using System; using System;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
/// <summary> /// <summary>
/// A Circle is a shape defined by a radius. /// A Circle is a shape defined by a radius.

View File

@ -1,9 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
/// <summary> /// <summary>
/// A line is a shape defined by exactly two points in space. /// A line is a shape defined by exactly two points in space.

View File

@ -1,9 +1,12 @@
using System; using System;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
/// <summary>
/// A Point is "that which has not part". All points by themselves are identical.
/// </summary>
public struct Point : IShape2D, IEquatable<Point> public struct Point : IShape2D, IEquatable<Point>
{ {
public AABB AABB { get; } public AABB AABB { get; }

View File

@ -2,9 +2,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
/// <summary> /// <summary>
/// A Shape defined by an arbitrary collection of vertices. /// A Shape defined by an arbitrary collection of vertices.
@ -59,7 +59,7 @@ namespace MoonTools.Core.Bonk
public bool Equals(IShape2D other) public bool Equals(IShape2D other)
{ {
return (other is Polygon otherPolygon && Equals(otherPolygon)); return other is Polygon otherPolygon && Equals(otherPolygon);
} }
public bool Equals(Polygon other) public bool Equals(Polygon other)

View File

@ -1,8 +1,8 @@
using System; using System;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
/// <summary> /// <summary>
/// A rectangle is a shape defined by a width and height. The origin is the center of the rectangle. /// A rectangle is a shape defined by a width and height. The origin is the center of the rectangle.
@ -23,15 +23,15 @@ namespace MoonTools.Core.Bonk
public Vector2 Min { get; } public Vector2 Min { get; }
public Vector2 Max { get; } public Vector2 Max { get; }
public Rectangle(int width, int height) public Rectangle(int left, int top, int width, int height)
{ {
Width = width; Width = width;
Height = height; Height = height;
AABB = new AABB(-width / 2f, -height / 2f, width / 2f, height / 2f); Left = left;
Right = AABB.Right; Right = left + width;
Left = AABB.Left; Top = top;
Top = AABB.Top; Bottom = top + height;
Bottom = AABB.Bottom; AABB = new AABB(left, top, Right, Bottom);
BottomLeft = new Vector2(Left, Bottom); BottomLeft = new Vector2(Left, Bottom);
TopRight = new Vector2(Top, Right); TopRight = new Vector2(Top, Right);
Min = AABB.Min; Min = AABB.Min;

View File

@ -1,9 +1,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
using System; using System;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
/// <summary> /// <summary>
/// A simplex is a shape with up to n - 2 vertices in the nth dimension. /// A simplex is a shape with up to n - 2 vertices in the nth dimension.

View File

@ -1,26 +1,21 @@
using System; using System;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
public struct SweepResult<T, U> where T : IEquatable<T> where U : struct, IShape2D public struct SweepResult<T> where T : IEquatable<T>
{ {
public static SweepResult<T, U> False = new SweepResult<T, U>(); public readonly static SweepResult<T> False = new SweepResult<T>();
public bool Hit { get; } public bool Hit { get; }
public Vector2 Motion { get; } public Vector2 Motion { get; }
public T ID { get; } public T ID { get; }
public U Shape { get; }
public Transform2D Transform { get; }
public SweepResult(bool hit, Vector2 motion, T id, U shape, Transform2D transform) public SweepResult(bool hit, Vector2 motion, T id)
{ {
Hit = hit; Hit = hit;
Motion = motion; Motion = motion;
ID = id; ID = id;
Shape = shape;
Transform = transform;
} }
} }
} }

View File

@ -1,13 +1,13 @@
using System; using System;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Bonk
{ {
public static class SweepTest public static class SweepTest
{ {
/// <summary> /// <summary>
/// Performs a sweep test on rectangles. /// Performs a sweep test on and against rectangles. Returns the position 1 pixel before overlap occurs.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="spatialHash">A spatial hash.</param> /// <param name="spatialHash">A spatial hash.</param>
@ -15,7 +15,7 @@ namespace MoonTools.Core.Bonk
/// <param name="transform">A transform by which to transform the IHasAABB2D.</param> /// <param name="transform">A transform by which to transform the IHasAABB2D.</param>
/// <param name="ray">Given in world-space.</param> /// <param name="ray">Given in world-space.</param>
/// <returns></returns> /// <returns></returns>
public static SweepResult<T, Rectangle> Rectangle<T>(SpatialHash<T> spatialHash, Rectangle rectangle, Transform2D transform, Vector2 ray) where T : IEquatable<T> public static SweepResult<T> Test<T>(SpatialHash<T> spatialHash, Rectangle rectangle, Transform2D transform, Vector2 ray) where T : IEquatable<T>
{ {
var transformedAABB = rectangle.TransformedAABB(transform); var transformedAABB = rectangle.TransformedAABB(transform);
var sweepBox = SweepBox(transformedAABB, ray); var sweepBox = SweepBox(transformedAABB, ray);
@ -27,74 +27,100 @@ namespace MoonTools.Core.Bonk
foreach (var (id, shape, shapeTransform) in spatialHash.Retrieve(sweepBox)) foreach (var (id, shape, shapeTransform) in spatialHash.Retrieve(sweepBox))
{ {
if (shape is Rectangle otherRectangle) Rectangle otherRectangle;
Transform2D otherTransform;
AABB otherTransformedAABB;
if (shape is Rectangle)
{ {
var otherTransformedAABB = otherRectangle.TransformedAABB(shapeTransform); otherRectangle = (Rectangle)shape;
float xInvEntry, yInvEntry; otherTransformedAABB = shape.TransformedAABB(shapeTransform);
otherTransform = shapeTransform;
}
else if (shape is MultiShape multiShape && multiShape.IsSingleShape<Rectangle>())
{
Transform2D rectangleOffset;
(otherRectangle, rectangleOffset) = multiShape.ShapeTransformPair<Rectangle>();
otherTransform = shapeTransform.Compose(rectangleOffset);
otherTransformedAABB = shape.TransformedAABB(otherTransform);
}
else
{
continue;
}
if (ray.X > 0) float xInvEntry, yInvEntry;
{
xInvEntry = shapeTransform.Position.X - (transform.Position.X + transformedAABB.Width);
}
else
{
xInvEntry = (shapeTransform.Position.X + otherTransformedAABB.Width) - transform.Position.X;
}
if (ray.Y > 0) if (ray.X > 0)
{ {
yInvEntry = shapeTransform.Position.Y - (transform.Position.Y + transformedAABB.Height); xInvEntry = otherTransformedAABB.Left - (transformedAABB.Right);
} }
else else
{ {
yInvEntry = (shapeTransform.Position.Y + otherTransformedAABB.Height) - shapeTransform.Position.Y; xInvEntry = (otherTransformedAABB.Right) - transformedAABB.Left;
} }
float xEntry, yEntry; if (ray.Y > 0)
{
yInvEntry = otherTransformedAABB.Top - (transformedAABB.Bottom);
}
else
{
yInvEntry = (otherTransformedAABB.Bottom) - transformedAABB.Top;
}
if (ray.X == 0) float xEntry, yEntry;
{
xEntry = float.MinValue;
}
else
{
xEntry = xInvEntry / ray.X;
}
if (ray.Y == 0) if (ray.X == 0)
{ {
yEntry = float.MinValue; xEntry = float.MinValue;
} }
else else
{ {
yEntry = yInvEntry / ray.Y; xEntry = xInvEntry / ray.X;
} }
var entryTime = Math.Max(xEntry, yEntry); if (ray.Y == 0)
{
yEntry = float.MinValue;
}
else
{
yEntry = yInvEntry / ray.Y;
}
if (entryTime > 0 && entryTime < 1) var entryTime = Math.Max(xEntry, yEntry);
if (entryTime >= 0 && entryTime <= 1)
{
if (entryTime < shortestDistance)
{ {
if (entryTime < shortestDistance) shortestDistance = entryTime;
{ nearestID = id;
shortestDistance = entryTime; nearestRectangle = otherRectangle;
nearestID = id; nearestTransform = shapeTransform;
nearestRectangle = rectangle;
nearestTransform = shapeTransform;
}
} }
} }
} }
if (nearestRectangle.HasValue) if (nearestRectangle.HasValue)
{ {
return new SweepResult<T, Rectangle>(true, ray * shortestDistance, nearestID, nearestRectangle.Value, nearestTransform.Value); var overlapPosition = ray * shortestDistance;
var correctionX = -Math.Sign(ray.X);
var correctionY = -Math.Sign(ray.Y);
return new SweepResult<T>(true, new Position2D((int)overlapPosition.X + correctionX, (int)overlapPosition.Y + correctionY), nearestID);
} }
else else
{ {
return SweepResult<T, Rectangle>.False; return SweepResult<T>.False;
} }
} }
public static SweepResult<T> Test<T>(SpatialHash<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 AABB SweepBox(AABB aabb, Vector2 ray)
{ {
return new AABB( return new AABB(

View File

@ -1,6 +1,6 @@
using System.Numerics; using System.Numerics;
namespace MoonTools.Core.Bonk.Extensions namespace MoonTools.Bonk.Extensions
{ {
internal static class Vector2Extensions internal static class Vector2Extensions
{ {
@ -15,4 +15,4 @@ namespace MoonTools.Core.Bonk.Extensions
return a.Cross(b) > 0 ? Vector2.Normalize(new Vector2(ab.Y, ab.X)) : Vector2.Normalize(new Vector2(ab.Y, -ab.X)); return a.Cross(b) > 0 ? Vector2.Normalize(new Vector2(ab.Y, ab.X)) : Vector2.Normalize(new Vector2(ab.Y, -ab.X));
} }
} }
} }

View File

@ -1,7 +1,7 @@
# MoonTools.Core.Bonk # MoonTools.Bonk
[![NuGet Badge](https://buildstats.info/nuget/MoonTools.Core.Bonk)](https://www.nuget.org/packages/MoonTools.Core.Bonk/) [![NuGet Badge](https://buildstats.info/nuget/MoonTools.Bonk)](https://www.nuget.org/packages/MoonTools.Bonk/)
[![CircleCI](https://circleci.com/gh/MoonsideGames/MoonTools.Core.Bonk.svg?style=svg)](https://circleci.com/gh/MoonsideGames/MoonTools.Core.Bonk) [![Build Status](https://gitea.drone.moonside.games/api/badges/MoonsideGames/MoonTools.Bonk/status.svg)](https://gitea.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. 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.

View File

@ -1,6 +1,6 @@
using NUnit.Framework; using NUnit.Framework;
using FluentAssertions; using FluentAssertions;
using MoonTools.Core.Bonk; using MoonTools.Bonk;
using System.Numerics; using System.Numerics;
namespace Tests namespace Tests

View File

@ -3,8 +3,8 @@ using FluentAssertions;
using System; using System;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Structs;
using MoonTools.Core.Bonk; using MoonTools.Bonk;
namespace Tests namespace Tests
{ {
@ -13,9 +13,9 @@ namespace Tests
[Test] [Test]
public void RectangleOverlap() public void RectangleOverlap()
{ {
var squareA = new Rectangle(2, 2); var squareA = new Rectangle(-1, -1, 2, 2);
var transformA = Transform2D.DefaultTransform; var transformA = Transform2D.DefaultTransform;
var squareB = new Rectangle(2, 2); var squareB = new Rectangle(-1, -1, 2, 2);
var transformB = new Transform2D(new Vector2(1.5f, 0)); var transformB = new Transform2D(new Vector2(1.5f, 0));
var (result, simplex) = NarrowPhase.FindCollisionSimplex(squareA, transformA, squareB, transformB); var (result, simplex) = NarrowPhase.FindCollisionSimplex(squareA, transformA, squareB, transformB);
@ -27,7 +27,7 @@ namespace Tests
intersection.X.Should().Be(1f); intersection.X.Should().Be(1f);
intersection.Y.Should().Be(0); intersection.Y.Should().Be(0);
var movedTransform = new Transform2D(transformA.Position - (intersection * 1.01f)); // move a tiny bit past var movedTransform = new Transform2D(transformA.Position - (intersection * 2)); // move past
NarrowPhase.TestCollision(squareA, movedTransform, squareB, transformB).Should().BeFalse(); NarrowPhase.TestCollision(squareA, movedTransform, squareB, transformB).Should().BeFalse();
} }
@ -52,7 +52,7 @@ namespace Tests
intersection.X.Should().BeApproximately(ix, 0.01f); intersection.X.Should().BeApproximately(ix, 0.01f);
intersection.Y.Should().BeApproximately(iy, 0.01f); intersection.Y.Should().BeApproximately(iy, 0.01f);
var movedTransform = new Transform2D(transformA.Position - (intersection * 1.01f)); // move a tiny bit past var movedTransform = new Transform2D(transformA.Position - (intersection * 2)); // move past
NarrowPhase.TestCollision(circleA, movedTransform, circleB, transformB).Should().BeFalse(); NarrowPhase.TestCollision(circleA, movedTransform, circleB, transformB).Should().BeFalse();
} }
@ -62,7 +62,7 @@ namespace Tests
{ {
var line = new Line(new Position2D(-4, -4), new Position2D(4, 4)); var line = new Line(new Position2D(-4, -4), new Position2D(4, 4));
var transformA = Transform2D.DefaultTransform; var transformA = Transform2D.DefaultTransform;
var square = new Rectangle(2, 2); var square = new Rectangle(-1, -1, 2, 2);
var transformB = Transform2D.DefaultTransform; var transformB = Transform2D.DefaultTransform;
var (result, simplex) = NarrowPhase.FindCollisionSimplex(line, transformA, square, transformB); var (result, simplex) = NarrowPhase.FindCollisionSimplex(line, transformA, square, transformB);
@ -71,7 +71,10 @@ namespace Tests
var intersection = NarrowPhase.Intersect(line, transformA, square, transformB, simplex); var intersection = NarrowPhase.Intersect(line, transformA, square, transformB, simplex);
var movedTransform = new Transform2D(transformA.Position - (intersection * 1.01f)); // move a tiny bit past intersection.X.Should().Be(1);
intersection.Y.Should().Be(-1);
var movedTransform = new Transform2D(transformA.Position - (intersection * 2)); // move past
NarrowPhase.TestCollision(line, movedTransform, square, transformB).Should().BeFalse(); NarrowPhase.TestCollision(line, movedTransform, square, transformB).Should().BeFalse();
} }

View File

@ -1,8 +1,8 @@
using NUnit.Framework; using NUnit.Framework;
using FluentAssertions; using FluentAssertions;
using MoonTools.Core.Bonk; using MoonTools.Bonk;
using MoonTools.Core.Structs; using MoonTools.Structs;
using System.Numerics; using System.Numerics;
using System.Collections.Immutable; using System.Collections.Immutable;
@ -140,8 +140,8 @@ namespace Tests
[Test] [Test]
public void RectangleEqual() public void RectangleEqual()
{ {
var a = new Rectangle(3, 3); var a = new Rectangle(-1, -1, 3, 3);
var b = new Rectangle(3, 3); var b = new Rectangle(-1, -1, 3, 3);
a.Equals(b).Should().BeTrue(); a.Equals(b).Should().BeTrue();
} }
@ -149,8 +149,8 @@ namespace Tests
[Test] [Test]
public void RectangleEqualOperator() public void RectangleEqualOperator()
{ {
var a = new Rectangle(3, 3); var a = new Rectangle(-1, -1, 3, 3);
var b = new Rectangle(3, 3); var b = new Rectangle(-1, -1, 3, 3);
(a == b).Should().BeTrue(); (a == b).Should().BeTrue();
} }
@ -158,8 +158,8 @@ namespace Tests
[Test] [Test]
public void RectangleNotEqual() public void RectangleNotEqual()
{ {
var a = new Rectangle(3, 3); var a = new Rectangle(-1, -1, 3, 3);
var b = new Rectangle(6, 6); var b = new Rectangle(-3, -3, 6, 6);
a.Equals(b).Should().BeFalse(); a.Equals(b).Should().BeFalse();
} }
@ -167,8 +167,8 @@ namespace Tests
[Test] [Test]
public void RectangleNotEqualOperator() public void RectangleNotEqualOperator()
{ {
var a = new Rectangle(3, 3); var a = new Rectangle(-1, -1, 3, 3);
var b = new Rectangle(6, 6); var b = new Rectangle(-3, -3, 6, 6);
(a != b).Should().BeTrue(); (a != b).Should().BeTrue();
} }

View File

@ -1,6 +1,6 @@
using NUnit.Framework; using NUnit.Framework;
using MoonTools.Core.Bonk; using MoonTools.Bonk;
using MoonTools.Core.Structs; using MoonTools.Structs;
using System.Numerics; using System.Numerics;
using FluentAssertions; using FluentAssertions;
using System.Collections.Immutable; using System.Collections.Immutable;
@ -55,7 +55,7 @@ namespace Tests
public void PointRectangleOverlapping() public void PointRectangleOverlapping()
{ {
var point = new Point(); var point = new Point();
var rectangle = new Rectangle(4, 4); var rectangle = new Rectangle(-2, -2, 4, 4);
NarrowPhase.TestCollision(point, Transform2D.DefaultTransform, rectangle, Transform2D.DefaultTransform).Should().BeTrue(); NarrowPhase.TestCollision(point, Transform2D.DefaultTransform, rectangle, Transform2D.DefaultTransform).Should().BeTrue();
} }
@ -65,7 +65,7 @@ namespace Tests
{ {
var point = new Point(); var point = new Point();
var pointTransform = new Transform2D(new Position2D(5, 5)); var pointTransform = new Transform2D(new Position2D(5, 5));
var rectangle = new Rectangle(4, 4); var rectangle = new Rectangle(-2, -2, 4, 4);
NarrowPhase.TestCollision(point, pointTransform, rectangle, Transform2D.DefaultTransform).Should().BeFalse(); NarrowPhase.TestCollision(point, pointTransform, rectangle, Transform2D.DefaultTransform).Should().BeFalse();
} }
@ -354,10 +354,10 @@ namespace Tests
[Test] [Test]
public void RectanglesNotOverlapping() public void RectanglesNotOverlapping()
{ {
var rectangleA = new Rectangle(12, 12); var rectangleA = new Rectangle(-6, -6, 12, 12);
var transformA = new Transform2D(new Position2D(39, 249)); var transformA = new Transform2D(new Position2D(39, 249));
var rectangleB = new Rectangle(16, 16); var rectangleB = new Rectangle(-8, -8, 16, 16);
var transformB = new Transform2D(new Position2D(16, 240)); var transformB = new Transform2D(new Position2D(16, 240));
NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeFalse(); NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeFalse();
@ -366,10 +366,10 @@ namespace Tests
[Test] [Test]
public void RotatedRectanglesOverlapping() public void RotatedRectanglesOverlapping()
{ {
var rectangleA = new Rectangle(3, 6); var rectangleA = new Rectangle(-1, -3, 3, 6);
var transformA = new Transform2D(new Vector2(4f, 0), (float)System.Math.PI / 2); var transformA = new Transform2D(new Vector2(4f, 0), (float)System.Math.PI / 2);
var rectangleB = new Rectangle(2, 2); var rectangleB = new Rectangle(-1, -1, 2, 2);
var transformB = new Transform2D(new Vector2(0, 0)); var transformB = new Transform2D(new Vector2(0, 0));
NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue();
@ -378,10 +378,10 @@ namespace Tests
[Test] [Test]
public void RectanglesTouchingGJK2D() public void RectanglesTouchingGJK2D()
{ {
var rectangleA = new Rectangle(2, 2); var rectangleA = new Rectangle(-1, -1, 2, 2);
var transformA = new Transform2D(new Position2D(-1, 0)); var transformA = new Transform2D(new Position2D(-1, 0));
var rectangleB = new Rectangle(2, 2); var rectangleB = new Rectangle(-1, -1, 2, 2);
var transformB = new Transform2D(new Vector2(1, 0)); var transformB = new Transform2D(new Vector2(1, 0));
NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue();
@ -390,10 +390,10 @@ namespace Tests
[Test] [Test]
public void RectanglesOverlappingGJK2D() public void RectanglesOverlappingGJK2D()
{ {
var rectangleA = new Rectangle(2, 2); var rectangleA = new Rectangle(-1, -1, 2, 2);
var transformA = new Transform2D(new Position2D(0, 0)); var transformA = new Transform2D(new Position2D(0, 0));
var rectangleB = new Rectangle(2, 2); var rectangleB = new Rectangle(-1, -1, 2, 2);
var transformB = new Transform2D(new Vector2(1, 0)); var transformB = new Transform2D(new Vector2(1, 0));
NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue();
@ -402,10 +402,10 @@ namespace Tests
[Test] [Test]
public void RectanglesTouchingOverlap() public void RectanglesTouchingOverlap()
{ {
var rectangleA = new Rectangle(2, 2); var rectangleA = new Rectangle(-1, -1, 2, 2);
var transformA = new Transform2D(new Position2D(-1, 0)); var transformA = new Transform2D(new Position2D(-1, 0));
var rectangleB = new Rectangle(2, 2); var rectangleB = new Rectangle(-1, -1, 2, 2);
var transformB = new Transform2D(new Vector2(1, 0)); var transformB = new Transform2D(new Vector2(1, 0));
NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue();
@ -414,11 +414,11 @@ namespace Tests
[Test] [Test]
public void RectanglesOverlappingOverlap() public void RectanglesOverlappingOverlap()
{ {
var rectangleA = new Rectangle(2, 2); var rectangleA = new Rectangle(-1, -1, 2, 2);
var transformA = new Transform2D(new Position2D(0, 0)); var transformA = new Transform2D(new Position2D(0, 0));
var rectangleB = new Rectangle(2, 2); var rectangleB = new Rectangle(-1, -1, 2, 2);
var transformB = new Transform2D(new Vector2(1, 0)); var transformB = new Transform2D(new Vector2(1, 0), 0, new Vector2(-1, 1));
NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue();
} }
@ -428,18 +428,18 @@ namespace Tests
{ {
var multiRectangleA = new MultiShape( var multiRectangleA = new MultiShape(
ImmutableArray.Create<(IShape2D, Transform2D)>( ImmutableArray.Create<(IShape2D, Transform2D)>(
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 0))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 2))) (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 2)))
) )
); );
var transformA = new Transform2D(new Position2D(5, 0)); var transformA = new Transform2D(new Position2D(5, 0));
var multiRectangleB = new MultiShape( var multiRectangleB = new MultiShape(
ImmutableArray.Create<(IShape2D, Transform2D)>( ImmutableArray.Create<(IShape2D, Transform2D)>(
(new Rectangle(4, 1), new Transform2D(new Position2D(4, -1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, -1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 0))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 1))) (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 1)))
) )
); );
var transformB = new Transform2D(new Position2D(0, 3)); var transformB = new Transform2D(new Position2D(0, 3));
@ -452,18 +452,18 @@ namespace Tests
{ {
var multiRectangleA = new MultiShape( var multiRectangleA = new MultiShape(
ImmutableArray.Create<(IShape2D, Transform2D)>( ImmutableArray.Create<(IShape2D, Transform2D)>(
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 0))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 2))) (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 2)))
) )
); );
var transformA = new Transform2D(new Position2D(5, 0)); var transformA = new Transform2D(new Position2D(5, 0));
var multiRectangleB = new MultiShape( var multiRectangleB = new MultiShape(
ImmutableArray.Create<(IShape2D, Transform2D)>( ImmutableArray.Create<(IShape2D, Transform2D)>(
(new Rectangle(4, 1), new Transform2D(new Position2D(4, -1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, -1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 0))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 1))) (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 1)))
) )
); );
var transformB = new Transform2D(new Position2D(0, -3)); var transformB = new Transform2D(new Position2D(0, -3));

View File

@ -1,7 +1,7 @@
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using MoonTools.Core.Structs; using MoonTools.Structs;
using MoonTools.Core.Bonk; using MoonTools.Bonk;
using System.Numerics; using System.Numerics;
using System.Collections.Immutable; using System.Collections.Immutable;
@ -14,16 +14,16 @@ namespace Tests
{ {
var spatialHash = new SpatialHash<int>(16); var spatialHash = new SpatialHash<int>(16);
var rectA = new Rectangle(4, 4); var rectA = new Rectangle(-2, -2, 4, 4);
var rectATransform = new Transform2D(new Vector2(-8, -8)); var rectATransform = new Transform2D(new Vector2(-8, -8));
var rectB = new Rectangle(4, 4); var rectB = new Rectangle(-2, -2, 4, 4);
var rectBTransform = new Transform2D(new Vector2(8, 8)); var rectBTransform = new Transform2D(new Vector2(8, 8));
var rectC = new Rectangle(4, 4); var rectC = new Rectangle(-2, -2, 4, 4);
var rectCTransform = new Transform2D(new Vector2(24, -4)); var rectCTransform = new Transform2D(new Vector2(24, -4));
var rectD = new Rectangle(4, 4); var rectD = new Rectangle(-2, -2, 4, 4);
var rectDTransform = new Transform2D(new Vector2(24, 24)); var rectDTransform = new Transform2D(new Vector2(24, 24));
var circleA = new Circle(2); var circleA = new Circle(2);
@ -40,9 +40,9 @@ namespace Tests
var multiRectangle = new MultiShape( var multiRectangle = new MultiShape(
ImmutableArray.Create<(IShape2D, Transform2D)>( ImmutableArray.Create<(IShape2D, Transform2D)>(
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, -2))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-2, -2))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, -1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-2, -1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, 0))) (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-2, 0)))
) )
); );
var multiRectangleTransform = new Transform2D(new Position2D(8, 8)); var multiRectangleTransform = new Transform2D(new Position2D(8, 8));
@ -78,13 +78,13 @@ namespace Tests
{ {
var spatialHash = new SpatialHash<int>(16); var spatialHash = new SpatialHash<int>(16);
var rectA = new Rectangle(4, 4); var rectA = new Rectangle(-2, -2, 4, 4);
var rectATransform = new Transform2D(new Vector2(-8, -8)); var rectATransform = new Transform2D(new Vector2(-8, -8));
var rectB = new Rectangle(4, 4); var rectB = new Rectangle(-2, -2, 4, 4);
var rectBTransform = new Transform2D(new Vector2(-8, -8)); var rectBTransform = new Transform2D(new Vector2(-8, -8));
var rectC = new Rectangle(2, 2); var rectC = new Rectangle(-2, -2, 2, 2);
var rectCTransform = new Transform2D(new Vector2(-8, -8)); var rectCTransform = new Transform2D(new Vector2(-8, -8));
spatialHash.Insert(0, rectA, rectATransform); spatialHash.Insert(0, rectA, rectATransform);
@ -99,10 +99,10 @@ namespace Tests
{ {
var spatialHash = new SpatialHash<int>(16); var spatialHash = new SpatialHash<int>(16);
var rectA = new Rectangle(4, 4); var rectA = new Rectangle(-2, -2, 4, 4);
var rectATransform = new Transform2D(new Vector2(-8, -8)); var rectATransform = new Transform2D(new Vector2(-8, -8));
var rectB = new Rectangle(4, 4); var rectB = new Rectangle(-2, -2, 4, 4);
var rectBTransform = new Transform2D(new Vector2(8, 8)); var rectBTransform = new Transform2D(new Vector2(8, 8));
spatialHash.Insert(0, rectA, rectATransform); spatialHash.Insert(0, rectA, rectATransform);

View File

@ -1,7 +1,7 @@
using System.Numerics; using System.Numerics;
using FluentAssertions; using FluentAssertions;
using MoonTools.Core.Bonk; using MoonTools.Bonk;
using MoonTools.Core.Structs; using MoonTools.Structs;
using NUnit.Framework; using NUnit.Framework;
namespace Tests namespace Tests
@ -11,24 +11,32 @@ namespace Tests
[Test] [Test]
public void SweepsThrough() public void SweepsThrough()
{ {
var rectangle = new Rectangle(4, 4); var rectangle = new Rectangle(-2, -2, 4, 4);
var transform = new Transform2D(new Position2D(-6, 0)); var transform = new Transform2D(new Position2D(-6, 0));
var otherRectangle = new Rectangle(4, 4); var otherRectangle = new Rectangle(-2, -2, 4, 4);
var otherTransform = new Transform2D(new Position2D(6, 0)); var otherTransform = new Transform2D(new Position2D(6, 0));
var farthestRectangle = new Rectangle(4, 4); var farthestRectangle = new Rectangle(-2, -2, 4, 4);
var farthestTransform = new Transform2D(new Position2D(12, 0)); var farthestTransform = new Transform2D(new Position2D(12, 0));
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 SpatialHash<int>(16);
spatialHash.Insert(1, otherRectangle, otherTransform); spatialHash.Insert(1, otherRectangle, otherTransform);
spatialHash.Insert(2, farthestRectangle, farthestTransform); spatialHash.Insert(2, farthestRectangle, farthestTransform);
spatialHash.Insert(3, downRectangle, downTransform);
SweepTest.Rectangle(spatialHash, rectangle, transform, new Vector2(12, 0)).Should().Be( SweepTest.Test(spatialHash, rectangle, transform, new Vector2(12, 0)).Should().Be(
new SweepResult<int, Rectangle>(true, new Vector2(8, 0), 1, otherRectangle, otherTransform) new SweepResult<int>(true, new Vector2(7, 0), 1)
); );
SweepTest.Rectangle(spatialHash, rectangle, transform, new Vector2(-12, 0)).Hit.Should().BeFalse(); SweepTest.Test(spatialHash, rectangle, transform, new Vector2(-12, 0)).Hit.Should().BeFalse();
SweepTest.Test(spatialHash, rectangle, transform, new Vector2(0, 20)).Should().Be(
new SweepResult<int>(true, new Vector2(0, 15), 3)
);
} }
} }
} }

View File

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