From 1c95dd1591848ebcf0a33a0e2d357889d2271bcb Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Fri, 25 Oct 2019 13:14:37 -0700 Subject: [PATCH] optimized EPA2D --- Bonk/Bonk.csproj | 1 + Bonk/EPA2D.cs | 14 +++---- Bonk/SimplexVertices.cs | 91 ----------------------------------------- 3 files changed, 8 insertions(+), 98 deletions(-) delete mode 100644 Bonk/SimplexVertices.cs diff --git a/Bonk/Bonk.csproj b/Bonk/Bonk.csproj index 7999b43..2110f49 100644 --- a/Bonk/Bonk.csproj +++ b/Bonk/Bonk.csproj @@ -17,5 +17,6 @@ + \ No newline at end of file diff --git a/Bonk/EPA2D.cs b/Bonk/EPA2D.cs index 6b3e1a5..9b04e43 100644 --- a/Bonk/EPA2D.cs +++ b/Bonk/EPA2D.cs @@ -4,11 +4,10 @@ * https://blog.hamaluik.ca/posts/building-a-collision-engine-part-2-2d-penetration-vectors/ */ +using Collections.Pooled; using Microsoft.Xna.Framework; -using MoonTools.Core.Bonk.Extensions; using MoonTools.Core.Structs; using System; -using System.Collections.Generic; namespace MoonTools.Core.Bonk { @@ -18,15 +17,14 @@ namespace MoonTools.Core.Bonk CounterClockwise } - // TODO: convert SimplexVertices to PooledList public static class EPA2D { // vector returned gives direction from A to B - public static Vector2 Intersect(IShape2D shapeA, Transform2D Transform2DA, IShape2D shapeB, Transform2D Transform2DB, Simplex givenSimplex) + public static Vector2 Intersect(IShape2D shapeA, Transform2D Transform2DA, IShape2D shapeB, Transform2D Transform2DB, Simplex simplex) { - var simplexVertices = new SimplexVertices(new Vector2?[36]); + var simplexVertices = new PooledList(36, ClearMode.Always); - foreach (var vertex in givenSimplex.Vertices) + foreach (var vertex in simplex.Vertices) { simplexVertices.Add(vertex); } @@ -57,10 +55,12 @@ namespace MoonTools.Core.Bonk } } + simplexVertices.Dispose(); + return intersection; } - private static Edge FindClosestEdge(PolygonWinding winding, SimplexVertices simplexVertices) + private static Edge FindClosestEdge(PolygonWinding winding, PooledList simplexVertices) { var closestDistance = float.PositiveInfinity; var closestNormal = Vector2.Zero; diff --git a/Bonk/SimplexVertices.cs b/Bonk/SimplexVertices.cs deleted file mode 100644 index b89b753..0000000 --- a/Bonk/SimplexVertices.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using Microsoft.Xna.Framework; - -namespace MoonTools.Core.Bonk -{ - public struct SimplexVertices : IEnumerable - { - public Vector2?[] vertices; - - /// - /// Make sure to pass in all nulls - /// - public SimplexVertices(Vector2?[] vertices) - { - this.vertices = vertices; - } - - public Vector2 this[int key] - { - get - { - if (!vertices[key].HasValue) { throw new IndexOutOfRangeException(); } - return vertices[key].Value; - } - set - { - vertices[key] = value; - } - } - - public int Count { - get - { - for (int i = 0; i < vertices.Length; i++) - { - if (!vertices[i].HasValue) { return i; } - } - return vertices.Length; - } - } - - public void Add(Vector2 vertex) - { - if (Count > vertices.Length - 1) { throw new IndexOutOfRangeException(); } - - vertices[Count] = vertex; - } - - public void Insert(int index, Vector2 vertex) - { - if (Count >= vertices.Length || index > vertices.Length - 1) { throw new IndexOutOfRangeException(); } - - var currentCount = Count; - - for (int i = currentCount - 1; i >= index; i--) - { - vertices[i + 1] = vertices[i]; - } - - vertices[index] = vertex; - } - - public IEnumerator GetEnumerator() - { - foreach (Vector2? vec in vertices) - { - if (!vec.HasValue) { yield break; } - yield return vec.Value; - } - } - - public void RemoveAt(int index) - { - if (index > vertices.Length - 1 || index > Count) { throw new ArgumentOutOfRangeException(); } - - for (int i = vertices.Length - 2; i >= index; i--) - { - vertices[i] = vertices[i + 1]; - } - - vertices[vertices.Length - 1] = null; - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - } -} \ No newline at end of file