public static void ComputeOBB(out OBB obb, Vec2[] vs, int count)
{
obb = new OBB();
Box2DXDebug.Assert(count <= Settings.MaxPolygonVertices);
Vec2[] p = new Vec2[Settings.MaxPolygonVertices + 1];
for (int i = 0; i < count; ++i)
{
p[i] = vs[i];
}
p[count] = p[0];
float minArea = Common.Settings.FLT_MAX;
for (int i = 1; i <= count; ++i)
{
Vec2 root = p[i - 1];
Vec2 ux = p[i] - root;
float length = ux.Normalize();
Box2DXDebug.Assert(length > Common.Settings.FLT_EPSILON);
Vec2 uy = new Vec2(-ux.Y, ux.X);
Vec2 lower = new Vec2(Common.Settings.FLT_MAX, Common.Settings.FLT_MAX);
Vec2 upper = new Vec2(-Common.Settings.FLT_MAX, -Common.Settings.FLT_MAX);
for (int j = 0; j < count; ++j)
{
Vec2 d = p[j] - root;
Vec2 r = new Vec2();
r.X = Vec2.Dot(ux, d);
r.Y = Vec2.Dot(uy, d);
lower = Common.Math.Min(lower, r);
upper = Common.Math.Max(upper, r);
}
float area = (upper.X - lower.X) * (upper.Y - lower.Y);
if (area < 0.95f * minArea)
{
minArea = area;
obb.R.Col1 = ux;
obb.R.Col2 = uy;
Vec2 center = 0.5f * (lower + upper);
obb.Center = root + Common.Math.Mul(obb.R, center);
obb.Extents = 0.5f * (upper - lower);
}
}
Box2DXDebug.Assert(minArea < Common.Settings.FLT_MAX);
}
}