forked from MoonsideGames/MoonTools.Bonk
optimize rectangle support function
parent
f9df547302
commit
9e7d16b0a9
|
@ -38,21 +38,36 @@ namespace MoonTools.Core.Bonk
|
|||
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)
|
||||
{
|
||||
var maxDotProduct = float.NegativeInfinity;
|
||||
var maxVertex = new Vector2(MinX, MinY);
|
||||
foreach (var vertex in Vertices)
|
||||
{
|
||||
var transformed = Vector2.Transform(vertex, transform.TransformMatrix);
|
||||
var dot = Vector2.Dot(transformed, direction);
|
||||
if (dot > maxDotProduct)
|
||||
{
|
||||
maxVertex = transformed;
|
||||
maxDotProduct = dot;
|
||||
}
|
||||
}
|
||||
return maxVertex;
|
||||
Matrix4x4 inverseTransform;
|
||||
Matrix4x4.Invert(transform.TransformMatrix, out inverseTransform);
|
||||
var inverseDirection = Vector2.TransformNormal(direction, inverseTransform);
|
||||
return Vector2.Transform(Support(inverseDirection), transform.TransformMatrix);
|
||||
}
|
||||
|
||||
public AABB TransformedAABB(Transform2D transform)
|
||||
|
|
Loading…
Reference in New Issue