PDA

View Full Version : Model follwing camera transformations



TheSpartan100
01-11-2018, 07:06 AM
Hi,

I'm trying to make the camera follow the car and the camera translation and rotation works fine, but the car transformations don't quite work. If i translate and then rotate the car, the translations following a rotation don't apply relative to the car's position, they apply relative to the origin. This is the code i have for the car rendering :


model = glm::mat4(1.0f);
model = glm::scale(model, glm::vec3(0.5f, 0.5, 0.5f));

model = glm::translate(model, modelSpeed);
model = glm::rotate(model, glm::radians(modelAngle), glm::vec3(0, 1, 0));

//send model matrix data to shader
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

//create normal matrix
normalMatrix = glm::mat3(glm::inverseTranspose(view*model));
//send normal matrix data to shader
glUniformMatrix3fv(normalMatrixLoc, 1, GL_FALSE, glm::value_ptr(normalMatrix));

car.Draw(myCustomShader);

Do you guys know a way to make the car follow the camera whichever way I'm looking? Thanks a lot.

Jippie
01-12-2018, 04:01 AM
Matrix multiplication (transformations) are not commutative. This means that the order of multiplication (which is what you're doing in your code) has a effect on the final result.

Either way, if your rotate around the origin instead of the car itself, then it means that the model has already been translated away from the origin before the rotation was applied.

Try to reverse the order of multiplication - rotate before you translate.

It's just an educated guess, but I hope this helps

Best regards,

Jip