views:

348

answers:

2

I am presently trying to construct an OBB (Oriented Bounding Box) using the source and math contained in the book "Real Time Collision Detection".

One problem with the code contained in this book is that it does very little to explain what the parameters mean for the methods.

I am trying to figure out what I need to feed my setOBB() method (I wrote this one). It goes like this:

void PhysicalObject::setOBB( Ogre::Vector3 centrePoint, Ogre::Vector3 localAxes[3], Ogre::Vector3 positiveHalfwidthExtents )
{
    // Ogre::Vector3 c;      // OBB center point
    // Ogre::Vector3 u[3]; // Local x-, y-, and z-axes (rotation matrix)
    // Ogre::Vector3 e; // Positive halfwidth extents of OBB along each axis

    m_obb.c         = centrePoint;
    m_obb.u[0]      = localAxes[0];
    m_obb.u[1]      = localAxes[1];
    m_obb.u[2]      = localAxes[2];
    m_obb.e         = positiveHalfwidthExtents;
}

Looking at the parameters it wants above, the first and third parameters I believe I understand.

  1. Pass in the centre position of the object.
  2. This is my problem. I believe it wants a matrix represented using an array of 3 vectors? but how?
  3. A Vector which contains magnitude for the distance between the centre point and the edge of the OBB in each x,y,z direction.

Here is what I'm doing currently:

// Build the OBB
Ogre::Vector3 rotation[3];
Ogre::Vector3 centrePoint       = sphere->getPosition();
rotation[0]      = ?
rotation[1]      = ?
rotation[2]      = ?
Ogre::Vector3 halfEdgeLengths   = Ogre::Vector3( 1,1,1 );

myObject->setOBB( centrepoint, rotation, halfEdgeLengths );

How can i represent a matrix using three vectors (which I cannot avoid doing this way). Thanks.

+1  A: 

Ok. So another question is that this functionality requires it to be a matrix, right? I'm assuming so, or you'd just use the individual axes vectors in the places they are needed during the calculation.

That said, if you stuff these vectors into a matrix in a uniform way, then you'll either have them as the rows of the matrix or the columns of the matrix and, more or less, have a 50% chance to get it right depending on how the functions accepting the matrix expect the matrix to be formatted. So, set up the 2d array as the matrix and go forward with caution, I'd suggest. For example:

int matrix[3][3] = {     { x1, x2, x3 },
                         { y1, y2, y3 },
                         { z1, z2, z3 }
                   };

Flip if you need to go column-major.

Kyle Walsh
I guess a proper disclaimer here is that I'm just showing a way to stuff some vectors into a matrix. Not making guarantees about the specific domain.
Kyle Walsh
Oh i see what you mean. This does help me, I guess I will have to try either by rows or columns depending on the way it's representing them internally.
Brock Woolf
@Brock Yeah, exactly. Just make sure you are consistent with your orientation throughout and you should be fine. Good luck!
Kyle Walsh
What I am still confused about however is, what does each "vector" represent for the column or row? I supposedly represents "orientation" in the OBB, but I don't fully understand how a row in a matrix can represent "orientation"?
Brock Woolf
Simply put the first column vector is the new X-Axis. when [1 0 0] is the unrotated X-Axis, and the first column is [0 1 0], then you have a rotation of 90° around Z-Axis, because this will make the X-Axis stand like the Y-Axis. (Try it with your fingers to see why ;) )
haffax
+2  A: 

A 3x3 matrix representing a rotation/scale in 3d space is nothing more than three vectors in row. Each column vector is the rotated and scaled main axis. First column is the scaled and rotated x axis, second and third are y and z. (Ogre uses column major matrices)

So, localAxes[3] simply is a rotation. And you can get it from a Quaternion.

Ogre::Vector3 rotation[3];
Ogre::Quaternion orientation = sphere->getOrientation();
orientation.ToAxes(rotation);
// Now rotation has the three axes representing the orientation of the sphere.
haffax
Less wordy and more clear than what I had stated :)
Kyle Walsh
Well strictly speaking what you mean is that with no scaling, the 3x3 matrix rows/columns - columns in this case gives 3 orthonormal vectors such that x,y,z are orthogonal and are unit vectors ( this wouldn't work if you had a scale ). This gives you your local space and from that it's easy to construct your OBB
zebrabox