# camera position/rotation

This topic is 2786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The code i'm using draw a cube of point sprites. What i'm trying to do is to be able to move like a spectator in a first person shooter game (like in counter strike source). Right now, i'm able to move, but something is wrong. I use w a s d to move forward, etc. Let's say i face the cube, if i use the mouse to look on my left, and then i click on forward, i won't move in the direction i'm facing. That's the real problem. Here the code located in void display() to move around in the 3D world (this code won't move in the direction i'm facing with the mouse, but will rotate correctly around where i'm in the space):

glRotatef(cameraAngleX, 1, 0, 0);
glRotatef(cameraAngleY, 0, 1, 0);
glTranslatef(-camX, -camY, -camZ);

The code below will make me move in the direction the mouse is facing, but the mouse will always rotate around the same point (always rotating around the origin, the first of 1000000 point sprites) :

glTranslatef(-camX, -camY, -camZ);
glRotatef(cameraAngleX, 1, 0, 0);
glRotatef(cameraAngleY, 0, 1, 0);

The code for the mouse and the keyboard :

void keyboardCB(unsigned char key, int x, int y)
{
switch(key)
{
case 27: // ESCAPE
exit(0);
break;
case 'w':
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
camX += float(sin(yrotrad)) * speed;
camZ -= float(cos(yrotrad)) * speed;
camY -= float(sin(xrotrad)) * speed;
break;
case 's':
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
camX -= float(sin(yrotrad)) * speed;
camZ += float(cos(yrotrad)) * speed;
camY += float(sin(xrotrad)) * speed;
break;
//Move Up/Down
case 'r':
camY += speed;
break;
case 'f':
camY -= speed;
break;

//Strafe Left/Right
case 'a':
yrotrad = (yrot / 180 * 3.141592654f);
camX -= float(cos(yrotrad)) * speed;
camZ -= float(sin(yrotrad)) * speed;
break;
case 'd':
yrotrad = (yrot / 180 * 3.141592654f);
camX += float(cos(yrotrad)) * speed;
camZ += float(sin(yrotrad)) * speed;
break;
default:
;
}

//glutPostRedisplay();
}

void mouseCB(int button, int state, int x, int y)
{
mouseX = x;
mouseY = y;

if(button == GLUT_LEFT_BUTTON)
{
if(state == GLUT_DOWN)
{
mouseLeftDown = true;
}
else if(state == GLUT_UP)
mouseLeftDown = false;
}

else if(button == GLUT_RIGHT_BUTTON)
{
if(state == GLUT_DOWN)
{
mouseRightDown = true;
}
else if(state == GLUT_UP)
mouseRightDown = false;
}
//glutPostRedisplay();
}

void mouseMotionCB(int x, int y)
{
if(mouseLeftDown)
{
cameraAngleY += (x-mouseX);
cameraAngleX += (y-mouseY);
mouseX = x;
mouseY = y;
}
if(mouseRightDown)
{
cameraDistance += (y - mouseY) * 0.2f;
mouseY = y;
}
//glutPostRedisplay();
}

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 17
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631758
• Total Posts
3002155
×