I'm trying to do a simple task using "gluLookAt()" in openGl C++: a "camera-based movement". What I want is allow cam's move through keys on the following scheme:
Directional keys:
← = Turn left
→ = Turn right
↑ = Look upward
↓ = Look downward
W, S, A, D
W = Move forward
S = Move backward
A = Move left (sideways)
D = Move right (sideways)
Q, E
Q = Left spin
E = Right spin
Until now I managed to work all types of movement I need, except the sideways using keys "A" and "D". It doesn't matter how much I try, when I press one of those keys the cam never move the way I want it to move. Someone told me that I need to perform some type of "cross product", but, despite some trials about it, I'm really lost.
//variables
float x1 = 0.0f, x2 = 0.0f, x3 = 0.0f, y1 = 1.0f, y2 = 1.0f, y3 = 1.0f, z1 = 5.0f, z2 = -1.0f, z3 = 0.0f;
//the camera
gluLookAt( x1, y1, z1, x1+x2, y2, z1+z2, x3, y3, z3);
//methods
void special(int key, int xx, int yy)
{
switch (key) {
case GLUT_KEY_LEFT :
camAngle -= 0.09f;
x2 = sin(camAngle);
z2 = -cos(camAngle);
break;
case GLUT_KEY_RIGHT :
camAngle += 0.09f;
x2 = sin(camAngle);
z2 = -cos(camAngle);
break;
///////////////////////////////////////////////////////
void normal(unsigned char key, int x, int y)
{
float part = 0.9f;
switch (key) {
case 'w' :
x1 += x2 * part ;
z1 += z2 * part ;
break;
case 's' :
x1 -= x2 * part ;
z1 -= z2 * part ;
break;
case 'a' :
??????????????????
break;
case 'd' :
??????????????????
It's unclear where all of the things you have above are happening since you didn't post all of your code and it's cut off in weird places. But if you want to move the camera left and right, it should be fairly easy. The definition of gluLookAt()
is:
gluLookAt (cameraX, cameraY, cameraZ, lookAtX, lookAtY, lookAtZ, upX, upY, upZ);
Moving the camera left and right without changing its angle is called "trucking", and sometimes 1st person shooters refer to it as the character strafing left or right. To do it, you need to move the camera and the look-at point by the same amount, like this:
gluLookAt (cameraX + deltaX, cameraY + deltaY, cameraZ + deltaZ, lookAtX + deltaX, lookAtY + deltaY, lookAtZ + deltaZ, upX, upY, upZ);
For left and right, deltaX will like be + or - some amount, and deltaY and deltaZ will probably be 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With