optimize rectangle support function
parent
f9df547302
commit
9e7d16b0a9
|
@ -38,21 +38,36 @@ namespace MoonTools.Core.Bonk
|
||||||
AABB = new AABB(minX, minY, maxX, maxY);
|
AABB = new AABB(minX, minY, maxX, maxY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Vector2 Support(Vector2 direction)
|
||||||
|
{
|
||||||
|
if (direction.X >= 0 && direction.Y >= 0)
|
||||||
|
{
|
||||||
|
return new Vector2(MaxX, MaxY);
|
||||||
|
}
|
||||||
|
else if (direction.X >= 0 && direction.Y < 0)
|
||||||
|
{
|
||||||
|
return new Vector2(MaxX, MinY);
|
||||||
|
}
|
||||||
|
else if (direction.X < 0 && direction.Y >= 0)
|
||||||
|
{
|
||||||
|
return new Vector2(MinX, MaxY);
|
||||||
|
}
|
||||||
|
else if (direction.X < 0 && direction.Y < 0)
|
||||||
|
{
|
||||||
|
return new Vector2(MinX, MinY);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Support vector direction cannot be zero.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Vector2 Support(Vector2 direction, Transform2D transform)
|
public Vector2 Support(Vector2 direction, Transform2D transform)
|
||||||
{
|
{
|
||||||
var maxDotProduct = float.NegativeInfinity;
|
Matrix4x4 inverseTransform;
|
||||||
var maxVertex = new Vector2(MinX, MinY);
|
Matrix4x4.Invert(transform.TransformMatrix, out inverseTransform);
|
||||||
foreach (var vertex in Vertices)
|
var inverseDirection = Vector2.TransformNormal(direction, inverseTransform);
|
||||||
{
|
return Vector2.Transform(Support(inverseDirection), transform.TransformMatrix);
|
||||||
var transformed = Vector2.Transform(vertex, transform.TransformMatrix);
|
|
||||||
var dot = Vector2.Dot(transformed, direction);
|
|
||||||
if (dot > maxDotProduct)
|
|
||||||
{
|
|
||||||
maxVertex = transformed;
|
|
||||||
maxDotProduct = dot;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return maxVertex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public AABB TransformedAABB(Transform2D transform)
|
public AABB TransformedAABB(Transform2D transform)
|
||||||
|
|
Loading…
Reference in New Issue