MoonTools.Bonk/Test/EPA2DTest.cs

74 lines
2.7 KiB
C#
Raw Normal View History

2019-09-06 08:11:58 +00:00
using NUnit.Framework;
using FluentAssertions;
using System;
2019-10-31 23:19:30 +00:00
using System.Numerics;
2019-09-06 08:11:58 +00:00
using MoonTools.Core.Structs;
using MoonTools.Core.Bonk;
namespace Tests
{
public class EPA2DTest
{
[Test]
public void RectangleOverlap()
{
2020-01-05 07:24:36 +00:00
var squareA = new TransformedShape2D<Rectangle>(new Rectangle(2, 2), Transform2D.DefaultTransform);
var squareB = new TransformedShape2D<Rectangle>(new Rectangle(2, 2), new Transform2D(new Vector2(1.5f, 0)));
2019-09-06 08:11:58 +00:00
2020-01-05 07:24:36 +00:00
var (result, simplex) = NarrowPhase.FindCollisionSimplex(squareA, squareB);
2019-09-06 08:11:58 +00:00
2019-10-25 10:46:47 +00:00
result.Should().BeTrue();
2019-09-06 08:11:58 +00:00
2020-01-05 07:24:36 +00:00
var intersection = NarrowPhase.Intersect(squareA, squareB, simplex);
2019-09-06 08:11:58 +00:00
intersection.X.Should().Be(1f);
intersection.Y.Should().Be(0);
2019-10-26 05:00:34 +00:00
2020-01-05 07:24:36 +00:00
var movedTransform = new Transform2D(-(intersection * 1.01f)); // move a tiny bit past
2019-10-26 05:00:34 +00:00
2020-01-05 07:24:36 +00:00
NarrowPhase.TestCollision(squareA.Compose(movedTransform), squareB).Should().BeFalse();
2019-09-06 08:11:58 +00:00
}
[Test]
public void CircleOverlap()
{
2020-01-05 07:24:36 +00:00
var circleA = new TransformedShape2D<Circle>(new Circle(2), Transform2D.DefaultTransform);
var circleB = new TransformedShape2D<Circle>(new Circle(1), new Transform2D(new Vector2(1, 1)));
2019-09-06 08:11:58 +00:00
2020-01-05 07:24:36 +00:00
var (result, simplex) = NarrowPhase.FindCollisionSimplex(circleA, circleB);
2019-09-06 08:11:58 +00:00
2019-10-25 10:46:47 +00:00
result.Should().BeTrue();
2019-09-06 08:11:58 +00:00
2020-01-05 07:24:36 +00:00
var intersection = NarrowPhase.Intersect(circleA, circleB, simplex);
2019-09-06 08:11:58 +00:00
2020-01-05 07:24:36 +00:00
var ix = (2 * (float)Math.Cos(Math.PI / 4)) - ((1 * (float)Math.Cos(5 * Math.PI / 4)) + 1);
var iy = (2 * (float)Math.Sin(Math.PI / 4)) - ((1 * (float)Math.Sin(5 * Math.PI / 4)) + 1);
2019-09-06 08:11:58 +00:00
intersection.X.Should().BeApproximately(ix, 0.01f);
intersection.Y.Should().BeApproximately(iy, 0.01f);
2019-10-26 05:00:34 +00:00
2020-01-05 07:24:36 +00:00
var movedTransform = new Transform2D(-(intersection * 1.01f)); // move a tiny bit past
2019-10-26 05:00:34 +00:00
2020-01-05 07:24:36 +00:00
NarrowPhase.TestCollision(circleA.Compose(movedTransform), circleB).Should().BeFalse();
2019-09-06 08:11:58 +00:00
}
[Test]
public void LineRectangleOverlap()
{
2020-01-05 07:24:36 +00:00
var line = new TransformedShape2D<Line>(new Line(new Position2D(-4, -4), new Position2D(4, 4)), Transform2D.DefaultTransform);
var square = new TransformedShape2D<Rectangle>(new Rectangle(2, 2), Transform2D.DefaultTransform);
2019-09-06 08:11:58 +00:00
2020-01-05 07:24:36 +00:00
var (result, simplex) = NarrowPhase.FindCollisionSimplex(line, square);
2019-09-06 08:11:58 +00:00
2019-10-25 10:46:47 +00:00
result.Should().BeTrue();
2019-09-06 08:11:58 +00:00
2020-01-05 07:24:36 +00:00
var intersection = NarrowPhase.Intersect(line, square, simplex);
2019-09-06 08:11:58 +00:00
2020-01-05 07:24:36 +00:00
var movedTransform = new Transform2D(-(intersection * 1.01f)); // move a tiny bit past
2019-10-26 05:00:34 +00:00
2020-01-05 07:24:36 +00:00
NarrowPhase.TestCollision(line.Compose(movedTransform), square).Should().BeFalse();
2019-09-06 08:11:58 +00:00
}
}
}