Advertisement

How to rotate without glRotate() ?

Started by May 01, 2002 04:31 AM
26 comments, last by ChacaL 22 years, 9 months ago
Can''t you just use binding boxes to detect colision. like in quake.
After checking the bounding boxes I still have to check that specific object''s vertices, otherwise, I''ll collide with spinning shapes... middle of doors and so.

Any idea?
Advertisement
The use bounding spheres. They''re always the same size no matter what rotation. Just do this:

R1 = Radius bounding sphere object1
R2 = same for object2
V1 = vector to center position object1
V2 = same for object2

bool Collide(R1, R2, V1, V2)
{
If distance(V1, V2)>(R1+R2)
return TRUE;

return FALSE;
}

- An eye for an eye will make the world go blind -

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

I have thought of a much easier way of doing your rotations within the collision function:

Rotate the colliding vector, not the object. To rotate the object meand to rotate all the vertexes. To rotate the vector that should be checked means only rotating 2 vertexes. Just rotate it opposite to the objects rotation in the world. Pass the objects vertex list to the collision function and check it against the rotated collision vector. Voila!

- An eye for an eye will make the world go blind -

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

I NEED to know the resulting values of a rotate vertice.

PLEASE, anyone know how?

Thanks!
I NEED to know the resulting values of a rotate vertice.

PLEASE, anyone know how?

Thanks!
Advertisement
In the appendix of the red book (although I''m not sure about the online version) it tells you the matrix created by glRotate.
Create that matrix yourself, then simply multibly tour vertex position by it.
try this:
http://www.makegames.com/3drotation/

this one is good:
http://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/3drota.htm
Alternatively you can let OpenGL generate a matrix for you and then transform a few vertices using it.

glGetFloatv(GL_MODELVIEW_MATRIX, matrix) will get the curent MODELVIEW matrix and copy it to matrix (declared
GLfloat matrix[16]).

Then you can use something similar to the vertex_trans function in this sample code to transorm a vertex acording to the matrix (it's a matrix multiplication).


    /* To compile this you need GLUT. Use the cursor keys, Z, z and a * to move the cube. The important stuff is in vertex_trans * and Display */#include <stdio.h>#include <stdlib.h>#include <math.h>#include <GL/glut.h>static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;static GLboolean Anim = GL_FALSE;static GLint ViewportWidth, ViewportHeight;static void Idle( void ){   Xrot += 3.0;   Yrot += 4.0;   Zrot += 2.0;   glutPostRedisplay();}static void vertex_trans(GLfloat r[3], const GLfloat v[3], const GLfloat m[16]){   r[0] = m[0]*v[0] + m[4]*v[1] + m[8]*v[2]  + m[12];   r[1] = m[1]*v[0] + m[5]*v[1] + m[9]*v[2]  + m[13];   r[2] = m[2]*v[0] + m[6]*v[1] + m[10]*v[2] + m[14];   r[3] = m[3]*v[0] + m[7]*v[1] + m[11]*v[2] + m[15];}static void Display( void ){   /* A cube vertices (and a lot of typing) */   static GLfloat cube[24][3] = {   {1.0f, 1.0f,-1.0f},                                    {-1.0f, 1.0f,-1.0f},                                    {-1.0f, 1.0f, 1.0f},                                    {1.0f, 1.0f, 1.0f},                                    {1.0f,-1.0f, 1.0f},                                    {-1.0f,-1.0f, 1.0f},                                    {-1.0f,-1.0f,-1.0f},                                    {1.0f,-1.0f,-1.0f},                                    {1.0f, 1.0f, 1.0f},                                    {-1.0f, 1.0f, 1.0f},                                    {-1.0f,-1.0f, 1.0f},                                    {1.0f,-1.0f, 1.0f},                                    {1.0f,-1.0f,-1.0f},                                    {-1.0f,-1.0f,-1.0f},                                    {-1.0f, 1.0f,-1.0f},                                    {1.0f, 1.0f,-1.0f},                                    {-1.0f, 1.0f, 1.0f},                                    {-1.0f, 1.0f,-1.0f},                                    {-1.0f,-1.0f,-1.0f},                                    {-1.0f,-1.0f, 1.0f},                                    {1.0f, 1.0f,-1.0f},                                    {1.0f, 1.0f, 1.0f},                                    {1.0f,-1.0f, 1.0f},                                    {1.0f,-1.0f,-1.0f}   };   GLfloat matrix[16];   GLfloat v[4];   GLint vertex;   glClear( GL_COLOR_BUFFER_BIT );   /* Use GL to set a new MODELVIEW matrix */   glLoadIdentity();   glTranslatef( 0.0, 0.0, -15.0 );   glRotatef(Xrot, 1, 0, 0);   glRotatef(Yrot, 0, 1, 0);   glRotatef(Zrot, 0, 0, 1);   /* Get the new MODELVIEW matrix */   glGetFloatv(GL_MODELVIEW_MATRIX, matrix);   /* Restore the matrix to the identity matrix */   glLoadIdentity();   /* Transform vertices using the previous matrix and draw them */   glBegin(GL_QUADS);      for (vertex=0; vertex<24; vertex++)         {            vertex_trans(v, cube[vertex], matrix);            glVertex3f(v[0], v[1], v[2]);         }   glEnd();   glutSwapBuffers();}static void Reshape( int width, int height ){   ViewportWidth = width;   ViewportHeight = height;      glViewport( 0, 0, width, height );   glMatrixMode( GL_PROJECTION );   glLoadIdentity();   glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );   glMatrixMode( GL_MODELVIEW );}static void Key( unsigned char key, int x, int y ){   const GLfloat step = 3.0;   (void) x;   (void) y;   switch (key) {      case 'a':         Anim = !Anim;         if (Anim)            glutIdleFunc(Idle);         else            glutIdleFunc(NULL);         break;      case 'z':         Zrot -= step;         break;      case 'Z':         Zrot += step;         break;      case 27:         exit(0);         break;   }   glutPostRedisplay();}static void SpecialKey( int key, int x, int y ){   const GLfloat step = 3.0;   (void) x;   (void) y;   switch (key) {      case GLUT_KEY_UP:         Xrot -= step;         break;      case GLUT_KEY_DOWN:         Xrot += step;         break;      case GLUT_KEY_LEFT:         Yrot -= step;         break;      case GLUT_KEY_RIGHT:         Yrot += step;         break;   }   glutPostRedisplay();}static void Init( void ){   /* setup lighting, etc */   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);   glShadeModel(GL_FLAT);   glDisable(GL_LIGHTING);   glDisable(GL_DEPTH_TEST);}int main( int argc, char *argv[] ){   glutInit( &argc, argv );   glutInitWindowPosition( 0, 0 );   glutInitWindowSize( 400, 400 );   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );   glutCreateWindow(argv[0]);   glutReshapeFunc( Reshape );   glutKeyboardFunc( Key );   glutSpecialFunc( SpecialKey );   glutDisplayFunc( Display );   Init();   glutMainLoop();   return 0;}    


I know, half of the code is grey. This is a
problem with the forum.

[edited by - __ALex_J_ on May 8, 2002 4:34:06 PM]
Couldn''t you just take the poly''s verticies and crank them thru the rotation matricies by hand?

This topic is closed to new replies.

Advertisement