126 lines
4.0 KiB
C#
126 lines
4.0 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using System;
|
|
|
|
namespace SemiColinGames.Tests {
|
|
[TestClass]
|
|
public class GeometryTests {
|
|
[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);
|
|
}
|
|
}
|
|
} |