gc-related optimizations

generics
Evan Hemsley 2019-10-25 03:46:47 -07:00
parent c340e74450
commit 420e617cec
10 changed files with 145 additions and 159 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
bin/
obj/
.vscode

View File

@ -16,5 +16,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MoonTools.Core.Structs" Version="1.0.1"/>
<PackageReference Include="morelinq" Version="3.2.0"/>
</ItemGroup>
</Project>

View File

@ -5,6 +5,7 @@
*/
using Microsoft.Xna.Framework;
using MoonTools.Core.Bonk.Extensions;
using MoonTools.Core.Structs;
using System;
using System.Collections.Generic;
@ -20,14 +21,15 @@ namespace MoonTools.Core.Bonk
public static class EPA2D
{
// vector returned gives direction from A to B
public static Vector2 Intersect(IShape2D shapeA, Transform2D Transform2DA, IShape2D shapeB, Transform2D Transform2DB, IEnumerable<Vector2> givenSimplexVertices)
public static Vector2 Intersect(IShape2D shapeA, Transform2D Transform2DA, IShape2D shapeB, Transform2D Transform2DB, (Func<Vector2, Vector2>, Vector2, Vector2) givenSimplexVertices)
{
var simplexVertices = new SimplexVertices(new Vector2?[36]);
foreach (var vertex in givenSimplexVertices)
{
simplexVertices.Add(vertex);
}
var (simplexSupport, a, b) = givenSimplexVertices;
simplexVertices.Add(simplexSupport(a));
simplexVertices.Add(simplexSupport(b));
simplexVertices.Add(simplexSupport(-a));
simplexVertices.Add(simplexSupport(-b));
var e0 = (simplexVertices[1].X - simplexVertices[0].X) * (simplexVertices[1].Y + simplexVertices[0].Y);
var e1 = (simplexVertices[2].X - simplexVertices[1].X) * (simplexVertices[2].Y + simplexVertices[1].Y);

View File

@ -1,126 +1,87 @@
/*
* Implementation of the GJK collision algorithm
* Based on some math blogs
* https://blog.hamaluik.ca/posts/building-a-collision-engine-part-1-2d-gjk-collision-detection/
* and some code from https://github.com/kroitor/gjk.c
*/
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework;
using MoonTools.Core.Structs;
using System;
using MoonTools.Core.Bonk.Extensions;
namespace MoonTools.Core.Bonk
{
public static class GJK2D
{
private enum SolutionStatus
public static bool TestCollision(IShape2D shapeA, Transform2D transformA, IShape2D shapeB, Transform2D transformB)
{
NoIntersection,
Intersection,
StillSolving
return OriginInside(MinkowskiDifference(shapeA, transformA, shapeB, transformB));
}
public static ValueTuple<bool, SimplexVertices> TestCollision(IShape2D shapeA, Transform2D Transform2DA, IShape2D shapeB, Transform2D Transform2DB)
public static (bool, (Func<Vector2, Vector2>, Vector2, Vector2)) CollisionAndSimplex(IShape2D shapeA, Transform2D transformA, IShape2D shapeB, Transform2D transformB)
{
var vertices = new SimplexVertices(new Vector2?[] { null, null, null, null });
const SolutionStatus solutionStatus = SolutionStatus.StillSolving;
var direction = Transform2DB.Position - Transform2DA.Position;
var result = (solutionStatus, direction);
while (result.solutionStatus == SolutionStatus.StillSolving)
{
result = EvolveSimplex(shapeA, Transform2DA, shapeB, Transform2DB, vertices, result.direction);
var support = MinkowskiDifference(shapeA, transformA, shapeB, transformB);
var result = OriginInsideWithSimplex(support);
return (result.Item1, (support, result.Item2, result.Item3));
}
return ValueTuple.Create(result.solutionStatus == SolutionStatus.Intersection, vertices);
private static Func<Vector2, Vector2> MinkowskiDifference(IShape2D shapeA, Transform2D transformA, IShape2D shapeB, Transform2D transformB)
{
return direction => shapeA.Support(direction, transformA) - shapeB.Support(-direction, transformB);
}
private static (SolutionStatus, Vector2) EvolveSimplex(IShape2D shapeA, Transform2D Transform2DA, IShape2D shapeB, Transform2D Transform2DB, SimplexVertices vertices, Vector2 direction)
private static bool OriginInside(Func<Vector2, Vector2> support)
{
switch(vertices.Count)
{
case 0:
if (direction == Vector2.Zero)
{
direction = Vector2.UnitX;
var a = support(Vector2.UnitX);
var b = support(-a);
return Vector2.Dot(a, b) > 0 ? false : CheckSimplex(support, a, b);
}
break;
case 1:
direction *= -1;
break;
case 2:
var ab = vertices[1] - vertices[0];
var a0 = vertices[0] * -1;
direction = TripleProduct(ab, a0, ab);
if (direction == Vector2.Zero)
private static (bool, Vector2, Vector2) OriginInsideWithSimplex(Func<Vector2, Vector2> support)
{
direction = Perpendicular(ab);
var a = support(Vector2.UnitX);
var b = support(-a);
return Vector2.Dot(a, b) > 0 ? (false, a, b) : Simplex(support, a, b);
}
break;
case 3:
var c0 = vertices[2] * -1;
var bc = vertices[1] - vertices[2];
var ca = vertices[0] - vertices[2];
var bcNorm = TripleProduct(ca, bc, bc);
var caNorm = TripleProduct(bc, ca, ca);
// the origin is outside line bc
// get rid of a and add a new support in the direction of bcNorm
if (Vector2.Dot(bcNorm, c0) > 0)
private static bool CheckSimplex(Func<Vector2, Vector2> support, Vector2 a, Vector2 b)
{
vertices.RemoveAt(0);
direction = bcNorm;
var axb = a.Cross(b);
var c = support((b - a).Perpendicular());
var axc = a.Cross(c);
var bxc = b.Cross(c);
var cxb = -bxc;
return (b - a) == Vector2.Zero || (axb.Y > 0 != bxc.Y > 0 ? CheckSimplex(support, b, c) : (axc.Y > 0 != cxb.Y > 0 ? CheckSimplex(support, a, c) : true));
}
// the origin is outside line ca
// get rid of b and add a new support in the direction of caNorm
else if (Vector2.Dot(caNorm, c0) > 0)
private static (bool, Vector2, Vector2) Simplex(Func<Vector2, Vector2> support, Vector2 a, Vector2 b)
{
vertices.RemoveAt(1);
direction = caNorm;
if ((b - a) == Vector2.Zero)
{
return (false, a, b);
}
// the origin is inside both ab and ac,
// so it must be inside the triangle!
else
{
return (SolutionStatus.Intersection, direction);
}
break;
}
var c = support((b - a).Perpendicular());
var axb = a.Cross(b);
var bxc = b.Cross(c);
return (AddSupport(shapeA, Transform2DA, shapeB, Transform2DB, vertices, direction) ?
SolutionStatus.StillSolving :
SolutionStatus.NoIntersection, direction);
}
private static bool AddSupport(IShape2D shapeA, Transform2D Transform2DA, IShape2D shapeB, Transform2D Transform2DB, SimplexVertices vertices, Vector2 direction)
if (axb.Y > 0 != bxc.Y > 0)
{
var newVertex = shapeA.Support(direction, Transform2DA) - shapeB.Support(-direction, Transform2DB);
vertices.Add(newVertex);
return Vector2.Dot(direction, newVertex) >= 0;
return Simplex(support, b, c);
}
private static Vector2 TripleProduct(Vector2 a, Vector2 b, Vector2 c)
else
{
var A = new Vector3(a.X, a.Y, 0);
var B = new Vector3(b.X, b.Y, 0);
var C = new Vector3(c.X, c.Y, 0);
var axc = a.Cross(c);
var cxb = -bxc;
var first = Vector3.Cross(A, B);
var second = Vector3.Cross(first, C);
return new Vector2(second.X, second.Y);
}
private static Vector2 Perpendicular(Vector2 v)
if (axc.Y > 0 != cxb.Y > 0)
{
return new Vector2(v.Y, -v.X);
return Simplex(support, a, b);
}
else
{
return (true, a, b);
}
}
}
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using MoonTools.Core.Structs;
@ -6,17 +7,28 @@ namespace MoonTools.Core.Bonk
{
public struct Line : IShape2D, IEquatable<IShape2D>
{
private Position2D[] vertices;
private Position2D v0;
private Position2D v1;
private IEnumerable<Position2D> vertices
{
get
{
yield return v0;
yield return v0;
}
}
public Line(Position2D start, Position2D end)
{
vertices = new Position2D[2] { start, end };
v0 = start;
v1 = end;
}
public Vector2 Support(Vector2 direction, Transform2D transform)
{
var TransformedStart = Vector2.Transform(vertices[0], transform.TransformMatrix);
var TransformedEnd = Vector2.Transform(vertices[1], transform.TransformMatrix);
var TransformedStart = Vector2.Transform(v0, transform.TransformMatrix);
var TransformedEnd = Vector2.Transform(v1, transform.TransformMatrix);
return Vector2.Dot(TransformedStart, direction) > Vector2.Dot(TransformedEnd, direction) ?
TransformedStart :
TransformedEnd;
@ -32,7 +44,7 @@ namespace MoonTools.Core.Bonk
if (other is Line)
{
var otherLine = (Line)other;
return vertices[0].ToVector2() == otherLine.vertices[0].ToVector2() && vertices[1].ToVector2() == otherLine.vertices[1].ToVector2();
return v0.ToVector2() == otherLine.v0.ToVector2() && v1.ToVector2() == otherLine.v1.ToVector2();
}
return false;

View File

@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using MoonTools.Core.Structs;
using MoreLinq;
namespace MoonTools.Core.Bonk
{
@ -11,7 +14,16 @@ namespace MoonTools.Core.Bonk
public int MaxX { get; }
public int MaxY { get; }
private Position2D[] vertices;
private IEnumerable<Position2D> vertices
{
get
{
yield return new Position2D(MinX, MinY);
yield return new Position2D(MinX, MaxY);
yield return new Position2D(MaxX, MinY);
yield return new Position2D(MaxX, MaxY);
}
}
public Rectangle(int minX, int minY, int maxX, int maxY)
{
@ -19,33 +31,11 @@ namespace MoonTools.Core.Bonk
MinY = minY;
MaxX = maxX;
MaxY = maxY;
vertices = new Position2D[4]
{
new Position2D(minX, minY),
new Position2D(minX, maxY),
new Position2D(maxX, minY),
new Position2D(maxX, maxY)
};
}
public Vector2 Support(Vector2 direction, Transform2D transform)
{
var furthestDistance = float.NegativeInfinity;
var furthestVertex = Vector2.Transform(vertices[0], transform.TransformMatrix);
foreach (var v in vertices)
{
var TransformedVertex = Vector2.Transform(v, transform.TransformMatrix);
var distance = Vector2.Dot(TransformedVertex, direction);
if (distance > furthestDistance)
{
furthestDistance = distance;
furthestVertex = TransformedVertex;
}
}
return furthestVertex;
return vertices.Select(vertex => Vector2.Transform(vertex, transform.TransformMatrix)).MaxBy(transformed => Vector2.Dot(transformed, direction)).First();
}
public AABB AABB(Transform2D Transform2D)

18
Bonk/Vector2Extensions.cs Normal file
View File

@ -0,0 +1,18 @@
using Microsoft.Xna.Framework;
namespace MoonTools.Core.Bonk.Extensions
{
public static class Vector2Extensions
{
public static Vector2 Cross(this Vector2 a, Vector2 b)
{
var vec3 = Vector3.Cross(new Vector3(a.X, a.Y, 0), new Vector3(b.X, b.Y, 0));
return new Vector2(vec3.X, vec3.Y);
}
public static Vector2 Perpendicular(this Vector2 v)
{
return new Vector2(v.Y, -v.X);
}
}
}

View File

@ -18,11 +18,11 @@ namespace Tests
var squareB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1);
var transformB = new Transform2D(new Vector2(1.5f, 0));
var test = GJK2D.TestCollision(squareA, transformA, squareB, transformB);
var (result, simplex) = GJK2D.CollisionAndSimplex(squareA, transformA, squareB, transformB);
Assert.That(test.Item1, Is.True);
result.Should().BeTrue();
var intersection = EPA2D.Intersect(squareA, transformA, squareB, transformB, test.Item2);
var intersection = EPA2D.Intersect(squareA, transformA, squareB, transformB, simplex);
intersection.X.Should().Be(1f);
intersection.Y.Should().Be(0);
@ -36,11 +36,11 @@ namespace Tests
var circleB = new Circle(1);
var transformB = new Transform2D(new Vector2(1, 1));
var test = GJK2D.TestCollision(circleA, transformA, circleB, transformB);
var (result, simplex) = GJK2D.CollisionAndSimplex(circleA, transformA, circleB, transformB);
Assert.That(test.Item1, Is.True);
result.Should().BeTrue();
var intersection = EPA2D.Intersect(circleA, transformA, circleB, transformB, test.Item2);
var intersection = EPA2D.Intersect(circleA, transformA, circleB, transformB, simplex);
var ix = circleA.Radius * (float)Math.Cos(Math.PI / 4) - (circleB.Radius * (float)Math.Cos(5 * Math.PI / 4) + transformB.Position.X);
var iy = circleA.Radius * (float)Math.Sin(Math.PI / 4) - (circleB.Radius * (float)Math.Sin(5 * Math.PI / 4) + transformB.Position.Y);
@ -57,11 +57,11 @@ namespace Tests
var square = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1);
var transformB = Transform2D.DefaultTransform;
var test = GJK2D.TestCollision(line, transformA, square, transformB);
var (result, simplex) = GJK2D.CollisionAndSimplex(line, transformA, square, transformB);
Assert.That(test.Item1, Is.True);
result.Should().BeTrue();
var intersection = EPA2D.Intersect(line, transformA, square, transformB, test.Item2);
var intersection = EPA2D.Intersect(line, transformA, square, transformB, simplex);
intersection.X.Should().Be(-1);
intersection.Y.Should().Be(1);

View File

@ -2,6 +2,7 @@
using MoonTools.Core.Bonk;
using MoonTools.Core.Structs;
using Microsoft.Xna.Framework;
using FluentAssertions;
namespace Tests
{
@ -13,7 +14,7 @@ namespace Tests
var lineA = new Line(new Position2D(-1, -1), new Position2D(1, 1));
var lineB = new Line(new Position2D(-1, 1), new Position2D(1, -1));
Assert.IsTrue(GJK2D.TestCollision(lineA, Transform2D.DefaultTransform, lineB, Transform2D.DefaultTransform).Item1);
GJK2D.TestCollision(lineA, Transform2D.DefaultTransform, lineB, Transform2D.DefaultTransform).Should().BeTrue();
}
[Test]
@ -22,7 +23,7 @@ namespace Tests
var lineA = new Line(new Position2D(0, 1), new Position2D(1, 0));
var lineB = new Line(new Position2D(-1, -1), new Position2D(-2, -2));
Assert.IsFalse(GJK2D.TestCollision(lineA, Transform2D.DefaultTransform, lineB, Transform2D.DefaultTransform).Item1);
GJK2D.TestCollision(lineA, Transform2D.DefaultTransform, lineB, Transform2D.DefaultTransform).Should().BeFalse();
}
[Test]
@ -33,7 +34,7 @@ namespace Tests
var circleB = new Circle(2);
var transformB = new Transform2D(new Vector2(1, 1));
Assert.IsTrue(GJK2D.TestCollision(circleA, transformA, circleB, transformB).Item1);
Assert.IsTrue(GJK2D.TestCollision(circleA, transformA, circleB, transformB));
}
[Test]
@ -44,7 +45,7 @@ namespace Tests
var circleB = new Circle(2);
var transformB = new Transform2D(new Vector2(5, 5));
Assert.IsFalse(GJK2D.TestCollision(circleA, transformA, circleB, transformB).Item1);
Assert.IsFalse(GJK2D.TestCollision(circleA, transformA, circleB, transformB));
}
[Test]
@ -64,7 +65,7 @@ namespace Tests
var transformB = new Transform2D(new Vector2(0.5f, 0.5f));
Assert.IsTrue(GJK2D.TestCollision(shapeA, transformA, shapeB, transformB).Item1);
Assert.IsTrue(GJK2D.TestCollision(shapeA, transformA, shapeB, transformB));
}
[Test]
@ -84,7 +85,7 @@ namespace Tests
var transformB = new Transform2D(new Vector2(5, 0));
Assert.IsFalse(GJK2D.TestCollision(shapeA, transformA, shapeB, transformB).Item1);
Assert.IsFalse(GJK2D.TestCollision(shapeA, transformA, shapeB, transformB));
}
[Test]
@ -101,7 +102,7 @@ namespace Tests
var transformB = Transform2D.DefaultTransform;
Assert.IsTrue(GJK2D.TestCollision(line, transformA, polygon, transformB).Item1);
Assert.IsTrue(GJK2D.TestCollision(line, transformA, polygon, transformB));
}
[Test]
@ -118,7 +119,7 @@ namespace Tests
var transformB = Transform2D.DefaultTransform;
Assert.IsFalse(GJK2D.TestCollision(line, transformA, polygon, transformB).Item1);
Assert.IsFalse(GJK2D.TestCollision(line, transformA, polygon, transformB));
}
[Test]
@ -129,7 +130,7 @@ namespace Tests
var circle = new Circle(1);
var transformB = Transform2D.DefaultTransform;
Assert.IsTrue(GJK2D.TestCollision(line, transformA, circle, transformB).Item1);
Assert.IsTrue(GJK2D.TestCollision(line, transformA, circle, transformB));
}
[Test]
@ -140,7 +141,7 @@ namespace Tests
var circle = new Circle(1);
var transformB = Transform2D.DefaultTransform;
Assert.IsFalse(GJK2D.TestCollision(line, transformA, circle, transformB).Item1);
Assert.IsFalse(GJK2D.TestCollision(line, transformA, circle, transformB));
}
[Test]
@ -156,7 +157,7 @@ namespace Tests
var transformB = Transform2D.DefaultTransform;
Assert.IsTrue(GJK2D.TestCollision(circle, transformA, square, transformB).Item1);
Assert.IsTrue(GJK2D.TestCollision(circle, transformA, square, transformB));
}
[Test]
@ -171,7 +172,7 @@ namespace Tests
);
var squareTransform = Transform2D.DefaultTransform;
Assert.IsFalse(GJK2D.TestCollision(circle, circleTransform, square, squareTransform).Item1);
Assert.IsFalse(GJK2D.TestCollision(circle, circleTransform, square, squareTransform));
}
[Test]
@ -183,7 +184,7 @@ namespace Tests
var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1);
var transformB = new Transform2D(new Vector2(1, 0));
Assert.IsTrue(GJK2D.TestCollision(rectangleA, transformA, rectangleB, transformB).Item1);
Assert.IsTrue(GJK2D.TestCollision(rectangleA, transformA, rectangleB, transformB));
}
[Test]
@ -195,7 +196,7 @@ namespace Tests
var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1);
var transformB = new Transform2D(new Vector2(1, 0));
Assert.IsTrue(GJK2D.TestCollision(rectangleA, transformA, rectangleB, transformB).Item1);
Assert.IsTrue(GJK2D.TestCollision(rectangleA, transformA, rectangleB, transformB));
}
}
}

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>