I'm developing a 3D maze-like game, just for learning(and of course for fun :) ). I have made the maze, I can move between the walls in First-Person mode. My only problem is, that I want some kind of weapon for my First-Person view( like an FPS game). To moving in the maze I'm using gluLookAt
.
Code snippets:
void RenderScene(void)
{
glLoadIdentity();
gluLookAt(x, 1.0f, z,x + lx, 1.0f, z + lz,0.0f, 1.0f, 0.0f);
....
glBindTexture(GL_TEXTURE_2D, texture[0]); //texture binding
glScalef(7.0f, 8.0f, 7.0f);
glTranslatef(-(r * 2), 0.0f, -(c * 2)); //place the maze walls(cubes)
glCallList(mazeListId);//using the display list
}
void SpecialKeys(int key, int xx, int yy)
{
// ...
int state;
float fraction = 1.0f;
switch (key) {
case GLUT_KEY_LEFT:
angle -= 0.15f;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_RIGHT:
angle += 0.15f;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_UP:
x += lx * fraction;
z += lz * fraction;
break;
case GLUT_KEY_DOWN:
x -= lx * fraction;
z -= lz * fraction;
break;
}
I've tried to do this with my cube( the cube is now the "weapon").
glPushMatrix();
glTranslatef(0.0f, 0.0f, 5.0f);
glTranslatef(x + lx, -0.5f, z + lz);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
drawCube(2);
glPopMatrix();
With this the cube moves forward és backward perfectly, but when I turn left or right, it stays at its position. Can somebody help me with the turning?
Finally, I solved the problem! I didn't made the right order with the transformations. I share my code, if someone will need it in the future.
gluLookAt(x, 1.0f, z,
x + lx, 1.0f, z + lz,
0.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(x + lx, -0.5f, z + lz);
glRotatef(-angle* 57.2957795, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, 5.0f); // offset
drawCube(2);
glPopMatrix();