Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving sideways using gluLookAt(); C++

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' :
            ??????????????????
like image 790
Karl Drog Avatar asked Aug 10 '13 21:08

Karl Drog


1 Answers

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.

like image 149
user1118321 Avatar answered Oct 10 '22 08:10

user1118321