There are loads of questions (all of which I have looked at) on here about cameras following objects. Using them, I have successfully got my camera to follow my car when driving, but I am unable to make the camera swing right when the car turns left (so it stays directly behind it) and vice-versa.
So far, this is how I've got my camera following my object:
camOrigin.x = (0 +globalPos.x+velocity.x); //Camera Position camOrigin.y = (0 +globalPos.y+velocity.y); camOrigin.z = (100 +globalPos.z+velocity.z); camLookingAt.x = (0 +globalPos.x+velocity.x); //Camera Looking at Object camLookingAt.y = (0 +globalPos.y+velocity.y); camLookingAt.z = (0 +globalPos.z+velocity.z); camNormalXYZ.x = (0); //Camera Normal = Y camNormalXYZ.y = (1); camNormalXYZ.z = (0);
I have tried to use the same algebra I've used to calculate the cars direction and velocity because I figured the angle the camera moves would be the inverted angle the camera needs to move, but it didn't work.
camOrigin.x = (0 +globalPos.x+velocity.x-sin(rotX_rad)); //Camera Position camOrigin.y = (0 +globalPos.y+velocity.y); camOrigin.z = (100 +globalPos.z+velocity.z-cos(rotX_rad));
I'm unsure how to take this further.
Edit: Little drawing to help describe the movement. I'm looking to move the camera from c to d when the car turns in the left direction (and vice-versa when turning right).
rotX_rad = rotX*PI/180; //convert to radians velocity.x = sin(rotX_rad); //calculate X coord velocity.z = cos(rotX_rad); //calculate Z coord //globalPos + velocity; globalPos.x + velocity.x*delta; globalPos.z + velocity.z*delta; ModelViewMatrix = glm::translate(viewMatrixMovingCube,globalPos); //move/update car ModelViewMatrix = glm::translate(ModelViewMatrix, glm::vec3(0,-48,0)); //position low on screen ModelViewMatrix = glm::rotate(ModelViewMatrix, rotX, glm::vec3(0,1,0)); //manually turn/control rotation of car