Car-like Physics - Basic Maths to Simulate Steering

Posted by Reanimation on Game Development See other posts from Game Development or by Reanimation
Published on 2013-11-10T16:34:05Z Indexed on 2013/11/10 22:25 UTC
Read the original article Hit count: 344

Filed under:
|

As my program stands I have a cube which I can control using keyboard input. I can make it move left, right, up, down, back, fourth along the axis only. I can also rotate the cube either left or right; all the translations and rotations are implemented using glm.

if (keys[VK_LEFT])  //move cube along xAxis negative
{
    globalPos.x -= moveCube;
    keys[VK_RIGHT] = false;
}
if (keys[VK_RIGHT]) //move cube along xAxis positive
{
    globalPos.x += moveCube;
    keys[VK_LEFT] = false;
}
if (keys[VK_UP])    //move cube along yAxis positive
{
    globalPos.y += moveCube;
    keys[VK_DOWN] = false;
}
if (keys[VK_DOWN])  //move cube along yAxis negative
{
    globalPos.y -= moveCube;
    keys[VK_UP] = false;
}
if (FORWARD)        //W - move cube along zAxis positive
{
    globalPos.z += moveCube;
    BACKWARD = false;  
}
if (BACKWARD)       //S- move cube along zAxis negative
{
    globalPos.z -= moveCube;
    FORWARD = false;  
}
if (ROT_LEFT)       //rotate cube left
{
    rotX +=0.01f;
    ROT_LEFT = false;  
}
if (ROT_RIGHT)      //rotate cube right
{
    rotX -=0.01f;
    ROT_RIGHT = false; 
}

I render the cube using this function which handles the shader and position on screen:

void renderMovingCube(){
    glUseProgram(myShader.handle());
    GLuint matrixLoc4MovingCube = glGetUniformLocation(myShader.handle(), "ProjectionMatrix");  
    glUniformMatrix4fv(matrixLoc4MovingCube, 1, GL_FALSE, &ProjectionMatrix[0][0]);
        glm::mat4 viewMatrixMovingCube;
        viewMatrixMovingCube = glm::lookAt(camOrigin,camLookingAt,camNormalXYZ);
        ModelViewMatrix = glm::translate(viewMatrixMovingCube,globalPos);
        ModelViewMatrix = glm::rotate(ModelViewMatrix,rotX, glm::vec3(0,1,0)); //manually rotate
        glUniformMatrix4fv(glGetUniformLocation(myShader.handle(), "ModelViewMatrix"), 1, GL_FALSE, &ModelViewMatrix[0][0]);
    movingCube.render();
    glUseProgram(0);
}

The glm::lookAt function always points to the screens centre (0,0,0).

The globalPos is a glm::vec3 globalPos(0,0,0); so when the program executes, renders the cube in the centre of the screens viewing matrix; the keyboard inputs above adjust the globalPos of the moving cube.

The glm::rotate is the function used to rotate manually.

My question is, how can I make the cube go forwards depending on what direction the cube is facing.... ie, once I've rotated the cube a few degrees using glm, the forwards direction, relative to the cube, is no longer on the z-Axis... how can I store the forwards direction and then use that to navigate forwards no matter what way it is facing? (either using vectors that can be applied to my code or some handy maths). Thanks.

© Game Development or respective owner

Related posts about opengl

Related posts about glsl