From 7875342da8275917ff4a01426408fce02001e45a Mon Sep 17 00:00:00 2001
From: Evan Hemsley <2342303+ehemsley@users.noreply.github.com>
Date: Sun, 5 Jan 2020 21:36:31 -0800
Subject: [PATCH] change how multishape shapes are transformed and add point
sweep test
---
Bonk/MultiShape.cs | 11 +++++++++--
Bonk/NarrowPhase/NarrowPhase.cs | 8 ++++----
Bonk/SweepTest/SweepResult.cs | 11 +++--------
Bonk/SweepTest/SweepTest.cs | 13 +++++++++----
4 files changed, 25 insertions(+), 18 deletions(-)
diff --git a/Bonk/MultiShape.cs b/Bonk/MultiShape.cs
index eb34cda..202120a 100644
--- a/Bonk/MultiShape.cs
+++ b/Bonk/MultiShape.cs
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Collections.Immutable;
+using System.Numerics;
using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk
@@ -22,11 +23,17 @@ namespace MoonTools.Core.Bonk
return AABB.Transformed(AABB, transform);
}
- public IEnumerable<(IShape2D, Transform2D)> TransformedShapeTransforms(Transform2D transform)
+ ///
+ /// Moves the shapes by pivoting with an offset transform.
+ ///
+ ///
+ ///
+ public IEnumerable<(IShape2D, Transform2D)> TransformShapesUsingOffset(Transform2D offsetTransform)
{
foreach (var (shape, shapeTransform) in ShapeTransformPairs)
{
- yield return (shape, transform.Compose(shapeTransform));
+ var newTransform = new Transform2D(Vector2.Transform(shapeTransform.Position, offsetTransform.TransformMatrix), offsetTransform.Rotation, offsetTransform.Scale);
+ yield return (shape, newTransform);
}
}
diff --git a/Bonk/NarrowPhase/NarrowPhase.cs b/Bonk/NarrowPhase/NarrowPhase.cs
index fe69be5..0ef1543 100644
--- a/Bonk/NarrowPhase/NarrowPhase.cs
+++ b/Bonk/NarrowPhase/NarrowPhase.cs
@@ -70,7 +70,7 @@ namespace MoonTools.Core.Bonk
///
public static bool TestCollision(MultiShape multiShape, Transform2D multiShapeTransform, IShape2D shape, Transform2D shapeTransform)
{
- foreach (var (otherShape, otherTransform) in multiShape.TransformedShapeTransforms(multiShapeTransform))
+ foreach (var (otherShape, otherTransform) in multiShape.TransformShapesUsingOffset(multiShapeTransform))
{
if (TestCollision(shape, shapeTransform, otherShape, otherTransform)) { return true; }
}
@@ -88,7 +88,7 @@ namespace MoonTools.Core.Bonk
///
public static bool TestCollision(IShape2D shape, Transform2D shapeTransform, MultiShape multiShape, Transform2D multiShapeTransform)
{
- foreach (var (otherShape, otherTransform) in multiShape.TransformedShapeTransforms(multiShapeTransform))
+ foreach (var (otherShape, otherTransform) in multiShape.TransformShapesUsingOffset(multiShapeTransform))
{
if (TestCollision(shape, shapeTransform, otherShape, otherTransform)) { return true; }
}
@@ -106,9 +106,9 @@ namespace MoonTools.Core.Bonk
///
public static bool TestCollision(MultiShape multiShapeA, Transform2D transformA, MultiShape multiShapeB, Transform2D transformB)
{
- foreach (var (shapeA, shapeTransformA) in multiShapeA.TransformedShapeTransforms(transformA))
+ foreach (var (shapeA, shapeTransformA) in multiShapeA.TransformShapesUsingOffset(transformA))
{
- foreach (var (shapeB, shapeTransformB) in multiShapeB.TransformedShapeTransforms(transformB))
+ foreach (var (shapeB, shapeTransformB) in multiShapeB.TransformShapesUsingOffset(transformB))
{
if (TestCollision(shapeA, shapeTransformA, shapeB, shapeTransformB)) { return true; }
}
diff --git a/Bonk/SweepTest/SweepResult.cs b/Bonk/SweepTest/SweepResult.cs
index 2575090..d6dc84d 100644
--- a/Bonk/SweepTest/SweepResult.cs
+++ b/Bonk/SweepTest/SweepResult.cs
@@ -1,26 +1,21 @@
using System;
using System.Numerics;
-using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk
{
- public struct SweepResult where T : IEquatable where U : struct, IShape2D
+ public struct SweepResult where T : IEquatable
{
- public static SweepResult False = new SweepResult();
+ public static SweepResult False = new SweepResult();
public bool Hit { get; }
public Vector2 Motion { get; }
public T ID { get; }
- public U Shape { get; }
- public Transform2D Transform { get; }
- public SweepResult(bool hit, Vector2 motion, T id, U shape, Transform2D transform)
+ public SweepResult(bool hit, Vector2 motion, T id)
{
Hit = hit;
Motion = motion;
ID = id;
- Shape = shape;
- Transform = transform;
}
}
}
diff --git a/Bonk/SweepTest/SweepTest.cs b/Bonk/SweepTest/SweepTest.cs
index 4dd53d4..652ebcd 100644
--- a/Bonk/SweepTest/SweepTest.cs
+++ b/Bonk/SweepTest/SweepTest.cs
@@ -7,7 +7,7 @@ namespace MoonTools.Core.Bonk
public static class SweepTest
{
///
- /// Performs a sweep test on rectangles. Returns the position 1 pixel before overlap occurs.
+ /// Performs a sweep test on and against rectangles. Returns the position 1 pixel before overlap occurs.
///
///
/// A spatial hash.
@@ -15,7 +15,7 @@ namespace MoonTools.Core.Bonk
/// A transform by which to transform the IHasAABB2D.
/// Given in world-space.
///
- public static SweepResult Rectangle(SpatialHash spatialHash, Rectangle rectangle, Transform2D transform, Vector2 ray) where T : IEquatable
+ public static SweepResult Test(SpatialHash spatialHash, Rectangle rectangle, Transform2D transform, Vector2 ray) where T : IEquatable
{
var transformedAABB = rectangle.TransformedAABB(transform);
var sweepBox = SweepBox(transformedAABB, ray);
@@ -108,14 +108,19 @@ namespace MoonTools.Core.Bonk
var overlapPosition = ray * shortestDistance;
var correctionX = -Math.Sign(ray.X);
var correctionY = -Math.Sign(ray.Y);
- return new SweepResult(true, new Position2D((int)overlapPosition.X + correctionX, (int)overlapPosition.Y + correctionY), nearestID, nearestRectangle.Value, nearestTransform.Value);
+ return new SweepResult(true, new Position2D((int)overlapPosition.X + correctionX, (int)overlapPosition.Y + correctionY), nearestID);
}
else
{
- return SweepResult.False;
+ return SweepResult.False;
}
}
+ public static SweepResult Test(SpatialHash spatialHash, Point point, Transform2D transform, Vector2 ray) where T : IEquatable
+ {
+ return Test(spatialHash, new Rectangle(0, 0), transform, ray);
+ }
+
private static AABB SweepBox(AABB aabb, Vector2 ray)
{
return new AABB(