rafvasq
rafvasq

Reputation: 1522

OpenGL: Moving around 3D Scene

I am trying to move a vehicle around the scene using arrow keys. I am able to rotate the vehicle around itself using left/right keys, but when I attempt to move forward/backward, it only follows a single axis no matter which way the face of the vehicle is facing.

glPushMatrix();

    // movement
    glTranslatef(movement, 0.0, 0.0);

    // turning
    glTranslatef(base, 0.0, 0.0);
    glRotatef(turnAngle, 0.0, 1.0, 0.0);
    glTranslatef(-base, 0.0, 0.0);

    drawCar();

glPopMatrix();

At the moment, I only increment or decrement movement on up/down keys. From what I understand, the reason my object moves on a single axis is because I'm only translating by movement on the x-axis. How do I track where the front of my vehicle is facing when turned and move 'forward' in that direction?

Upvotes: 1

Views: 742

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

First of all you should know that you use an old an deprecated way do things in OpenGL. See Khronos wiki - Fixed Function Pipeline and See Khronos wiki - Legacy OpenGL.


In a rendering, each mesh of the scene usually is transformed by the model matrix, the view matrix and the projection matrix. The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport. The view matrix describes the direction and position from which the scene is looked at. The model matrix defines the location, oriantation and the relative size of a mesh in the scene.
(See Transform the modelMatrix)

For the controlled movement of an object through the scene, the model matrix has to be incrementally changed. This means you have to calculate the current movement and current rotation matrix. Apply the movement and rotation to the model and keep the model for the next cycle of the loop. At the next cycle of the loop you have to use the manipulated model matrix from the previous cycle and you have to apply the new movement and rotation. This causes that the model incremental changes, always based on its current position and orientation.

enter image description here

To incrementally change the model matrix, you have to do the following:

In OpenGL there is one matrix stack for each matrix mode (See glMatrixMode). The matrix modes are GL_MODELVIEW, GL_PROJECTION, and GL_TEXTURE.
For the model matrix you need an variable where you can store it. The model_mat should be initialized with the initial position, the current matrix of the matrix stack can be get by glGetFloatv(GL_MODELVIEW_MATRIX, model_mat):

float model_mat[16];

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(base, 0.0, 0.0);
glGetFloatv(GL_MODELVIEW_MATRIX, model_mat);
glPopMatrix();

To incrementally change of the model matrix can be processed as follows:

float add_movment   = ....; 
flaot add_turnangle = ....;

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

// load the current position and orientation of the object
glLoadMatrixf(model_mat);

// add movment
glTranslatef(add_movment, 0.0, 0.0);

// add trun angle
glRotatef(add_turnangle, 0.0, 1.0, 0.0);

// get the new model matrix
glGetFloatv(GL_MODELVIEW_MATRIX, model_mat);

// draw the object
drawCar();

glPopMatrix();

Upvotes: 1

Related Questions