MoonTools.Bonk/Bonk/Shapes/Simplex.cs

66 lines
2.0 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
{
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);
}
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);
}
}
}