MoonTools.Bonk/Bonk/Shapes/Simplex.cs

101 lines
3.4 KiB
C#
Raw Normal View History

2019-10-25 19:42:39 +00:00
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using MoonTools.Core.Structs;
using MoonTools.Core.Bonk.Extensions;
namespace MoonTools.Core.Bonk
{
2019-10-25 21:01:36 +00:00
/// <summary>
/// A simplex is a shape used to calculate overlap. It is defined by a Minkowski difference and two direction vectors.
/// </summary>
2019-10-25 19:42:39 +00:00
public struct Simplex : IShape2D
{
MinkowskiDifference minkowskiDifference;
Vector2 directionA;
Vector2 directionB;
2019-10-25 20:09:03 +00:00
public Vector2 DirectionA { get { return directionA; } }
public Vector2 DirectionB { get { return directionB; } }
2019-10-25 19:42:39 +00:00
public Simplex(MinkowskiDifference minkowskiDifference, Vector2 directionA, Vector2 directionB)
{
this.minkowskiDifference = minkowskiDifference;
this.directionA = directionA;
this.directionB = directionB;
}
2019-10-25 20:09:03 +00:00
public Simplex WithDirections(Vector2 a, Vector2 b)
{
return new Simplex(minkowskiDifference, a, b);
}
2019-10-25 19:42:39 +00:00
public IEnumerable<Position2D> 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);
}
2019-10-25 23:20:43 +00:00
public Vector2 Support(Vector2 direction)
{
return minkowskiDifference.Support(direction);
}
public Vector2 Support(Vector2 direction, Transform2D transform)
{
return Vector2.Transform(Support(direction), transform.TransformMatrix);
}
public override bool Equals(object obj)
{
if (obj is IShape2D other)
{
return Equals(other);
}
return false;
}
2019-10-25 19:42:39 +00:00
public bool Equals(IShape2D other)
{
2019-10-25 23:20:43 +00:00
if (other is Simplex otherSimplex)
2019-10-25 19:42:39 +00:00
{
2019-10-25 23:20:43 +00:00
return minkowskiDifference == otherSimplex.minkowskiDifference &&
((directionA == otherSimplex.directionA && directionB == otherSimplex.directionB) ||
(directionA == otherSimplex.directionB && directionB == otherSimplex.directionA));
2019-10-25 19:42:39 +00:00
}
return false;
}
2019-10-25 23:20:43 +00:00
public override int GetHashCode()
2019-10-25 19:42:39 +00:00
{
2019-10-25 23:20:43 +00:00
var hashCode = 74270316;
hashCode = hashCode * -1521134295 + EqualityComparer<MinkowskiDifference>.Default.GetHashCode(minkowskiDifference);
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2>.Default.GetHashCode(directionA);
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2>.Default.GetHashCode(directionB);
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2>.Default.GetHashCode(DirectionA);
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2>.Default.GetHashCode(DirectionB);
hashCode = hashCode * -1521134295 + EqualityComparer<IEnumerable<Position2D>>.Default.GetHashCode(Vertices);
return hashCode;
2019-10-25 19:42:39 +00:00
}
2019-10-25 23:20:43 +00:00
public static bool operator ==(Simplex a, Simplex b)
2019-10-25 19:42:39 +00:00
{
2019-10-25 23:20:43 +00:00
return a.Equals(b);
}
public static bool operator !=(Simplex a, Simplex b)
{
return !(a == b);
2019-10-25 19:42:39 +00:00
}
}
}