# The Industry's Foundation for High Performance Graphics

1. ## Rotating mess

Hi all!

I'm in the proces of programming a sort of flightsim game, in 2nd person perspective (ie the player's flying behind his plane. People who've played Independance Day or Lylat Wars (N64) will recognize this). Anyway, I've got this rotation problem. Whenever I rotate along, let's say the z-axis, things go fine. But when I rotate along the x-axis and then along the z-axis, my plane gets messed up. This is because rotation is performed along the axes of the screen, and not the axes of the plane.
This is the case when I use glRotatef, but this is also the case when I rotate manually (ie. with all the sine/cosine matrix-math-mess =).
Does anyone have a suggestion on how I can change this? Maybe some example code from a similar game (haven't found something like that yet)?

DragonLord

2. ## Re: Rotating mess

Sorry about the mass of code..... Hope this is a clear example

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

static int shoulder =0, elbow = 0, finger1=0, finger2=0, finger3=0, finger4=0, finger5=0;
GLfloat diffuseMaterial[4] = {0.5, 0.5, 0.5, 1.0};

void init(void)
{
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {25.0};
GLfloat light_position0[] = {1.0, 1.0, 1.0, 0.0};
GLfloat light_position1[] = {-1.0, -1.0, -1.0, 0.0};
glClearColor(0.5, 0.5, 0.5, 0.0);

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);

glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
glLightfv(GL_LIGHT1, GL_POSITION, light_position1);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}

void display(void)
{
GLfloat mat_ambient1[] = {1.0, 0.0, 0.0, 0.0};
GLfloat mat_ambient2[] = {1.0, 1.0, 0.0, 0.0};
GLfloat mat_ambient3[] = {0.0, 1.0, 0.0, 0.0};
GLfloat mat_ambient4[] = {0.0, 0.0, 1.0, 0.0};
GLfloat mat_ambient5[] = {0.0, 0.5, 0.5, 0.0};
GLfloat mat_ambient6[] = {0.5, 0.0, 0.5, 0.0};

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-1.0, 0.0, 0.0);
glRotatef((GLfloat) shoulder, 0.0, 0.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);
glPushMatrix();
glScalef(2.0, 0.4, 1.0);
glColor3f(1, 0, 0);
glutSolidCube(1.0);
glPopMatrix();

glTranslatef(1.0, 0.0, 0.0);
glRotatef((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);
glPushMatrix();
glScalef(2.0, 0.04, 1.0);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient1);
glColor3f(1.0, 0.0, 0.0);
glutSolidCube(1.0);
glPopMatrix();

glTranslatef(1.0, 0.0, 0.0); /* finger 1 */
glPushMatrix();
glRotatef((GLfloat) finger1, 0.0, 0.0, 1.0);
glTranslatef(0.15, 0.0, 0.425);
glPushMatrix();
glScalef(0.3, 0.04, 0.175);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient2);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient2);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient2);
glColor3f(1.0, 1.0, 0.0);
glutSolidCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(0.15, 0.0, 0.0);
glRotatef((GLfloat) finger1, 0.0, 0.0, 0.5);
glTranslatef(0.15, 0.0, 0.0);
glPushMatrix();
glScalef(0.28, 0.04, 0.175);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient2);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient2);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient2);
glColor3f(1.0, 1.0, 0.0);
glutSolidCube(1.0);
glPopMatrix();
glPopMatrix();
glPopMatrix();

glPushMatrix();
glRotatef((GLfloat) finger2, 0.0, 0.0, 1.0);
glTranslatef(0.2, 0.0, 0.21);
glPushMatrix();
glScalef(0.4, 0.04, 0.15);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient3);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient3);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient3);
glColor3f(0.0, 1.0, 0.0);
glutSolidCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(0.2, 0.0, 0.0);
glRotatef((GLfloat) finger2, 0.0, 0.0, 0.5);
glTranslatef(0.2, 0.0, 0.0);
glPushMatrix();
glScalef(0.38, 0.04, 0.15);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient3);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient3);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient3);
glColor3f(0.0, 1.0, 0.0);
glutSolidCube(1.0);
glPopMatrix();
glPopMatrix();
glPopMatrix();

glPushMatrix();
glRotatef((GLfloat) finger3, 0.0, 0.0, 1.0);
glTranslatef(0.3, 0.0, -0.0025);
glPushMatrix();
glScalef(0.6, 0.04, 0.15);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient4);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient4);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient4);
glColor3f(0.0, 0.0, 1.0);
glutSolidCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(0.3, 0.0, 0.0);
glRotatef((GLfloat) finger3, 0.0, 0.0, 0.5);
glTranslatef(0.3, 0.0, 0.0);
glPushMatrix();
glScalef(0.58, 0.04, 0.15);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient4);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient4);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient4);
glColor3f(0.0, 0.0, 1.0);
glutSolidCube(1.0);
glPopMatrix();
glPopMatrix();
glPopMatrix();

glPushMatrix();
glRotatef((GLfloat) finger4, 0.0, 0.0, 1.0);
glTranslatef(0.2, 0.0, -0.215);
glPushMatrix();
glScalef(0.4, 0.04, 0.15);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient5);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient5);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient5);
glColor3f(0.0, 0.5, 0.5);
glutSolidCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(0.2, 0.0, 0.0);
glRotatef((GLfloat) finger4, 0.0, 0.0, 0.5);
glTranslatef(0.2, 0.0, 0.0);
glPushMatrix();
glScalef(0.38, 0.04, 0.15);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient5);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient5);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient5);
glColor3f(0.0, 0.5, 0.5);
glutSolidCube(1.0);
glPopMatrix();
glPopMatrix();
glPopMatrix();

glPushMatrix();
glRotatef((GLfloat) finger5, 0.0, 0.0, 1.0);
glTranslatef(0.15, 0.0, -0.4275);
glPushMatrix();
glScalef(0.3, 0.04, 0.15);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient6);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient6);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient6);
glColor3f(0.5, 0.0, 0.5);
glutSolidCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(0.15, 0.0, 0.0);
glRotatef((GLfloat) finger5, 0.0, 0.0, 0.5);
glTranslatef(0.15, 0.0, 0.0);
glPushMatrix();
glScalef(0.28, 0.04, 0.15);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient6);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient6);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient6);
glColor3f(0.5, 0.0, 0.5);
glutSolidCube(1.0);
glPopMatrix();
glPopMatrix();
glPopMatrix();

glPopMatrix();
glutSwapBuffers();
glFlush();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
gluPerspective(100.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, -5.0);
}

void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 's': /*s kewy rotates at shoulder */
shoulder = (shoulder+5) % 360;
glutPostRedisplay();
break;
case 'S':
shoulder = (shoulder-5) % 360;
glutPostRedisplay();
break;
case 'e': /*e key rotates at elbow */
elbow = (elbow+5) % 360;
glutPostRedisplay();
break;
case 'E':
elbow = (elbow-5) % 360;
glutPostRedisplay();
break;
case '1': /*f key rotates at finger1 */
finger1 = (finger1+5) % 360;
glutPostRedisplay();
break;
case '!':
finger1 = (finger1-5) % 360;
glutPostRedisplay();
break;
case '2': /*f key rotates at finger2 */
finger2 = (finger2+5) % 360;
glutPostRedisplay();
break;
case '@':
finger2 = (finger2-5) % 360;
glutPostRedisplay();
break;
case '3': /*f key rotates at finger3 */
finger3 = (finger3+5) % 360;
glutPostRedisplay();
break;
case '#':
finger3 = (finger3-5) % 360;
glutPostRedisplay();
break;
case '4': /*f key rotates at finger4 */
finger4 = (finger4+5) % 360;
glutPostRedisplay();
break;
case '\$':
finger4 = (finger4-5) % 360;
glutPostRedisplay();
break;
case '5': /*f key rotates at finger4 */
finger5 = (finger5+5) % 360;
glutPostRedisplay();
break;
case '%':
finger5 = (finger5-5) % 360;
glutPostRedisplay();
break;
case 'x': /*f key rotates at finger4 */
glRotatef(5, 1.0, 0.0, 0.0);
glutPostRedisplay();
break;
case 'X':
glRotatef(-5, 1.0, 0.0, 0.0);
glutPostRedisplay();
break;
case 'y': /*f kefy rotates at finger4 */
glRotatef(5, 0.0, 1.0, 0.0);
glutPostRedisplay();
break;
case 'Y':
glRotatef(-5, 0.0, 1.0, 0.0);
glutPostRedisplay();
break;
case 'z': /*f key rotates at finger4 */
glRotatef(5, 0.0, 0.0, 1.0);
glutPostRedisplay();
break;
case 'Z':
glRotatef(-5, 0.0, 0.0, 1.0);
glutPostRedisplay();
break;
default:
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 800);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•