diff --git a/Bonk/GJK2D.cs b/Bonk/GJK2D.cs index 31a8783..1d9ae3d 100644 --- a/Bonk/GJK2D.cs +++ b/Bonk/GJK2D.cs @@ -2,90 +2,10 @@ using MoonTools.Core.Structs; using System; using MoonTools.Core.Bonk.Extensions; -using System.Collections.Generic; namespace MoonTools.Core.Bonk { - public struct MinkowskiDifference : IEquatable - { - private IShape2D shapeA; - private Transform2D transformA; - private IShape2D shapeB; - private Transform2D transformB; - public MinkowskiDifference(IShape2D shapeA, Transform2D transformA, IShape2D shapeB, Transform2D transformB) - { - this.shapeA = shapeA; - this.transformA = transformA; - this.shapeB = shapeB; - this.transformB = transformB; - } - - public bool Equals(MinkowskiDifference other) - { - return - shapeA == other.shapeA && - transformA.Equals(other.transformA) && - shapeB == other.shapeB && - transformB.Equals(other.transformB); - } - - public Vector2 Support(Vector2 direction) - { - return shapeA.Support(direction, transformA) - shapeB.Support(-direction, transformB); - } - } - - public struct Simplex : IShape2D - { - MinkowskiDifference minkowskiDifference; - Vector2 directionA; - Vector2 directionB; - - public Simplex(MinkowskiDifference minkowskiDifference, Vector2 directionA, Vector2 directionB) - { - this.minkowskiDifference = minkowskiDifference; - this.directionA = directionA; - this.directionB = directionB; - } - - public IEnumerable Vertices - { - get - { - yield return (Position2D)Support(directionA); - yield return (Position2D)Support(directionB); - yield return (Position2D)Support(-(directionB - directionA).Perpendicular()); - } - } - - public AABB AABB(Transform2D transform) - { - return Bonk.AABB.FromTransformedVertices(Vertices, transform); - } - - public bool Equals(IShape2D other) - { - if (other is Simplex polytope) - { - return minkowskiDifference.Equals(polytope.minkowskiDifference) && - directionA == polytope.directionA && - directionB == polytope.directionB; - } - - return false; - } - - public Vector2 Support(Vector2 direction) - { - return minkowskiDifference.Support(direction); - } - - public Vector2 Support(Vector2 direction, Transform2D transform) - { - return Vector2.Transform(Support(direction), transform.TransformMatrix); - } - } public static class GJK2D { diff --git a/Bonk/MinkowskiDifference.cs b/Bonk/MinkowskiDifference.cs new file mode 100644 index 0000000..bee297a --- /dev/null +++ b/Bonk/MinkowskiDifference.cs @@ -0,0 +1,36 @@ +using System; +using Microsoft.Xna.Framework; +using MoonTools.Core.Structs; + +namespace MoonTools.Core.Bonk +{ + public struct MinkowskiDifference : IEquatable + { + private IShape2D shapeA; + private Transform2D transformA; + private IShape2D shapeB; + private Transform2D transformB; + + public MinkowskiDifference(IShape2D shapeA, Transform2D transformA, IShape2D shapeB, Transform2D transformB) + { + this.shapeA = shapeA; + this.transformA = transformA; + this.shapeB = shapeB; + this.transformB = transformB; + } + + public bool Equals(MinkowskiDifference other) + { + return + shapeA == other.shapeA && + transformA.Equals(other.transformA) && + shapeB == other.shapeB && + transformB.Equals(other.transformB); + } + + public Vector2 Support(Vector2 direction) + { + return shapeA.Support(direction, transformA) - shapeB.Support(-direction, transformB); + } + } +} \ No newline at end of file diff --git a/Bonk/Circle.cs b/Bonk/Shapes/Circle.cs similarity index 100% rename from Bonk/Circle.cs rename to Bonk/Shapes/Circle.cs diff --git a/Bonk/Line.cs b/Bonk/Shapes/Line.cs similarity index 100% rename from Bonk/Line.cs rename to Bonk/Shapes/Line.cs diff --git a/Bonk/Polygon.cs b/Bonk/Shapes/Polygon.cs similarity index 100% rename from Bonk/Polygon.cs rename to Bonk/Shapes/Polygon.cs diff --git a/Bonk/Rectangle.cs b/Bonk/Shapes/Rectangle.cs similarity index 100% rename from Bonk/Rectangle.cs rename to Bonk/Shapes/Rectangle.cs diff --git a/Bonk/Shapes/Simplex.cs b/Bonk/Shapes/Simplex.cs new file mode 100644 index 0000000..9e09334 --- /dev/null +++ b/Bonk/Shapes/Simplex.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using Microsoft.Xna.Framework; +using MoonTools.Core.Structs; +using MoonTools.Core.Bonk.Extensions; + +namespace MoonTools.Core.Bonk +{ + public struct Simplex : IShape2D + { + MinkowskiDifference minkowskiDifference; + Vector2 directionA; + Vector2 directionB; + + public Simplex(MinkowskiDifference minkowskiDifference, Vector2 directionA, Vector2 directionB) + { + this.minkowskiDifference = minkowskiDifference; + this.directionA = directionA; + this.directionB = directionB; + } + + public IEnumerable Vertices + { + get + { + yield return (Position2D)Support(directionA); + yield return (Position2D)Support(directionB); + yield return (Position2D)Support(-(directionB - directionA).Perpendicular()); + } + } + + public AABB AABB(Transform2D transform) + { + return Bonk.AABB.FromTransformedVertices(Vertices, transform); + } + + public bool Equals(IShape2D other) + { + if (other is Simplex polytope) + { + return minkowskiDifference.Equals(polytope.minkowskiDifference) && + directionA == polytope.directionA && + directionB == polytope.directionB; + } + + return false; + } + + public Vector2 Support(Vector2 direction) + { + return minkowskiDifference.Support(direction); + } + + public Vector2 Support(Vector2 direction, Transform2D transform) + { + return Vector2.Transform(Support(direction), transform.TransformMatrix); + } + } +} \ No newline at end of file diff --git a/Bonk/Vector2Extensions.cs b/Bonk/Vector2Extensions.cs index f411b4a..016a8cb 100644 --- a/Bonk/Vector2Extensions.cs +++ b/Bonk/Vector2Extensions.cs @@ -2,15 +2,15 @@ using Microsoft.Xna.Framework; namespace MoonTools.Core.Bonk.Extensions { - public static class Vector2Extensions + internal static class Vector2Extensions { - public static Vector2 Cross(this Vector2 a, Vector2 b) + internal static Vector2 Cross(this Vector2 a, Vector2 b) { var vec3 = Vector3.Cross(new Vector3(a.X, a.Y, 0), new Vector3(b.X, b.Y, 0)); return new Vector2(vec3.X, vec3.Y); } - public static Vector2 Perpendicular(this Vector2 v) + internal static Vector2 Perpendicular(this Vector2 v) { return new Vector2(v.Y, -v.X); }