161 lines
6.0 KiB
C#
161 lines
6.0 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using System;
|
|
|
|
namespace SemiColinGames.Tests {
|
|
[TestClass]
|
|
public class GeometryTests {
|
|
|
|
public void AssertVectorsAreEqual(Vector2 v1, Vector2 v2) {
|
|
Assert.AreEqual(v1.X, v2.X, 1e-5);
|
|
Assert.AreEqual(v1.Y, v2.Y, 1e-5);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestVector2Rotate() {
|
|
Vector2 v = new Vector2(10, 0);
|
|
AssertVectorsAreEqual(new Vector2(10, 0), v.Rotate(FMath.DegToRad(0)));
|
|
AssertVectorsAreEqual(new Vector2(0, 10), v.Rotate(FMath.DegToRad(90)));
|
|
AssertVectorsAreEqual(new Vector2(-10, 0), v.Rotate(FMath.DegToRad(180)));
|
|
AssertVectorsAreEqual(new Vector2(0, -10), v.Rotate(FMath.DegToRad(270)));
|
|
AssertVectorsAreEqual(new Vector2(10, 0), v.Rotate(FMath.DegToRad(360)));
|
|
AssertVectorsAreEqual(new Vector2(0, 10), v.Rotate(FMath.DegToRad(450)));
|
|
|
|
AssertVectorsAreEqual(new Vector2(0, -10), v.Rotate(FMath.DegToRad(-90)));
|
|
AssertVectorsAreEqual(new Vector2(-10, 0), v.Rotate(FMath.DegToRad(-180)));
|
|
AssertVectorsAreEqual(new Vector2(0, 10), v.Rotate(FMath.DegToRad(-270)));
|
|
AssertVectorsAreEqual(new Vector2(10, 0), v.Rotate(FMath.DegToRad(-360)));
|
|
AssertVectorsAreEqual(new Vector2(0, -10), v.Rotate(FMath.DegToRad(-450)));
|
|
|
|
AssertVectorsAreEqual(new Vector2(7.0710678f, 7.0710678f), v.Rotate(FMath.DegToRad(45)));
|
|
AssertVectorsAreEqual(new Vector2(8.6602540f, 5f), v.Rotate(FMath.DegToRad(30)));
|
|
AssertVectorsAreEqual(new Vector2(5f, 8.6602540f), v.Rotate(FMath.DegToRad(60)));
|
|
AssertVectorsAreEqual(new Vector2(-5f, 8.6602540f), v.Rotate(FMath.DegToRad(120)));
|
|
AssertVectorsAreEqual(new Vector2(-8.6602540f, 5f), v.Rotate(FMath.DegToRad(150)));
|
|
|
|
AssertVectorsAreEqual(new Vector2(7.0710678f, -7.0710678f), v.Rotate(FMath.DegToRad(-45)));
|
|
AssertVectorsAreEqual(new Vector2(8.6602540f, -5f), v.Rotate(FMath.DegToRad(-30)));
|
|
AssertVectorsAreEqual(new Vector2(5f, -8.6602540f), v.Rotate(FMath.DegToRad(-60)));
|
|
AssertVectorsAreEqual(new Vector2(-5f, -8.6602540f), v.Rotate(FMath.DegToRad(-120)));
|
|
AssertVectorsAreEqual(new Vector2(-8.6602540f, -5f), v.Rotate(FMath.DegToRad(-150)));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestIntersectSegmentNotColliding() {
|
|
AABB box = new AABB(new Vector2(0, 0), new Vector2(8, 8));
|
|
Assert.IsNull(box.IntersectSegment(new Vector2(-16, -16), new Vector2(32, 0)));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestIntersectSegmentHit() {
|
|
AABB box = new AABB(new Vector2(0, 0), new Vector2(8, 8));
|
|
var point = new Vector2(-16, 4);
|
|
var delta = new Vector2(32, 0);
|
|
Hit? maybeHit = box.IntersectSegment(point, delta);
|
|
|
|
Assert.IsNotNull(maybeHit);
|
|
Hit hit = (Hit) maybeHit;
|
|
|
|
Assert.AreEqual(box, hit.Collider);
|
|
Assert.AreEqual(0.25, hit.Time);
|
|
|
|
Assert.AreEqual(point.X + delta.X * hit.Time, hit.Position.X);
|
|
Assert.AreEqual(point.Y + delta.Y * hit.Time, hit.Position.Y);
|
|
|
|
Assert.AreEqual((1.0f - hit.Time) * -delta.X, hit.Delta.X);
|
|
Assert.AreEqual((1.0f - hit.Time) * -delta.Y, hit.Delta.Y);
|
|
|
|
Assert.AreEqual(-1, hit.Normal.X);
|
|
Assert.AreEqual(0, hit.Normal.Y);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestIntersectSegmentFromInsideBox() {
|
|
AABB box = new AABB(new Vector2(0, 0), new Vector2(8, 8));
|
|
var point = new Vector2(-4, 4);
|
|
var delta = new Vector2(32, 0);
|
|
Hit? maybeHit = box.IntersectSegment(point, delta);
|
|
|
|
Assert.IsNotNull(maybeHit);
|
|
Hit hit = (Hit) maybeHit;
|
|
|
|
Assert.AreEqual(box, hit.Collider);
|
|
Assert.AreEqual(0.0, hit.Time);
|
|
|
|
Assert.AreEqual(-4, hit.Position.X);
|
|
Assert.AreEqual(4, hit.Position.Y);
|
|
|
|
Assert.AreEqual(-delta.X, hit.Delta.X);
|
|
Assert.AreEqual(-delta.Y, hit.Delta.Y);
|
|
|
|
Assert.AreEqual(-1, hit.Normal.X);
|
|
Assert.AreEqual(0, hit.Normal.Y);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestIntersectSegmentWithPadding() {
|
|
AABB box = new AABB(new Vector2(0, 0), new Vector2(8, 8));
|
|
var point = new Vector2(-16, 4);
|
|
var delta = new Vector2(32, 0);
|
|
int padding = 4;
|
|
Hit? maybeHit = box.IntersectSegment(point, delta, new Vector2(padding, padding));
|
|
|
|
Assert.IsNotNull(maybeHit);
|
|
Hit hit = (Hit) maybeHit;
|
|
|
|
Assert.AreEqual(box, hit.Collider);
|
|
Assert.AreEqual(0.125, hit.Time);
|
|
|
|
Assert.AreEqual(point.X + delta.X * hit.Time, hit.Position.X);
|
|
Assert.AreEqual(point.Y + delta.Y * hit.Time, hit.Position.Y);
|
|
|
|
Assert.AreEqual((1.0f - hit.Time) * -delta.X, hit.Delta.X);
|
|
Assert.AreEqual((1.0f - hit.Time) * -delta.Y, hit.Delta.Y);
|
|
|
|
Assert.AreEqual(-1, hit.Normal.X);
|
|
Assert.AreEqual(0, hit.Normal.Y);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestIntersectSegmentFromTwoDirections() {
|
|
AABB box = new AABB(new Vector2(0, 0), new Vector2(32, 32));
|
|
var farPos = new Vector2(64, 0);
|
|
var farToNearDelta = new Vector2(-32, 0);
|
|
Assert.IsNull(box.IntersectSegment(farPos, farToNearDelta));
|
|
|
|
var nearPos = new Vector2(32, 0);
|
|
var nearToFarDelta = new Vector2(32, 0);
|
|
Assert.IsNull(box.IntersectSegment(nearPos, nearToFarDelta));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestIntersectSegmentXAxisAligned() {
|
|
AABB box = new AABB(new Vector2(0, 0), new Vector2(16, 16));
|
|
var pos = new Vector2(-32, 0);
|
|
var delta = new Vector2(64, 0);
|
|
Hit? maybeHit = box.IntersectSegment(pos, delta);
|
|
|
|
Assert.IsNotNull(maybeHit);
|
|
Hit hit = (Hit) maybeHit;
|
|
|
|
Assert.AreEqual(0.25, hit.Time);
|
|
Assert.AreEqual(-1, hit.Normal.X);
|
|
Assert.AreEqual(0, hit.Normal.Y);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestIntersectSegmentYAxisAligned() {
|
|
AABB box = new AABB(new Vector2(0, 0), new Vector2(16, 16));
|
|
var pos = new Vector2(0, -32);
|
|
var delta = new Vector2(0, 64);
|
|
Hit? maybeHit = box.IntersectSegment(pos, delta);
|
|
|
|
Assert.IsNotNull(maybeHit);
|
|
Hit hit = (Hit) maybeHit;
|
|
|
|
Assert.AreEqual(0.25, hit.Time);
|
|
Assert.AreEqual(0, hit.Normal.X);
|
|
Assert.AreEqual(-1, hit.Normal.Y);
|
|
}
|
|
}
|
|
} |