PDA

View Full Version : How to update the forward movement in OpenGL



drew_jewel
06-20-2015, 09:44 AM
Basically I would like to move in the direction pointed by the cursor, but this doesn't happen and instead the "forward" direction remains the same it was at the beginning.

For example if I turn 180 and press "w" to walk forward, I walk backwards instead of going where I'm looking at. I'm talking about walking because I'm trying to replicate a first-person camera.

Down here you can see the code I've written so far:



// angle of rotation for the camera direction
float angle = 0.0f;

void mouseMove(int x, int y) {
// this will only be true when the left button is down
if (xOrigin >= 0) {

// update deltaAngle
deltaAngle = (x - xOrigin) * 0.001f;

// update camera's direction
lx = sin(angle + deltaAngle)*2;
lz = -cos(angle + deltaAngle)*2;
}
}

void mouseButton(int button, int state, int x, int y) {
// only start motion if the left button is pressed
if (button == GLUT_LEFT_BUTTON) {
// when the button is released
if (state == GLUT_UP) {
angle += deltaAngle;
xOrigin = -1;
}
else {// state = GLUT_DOWN
xOrigin = x;
}
}
}

void handleKeys(unsigned char key, int x, int y){
switch (key)
{
case 'w':
posZ+=4;
cout << "FORWARD\n";
break;
case 's':
posZ-=4;
cout << "BACK\n";
break;
case 'd':
posX-=4;
cout << "RIGHT\n";
break;
case 'a':
posX+=4;
cout << "LEFT\n";
break;
case 27:
exit(0);
break;
}
}

void computePos(float deltaMove) {
x += deltaMove * lx * 0.1f;
z += deltaMove * lz * 0.1f;
}

//draws the 3D sccene
void drawScene(){
cout << "ANGLE: "<<angle<<"\n";
if (deltaMove){
computePos(deltaMove);
}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set the camera
gluLookAt(x, 1.0f, z,
x + lx, 1.0f, z + lz,
0.0f, 1.0f, 0.0f);

glTranslatef(posX, 0.0f, posZ);

glColor3f(1.0f, 1.0f, 0.0f); //we set the cube color

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glBegin(GL_QUADS);

//Front
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-40.5f, -40.0f, 40.5f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(40.5f, -40.0f, 40.5f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(40.5f, 40.0f, 40.5f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-40.5f, 40.0f, 40.5f);

//Right
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(40.5f, -40.0f, -80.5f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(40.5f, 40.0f, -80.5f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(40.5f, 40.0f, 40.5f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(40.5f, -40.0f, 40.5f);

//Back
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-40.5f, -40.0f, -80.5f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-40.5f, 40.0f, -80.5f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(40.5f, 40.0f, -80.5f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(40.5f, -40.0f, -80.5f);

//Left
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-40.5f, -40.0f, -80.5f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-40.5f, -40.0f, 40.5f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-40.5f, 40.0f, 40.5f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-40.5f, 40.0f, -80.5f);

glEnd();

glutSwapBuffers();
}

fiodis
06-24-2015, 11:45 AM
Your "go forward" code

case 'w':
posZ+=4;
cout << "FORWARD\n";
break;
This works fine assuming positive Z is the forward direction. When you turn, the forward direction is pointing somewhere else, but the code is still incrementing just the Z component of the position. In reality, the position is a three dimensional vector and you want something like

case 'w':
pos += directionVector*timeStep;
cout << "FORWARD\n";
break;
Where directionVector is the forward direction that you update every time you turn. (And timeStep is related to the time between frames, so that you go forward at a consistent speed even if your framerate drops.)

Take a look at this tutorial (http://www.opengl-tutorial.org/beginners-tutorials/tutorial-6-keyboard-and-mouse/), it's what helped me get on the right track with making a camera.