Sign in to follow this  
Irlan

Oriented Bounding Box (rotating about the central point)

Recommended Posts

Irlan    4067
The problem is: when rotating the OBB, it performs a rotation about the x axis instead of a rotation about the local axis (wich seems that i've already implemented this with quaternion). I think that probably is the drawing method. What i'm doing wrong? How do you guys perfoms render on a cube (GL_POLYGON, GL_CUBE, GL_LINELOOP...) ?
 
min = 0, 0, 0
max = 2, 2, 2
 
PS: when i draw a cube/sphere it's rotate normally
 
Problem: I need to rotate the OBB about the central point.
 
float angle;
        Vector axis;
 
        orientation.angleAxis(angle, axis);
        glTranslatef(position.x, position.y, position.z);
        glRotatef(angle / Mathematics::pi * 180.0f, axis.x, axis.y, axis.z);
 
        glBegin(GL_LINES);
        glColor3f(1.0f, 1.0f, 1.0f);
 
        //8 LINE SEGMENTS;
 
        Vector v0(min[0], min[1], min[2]);
        Vector v1(max[0], min[1], min[2]);
        Vector v2(max[0], max[1], min[2]);
        Vector v3(min[0], max[1], min[2]);
        Vector v4(min[0], min[1], max[2]);
        Vector v5(max[0], min[1], max[2]);
        Vector v6(max[0], max[1], max[2]);
        Vector v7(min[0], max[1], max[2]);
 
        glVertex3fv(&v0[0]); glVertex3fv(&v1[0]);
        glVertex3fv(&v1[0]); glVertex3fv(&v2[0]);
        glVertex3fv(&v2[0]); glVertex3fv(&v3[0]);
        glVertex3fv(&v3[0]); glVertex3fv(&v0[0]);
        glVertex3fv(&v4[0]); glVertex3fv(&v5[0]);
        glVertex3fv(&v5[0]); glVertex3fv(&v6[0]);
        glVertex3fv(&v6[0]); glVertex3fv(&v7[0]);
        glVertex3fv(&v7[0]); glVertex3fv(&v4[0]);
        glVertex3fv(&v0[0]); glVertex3fv(&v4[0]);
        glVertex3fv(&v1[0]); glVertex3fv(&v5[0]);
        glVertex3fv(&v2[0]); glVertex3fv(&v6[0]);
        glVertex3fv(&v3[0]); glVertex3fv(&v7[0]);
glEnd();
glPushMatrix();
 
Thanks guys!!!

Share this post


Link to post
Share on other sites
_Slin_    209
glTranslatef(position.x, position.y, position.z);
glRotatef(angle / Mathematics::pi * 180.0f, axis.x, axis.y, axis.z);
 
just do it the other way around:
glRotatef(angle / Mathematics::pi * 180.0f, axis.x, axis.y, axis.z);
glTranslatef(position.x, position.y, position.z);
 
Edit: The idea is, that your objects vertices have positions relative to your objects center. Without transformation, this center will be the same as the worlds center. glTranslate and glRotate always transform relative to the worlds center. This means if you first translate, your object will be placed at a different position in the world, now if you rotate, that new position+your vertex positions will be rotate around the worlds center. If you rotate first, your object is rotated around the worlds center, which is also its own center and then it is moved to its final position. This explanation is probably not mathematically perfect, but should give a basic feeling for correct transformation orders. If you want to understand this in more detail, check out rotation and translation matrices and matrix multiplication, especially homogeneous matrices.
Edited by _Slin_

Share this post


Link to post
Share on other sites
Irlan    4067

Now seems to REALLY rotate about the world center sad.png . I think the first try it's more correct. When I draw a cube like this:

 

glBegin(GL_POLYGON);
glColor3f( 1.0, 1.0, 1.0 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glEnd();

// Purple side - RIGHT
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glEnd();

// Green side - LEFT
glBegin(GL_POLYGON);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();

// Blue side - TOP
glBegin(GL_POLYGON);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glEnd();

// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();

 

Rotates perfectly. Maybe is a problem with max and min and I can't figure out. If someone know how to correctly draw a OBB...

Share this post


Link to post
Share on other sites
BornToCode    1185

glTranslate(x,y,z);glRotate(angle,x,y,z)  and Set up all your vertices as if your box is axigned aligned. If you are using Quaternion you can always convert your quaternion of your object local rotation to an axis and angle and feed that to glRotate.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this