I've got an OBB implemented, and it works for calculating collisions. The only issue I'm having with it is drawing the debug lines, so they give a correct visual representation of the box bounded by it. Currently I'm calculating a 3 x 3 array based on a quaternion, saving it to a single dimension array of Vector3s and using that to figure out rotations.
The array at the identity position ends up looking like:
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]
I'm pretty sure my original matrix is correct, but I don't know how to use it for just updating my Debug lines indivisually.
A snippet for how I'm modifying the debug lines is this:
var xOrientation = orientationMatrix[0].x + orientationMatrix[1].x + orientationMatrix[2].x;
var yOrientation = orientationMatrix[0].y + orientationMatrix[1].y + orientationMatrix[2].y;
var zOrientation = orientationMatrix[0].z + orientationMatrix[1].z + orientationMatrix[2].z;
Debug.DrawLine(
new Vector3((center.x - HalfWidths.x) * xOrientation, (center.y + HalfWidths.y) * yOrientation,
(center.z + HalfWidths.z) * zOrientation),
new Vector3((center.x + HalfWidths.x) * xOrientation, (center.y + HalfWidths.y) * yOrientation,
(center.z + HalfWidths.z) * zOrientation), Color.magenta);
How should I multiply the center values and / or halfwidths to rotate the lines with the box? Would the MinAreaRect algorithm, discussed in Real Time Collision Detection accomplish the same thing? I'm not sure what to do with the float returned from MinAreaRect.
Here's the code in MinAreaRect.
// Compute the center point 'c' and axis orientation oM[0] and oM[1], of
// the minimum area rectanble to the xy plane containing the points pt[]
float MinAreaRect(Vector2[] point, int numPoints, ref Vector2 c, ref Vector2[] oM)
{
float minArea = float.MaxValue;
// Loop through all edges; j trails i by 1, modulo numpts
for (int i = 0, j = numPoints - 1; i < numPoints; j = i, i++)
{
// Get current edge e0 (e0x, e0y), normalized
Vector2 e0 = point[i] - point[j];
e0.Normalize();
// Get an axis e1 orthogonal to edge e0
Vector2 e1 = new Vector2(-e0.y, e0.x); // = Perp2D(e0)
// Loop through all point to get maximum extents
float min0 = 0.0f, min1 = 0.0f, max0 = 0.0f, max1 = 0.0f;
for (int k = 0; k < numPoints; k++)
{
// Project points onto axes e0 and e1 and keep track
// of minimum and maximum values along both axes
Vector2 distance = point[k] - point[j];
float dot = Vector2.Dot(distance, e0);
if (dot < min0)
min0 = dot;
if (dot > max0)
max0 = dot;
dot = Vector2.Dot(distance, e1);
if (dot < min1)
min1 = dot;
if (dot > max1)
max1 = dot;
}
float area = (max0 - min0)*(max1 - min1);
// if best so far, remember area, center, and axes
if (area < minArea)
{
minArea = area;
c = point[j] + 0.5f*((min0 + max0)*e0 + (min1 + max1)*e1);
oM[0] = e0;
oM[1] = e1;
}
}
return minArea;
}
I was mainly trying to get the debug lines working with rotations, before seeing how MinAreaRect should work with my OBB class.