forked from MoonsideGames/MoonTools.Bonk
				
			optimized EPA2D
							parent
							
								
									a3eec5c01c
								
							
						
					
					
						commit
						1c95dd1591
					
				| 
						 | 
				
			
			@ -17,5 +17,6 @@
 | 
			
		|||
  <ItemGroup>
 | 
			
		||||
    <PackageReference Include="MoonTools.Core.Structs" Version="1.0.1"/>
 | 
			
		||||
    <PackageReference Include="morelinq" Version="3.2.0"/>
 | 
			
		||||
    <PackageReference Include="Collections.Pooled" Version="1.0.82"/>
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
</Project>
 | 
			
		||||
| 
						 | 
				
			
			@ -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<Vector2>(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<Vector2> simplexVertices)
 | 
			
		||||
        {
 | 
			
		||||
            var closestDistance = float.PositiveInfinity;
 | 
			
		||||
            var closestNormal = Vector2.Zero;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<Vector2>
 | 
			
		||||
    {
 | 
			
		||||
        public Vector2?[] vertices;
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Make sure to pass in all nulls
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        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<Vector2> 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();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue