MoonTools.Bonk/Test/EPA2DTest.cs

83 lines
3.0 KiB
C#
Raw Permalink 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;
2020-02-21 02:07:59 +00:00
using MoonTools.Structs;
using MoonTools.Bonk;
2019-09-06 08:11:58 +00:00
namespace Tests
{
public class EPA2DTest
{
[Test]
public void RectangleOverlap()
{
var squareA = new Rectangle(-1, -1, 2, 2);
2019-09-06 08:11:58 +00:00
var transformA = Transform2D.DefaultTransform;
var squareB = new Rectangle(-1, -1, 2, 2);
2019-09-06 08:11:58 +00:00
var transformB = new Transform2D(new Vector2(1.5f, 0));
var (result, simplex) = NarrowPhase.FindCollisionSimplex(squareA, transformA, squareB, transformB);
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
var intersection = NarrowPhase.Intersect(squareA, transformA, squareB, transformB, 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-02-21 02:07:59 +00:00
var movedTransform = new Transform2D(transformA.Position - (intersection * 2)); // move past
2019-10-26 05:00:34 +00:00
NarrowPhase.TestCollision(squareA, movedTransform, squareB, transformB).Should().BeFalse();
2019-09-06 08:11:58 +00:00
}
[Test]
public void CircleOverlap()
{
var circleA = new Circle(2);
var transformA = Transform2D.DefaultTransform;
var circleB = new Circle(1);
var transformB = new Transform2D(new Vector2(1, 1));
var (result, simplex) = NarrowPhase.FindCollisionSimplex(circleA, transformA, circleB, transformB);
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
var intersection = NarrowPhase.Intersect(circleA, transformA, circleB, transformB, simplex);
2019-09-06 08:11:58 +00:00
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);
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-02-21 02:07:59 +00:00
var movedTransform = new Transform2D(transformA.Position - (intersection * 2)); // move past
2019-10-26 05:00:34 +00:00
NarrowPhase.TestCollision(circleA, movedTransform, circleB, transformB).Should().BeFalse();
2019-09-06 08:11:58 +00:00
}
[Test]
public void LineRectangleOverlap()
{
var line = new Line(new Position2D(-4, -4), new Position2D(4, 4));
var transformA = Transform2D.DefaultTransform;
var square = new Rectangle(-1, -1, 2, 2);
2019-09-06 08:11:58 +00:00
var transformB = Transform2D.DefaultTransform;
var (result, simplex) = NarrowPhase.FindCollisionSimplex(line, transformA, square, transformB);
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
var intersection = NarrowPhase.Intersect(line, transformA, square, transformB, simplex);
2019-09-06 08:11:58 +00:00
2020-02-21 02:07:59 +00:00
intersection.X.Should().Be(1);
intersection.Y.Should().Be(-1);
var movedTransform = new Transform2D(transformA.Position - (intersection * 2)); // move past
2019-10-26 05:00:34 +00:00
NarrowPhase.TestCollision(line, movedTransform, square, transformB).Should().BeFalse();
2019-09-06 08:11:58 +00:00
}
}
}