GLM Velocity Vectors - 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/13
4:16 UTC
Read the original article
Hit count: 356
UPDATE - Code updated below but still need help adjusting my math.
I have a cube rendered on the screen which represents a car (or similar).
Using Projection/Model matrices and Glm I am able to move it back and fourth along the axes and rotate it left or right.
I'm having trouble with the vector mathematics to make the cube move forwards no matter which direction it's current orientation is. (ie. if I would like, if it's rotated right 30degrees, when it's move forwards, it travels along the 30degree angle on a new axes).
I hope I've explained that correctly.
This is what I've managed to do so far in terms of using glm to move the cube:
glm::vec3 vel; //velocity vector
void renderMovingCube(){
glUseProgram(movingCubeShader.handle());
GLuint matrixLoc4MovingCube = glGetUniformLocation(movingCubeShader.handle(), "ProjectionMatrix");
glUniformMatrix4fv(matrixLoc4MovingCube, 1, GL_FALSE, &ProjectionMatrix[0][0]);
glm::mat4 viewMatrixMovingCube;
viewMatrixMovingCube = glm::lookAt(camOrigin, camLookingAt, camNormalXYZ);
vel.x = cos(rotX); vel.y=sin(rotX);
vel*=moveCube;
//move cube
ModelViewMatrix = glm::translate(viewMatrixMovingCube,globalPos*vel);
//bring ground and cube to bottom of screen
ModelViewMatrix = glm::translate(ModelViewMatrix, glm::vec3(0,-48,0));
ModelViewMatrix = glm::rotate(ModelViewMatrix, rotX, glm::vec3(0,1,0)); //manually turn
glUniformMatrix4fv(glGetUniformLocation(movingCubeShader.handle(), "ModelViewMatrix"), 1, GL_FALSE, &ModelViewMatrix[0][0]); //pass matrix to shader
movingCube.render(); //draw
glUseProgram(0);
}
keyboard input:
void keyboard()
{
char BACKWARD = keys['S']; char FORWARD = keys['W'];
char ROT_LEFT = keys['A']; char ROT_RIGHT = keys['D'];
if (FORWARD) //W - move forwards
{
globalPos += vel;
//globalPos.z -= moveCube;
BACKWARD = false;
}
if (BACKWARD)//S - move backwards
{
globalPos.z += moveCube;
FORWARD = false;
}
if (ROT_LEFT)//A - turn left
{
rotX +=0.01f;
ROT_LEFT = false;
}
if (ROT_RIGHT)//D - turn right
{
rotX -=0.01f;
ROT_RIGHT = false;
}
Where am I going wrong with my vectors? I would like change the direction of the cube (which it does) but then move forwards in that direction.
© Game Development or respective owner