# problem with translation after rotation camera

Hi gays, I’m coding a simple scene where there’s some object and I detect the collisions with them.
I’m implemented a very simple rotation of the camera (I rotate the whole scene instead :)) but when I restart to move forward or back after the rotation I always go in the direction of the point of view of glulookat();
can you tell me a way to store the direction that I’ve obtained after the rotation of the scene, this way I can move everywhere in the scene

PS sorry for my english I’m italian!

Your statement above is not clear: rotating the camera or the models in scene?

1. Euler matrix multiplication
2. Quaternion.

The latter approach is smooth and is free from defects such as gimbel locking.

I cannot explain my ideas realy weel in english…
What a wanted say is :
I try to rotate the camera but I realize that it was easier rotate the whole scene around the point of view … atleast for me!

It is exactly the same.

yes I know I’m asking you How can I after the rotation of the whole scene move the camera forward or back in the direction of the point of view.

I rotate the scene in this way

``````
glTranslatef(eye.getX(), eye.getY(), eye.getZ());
glRotatef(anglex, 0, 1, 0);
glTranslatef(-eye.getX(), -eye.getY(), -eye.getZ());

``````

after this the scene is rotated but what if I want to move forward or backward?

Are you really asking why this code doesn’t translate the scene contents?

Look, you translate with -eye, then you rotate and translate with +eye. Therefore you invert the translation. I suggest you have a look at (pun intended) gluLookAt.

sorry but the code above is correct! and it works for me.
is indented in a glPushMatrix(); glPopMatrix();
and so is intended upside down…and it for me works.

I’ll put here the cose of my keybord function and of my display func:

``````void keyboard (unsigned char key, int x, int y){
switch (key) {
case ' ':
transl = true;
break;
case 'm':
start_move = true;
break;
case 'z':
start_move = false;
break;
case 's'://muoviti in indietro
if(!detectCollision() || collision_forward == true){
collision_back = false;
eye.set(eye.getX() + (sin(anglex/57) * 5), 5, eye.getZ() + (cos(anglex/57) * 5));
}
else
collision_back = true;
break;
case 'w'://muoviti avanti
if(!detectCollision() || collision_back == true){
collision_forward = false;
eye.set(eye.getX() - (sin(anglex/57) * 5), 5, eye.getZ() - (cos(anglex/57) * 5));
}
else
collision_forward = true;
break;
case 'd'://ruota a sinistra
anglex = ( anglex + 5 ) % 360;
break;
case 'a'://ruota a destra
anglex = ( anglex - 5 ) % 360;
break;
case 'p':
ambient[0] = ambient[0] + 0.01;
ambient[1] = ambient[1] + 0.01;
ambient[2] = ambient[2] + 0.01;
break;
case 'l':
ambient[0] = ambient[0] - 0.01;
ambient[1] = ambient[1] - 0.01;
ambient[2] = ambient[2] - 0.01;
break;
case 'o':
diffuse[0] = diffuse[0] + 0.01;
diffuse[1] = diffuse[1] + 0.01;
diffuse[2] = diffuse[2] + 0.01;
break;
case 'k':
diffuse[0] = diffuse[0] - 0.01;
diffuse[1] = diffuse[1] - 0.01;
diffuse[2] = diffuse[2] - 0.01;
break;
case 'i':
specular[0] = specular[0] + 0.01;
specular[1] = specular[1] + 0.01;
specular[2] = specular[2] + 0.01;
break;
case 'j':
specular[0] = specular[0] - 0.01;
specular[1] = specular[1] - 0.01;
specular[2] = specular[2] - 0.01;
break;
case 'n':
modalita_navigatore = !modalita_navigatore;
if(modalita_navigatore){
saved_eye.set(eye.getX(), eye.getY(), eye.getZ());
saved_focus.set(focus.getX(), focus.getY(), focus.getZ());
}
else{
eye.set(saved_eye.getX(), saved_eye.getY(), saved_eye.getZ());
focus.set(saved_focus.getX(), saved_focus.getY(), saved_focus.getZ());
}
break;
case 27:
exit(0);
}

glMatrixMode(GL_MODELVIEW);
gluLookAt(eye.getX(), eye.getY(), eye.getZ(), focus.getX(), focus.getY(), focus.getZ(), normal.getX(), normal.getY(), normal.getZ());
glutPostRedisplay();
}

/**
* disegna tutti i componenti della scena.
*/
void disp()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 1.0);
// Create light components
GLfloat ambientLight[] = { ambient[0], ambient[1], ambient[2], ambient[3] };
GLfloat diffuseLight[] = { diffuse[0], diffuse[1], diffuse[2], diffuse[3] };
GLfloat specularLight[] = { specular[0], specular[1], specular[2], specular[3] };

// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);

glPushMatrix();
//se vuoi muovere la telecamera a destra o sinistra
//allora aggiungi queste trasformazioni
if(!modalita_navigatore){
glTranslatef(eye.getX(), eye.getY(), eye.getZ());
glRotatef(anglex, 0, 1, 0);
//lo sposto con il centro in zero e applico le 3 rotazioni
glTranslatef(-eye.getX(), -eye.getY(), -eye.getZ());
}
if(modalita_navigatore){
//lo rimetto nella posizione di partenza
glTranslatef( ((num_square * size)/2), (num_square*size)/2, (num_square*size)/2);
glRotatef(anglex, 1, 0, 0);
glRotatef(angley, 0, 1, 0);
glRotatef(anglez, 0, 0, 1);
//lo sposto con il centro in zero e applico le 3 rotazioni
glTranslatef(- ((num_square * size)/2), - (num_square*size)/2, - (num_square*size)/2);
}
//crea la luce e la posiziona
glPushMatrix();

drawLight();
//disabilita la luce nell'oggetto
glDisable (GL_LIGHTING);
//disegna l'oggetto pieno
glColor3f (1.0, 1.0, 0.0);
glutSolidSphere(0.8, 48, 48);
//ritorna alla modalità wired
//riabilita la luce
glEnable (GL_LIGHTING);

glPopMatrix();
//crea una sfera rossa al centro
float mcolor[] = { 1.0f, 0.0f, 0.0f, 0.25f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mcolor);
glutSolidSphere(10, 50, 50);
//crea un'altra sfera più a sinistra
glPushMatrix();
glTranslatef(-100, 10, 0);
float rcolor[] = { 1.0f, 0.0f, 1.0f, 0.25f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rcolor);
glutSolidSphere(10, 50, 50);
glPopMatrix();
//crea un cubo con di sopra un cono
glPushMatrix();
//crea cono
glPushMatrix();
glTranslatef(100, 50, 0);
glRotatef(-90, 1, 0, 0);
float zcolor[] = { 1.0f, 1.0f, 0.0f, 0.25f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, zcolor);
glutSolidCone(12, 22, 34, 34);
glPopMatrix();
//crea cubo
glTranslatef(100, 25, 0);
float ccolor[] = { 0.0f, 1.0f, 0.0f, 0.25f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, ccolor);
glutSolidCube(50);
glPopMatrix();
//crea il terreno su cui disporre gli oggetti
drawFloor();
glPopMatrix();

glutSwapBuffers();
}
``````

What you need is a direction vector to calculate your next camera position. The computed camera position will be supplied to your glTranslatef(). i.e some form of linear interpolation to evaluate next position for your camera.

P1 = P0 + lamda * V
Where P0 is your current camera position. P1 is your evaluated next position. lambda is control parameter and V is your direction vector computed as the difference of your current camera position and the position your camera is directed at.