Camera that follows object + rotations

I am creating a 3D space shooter using OpenGL. I have a camera that is set to follow the player’s spaceship. I have managed to keep it at a certain distance and rotate together with the spaceship on the Y axis. See the code bellow for the camera :

float CalculateHorizontalDistance() {
	if (!mpPlayer) return 0.f;

	return distanceFromPlayer * glm::cos(glm::radians(-UpAngle));

float CalculateVerticalDistance() {
	if (!mpPlayer) return 0.f;

	return distanceFromPlayer * glm::sin(glm::radians(-UpAngle));

void calculateCameraPosition(float horizDistance, float verticDistance)
	if (!mpPlayer) return;

	float theta = mpPlayer->GetRotationY() + angleAroundPlayer;
	float offsetX = horizDistance * glm::sin(glm::radians(theta));
	float offsetZ = horizDistance * glm::cos(glm::radians(theta));

	Position.x = mpPlayer->GetPosition().x - offsetX;
	Position.y = mpPlayer->GetPosition().y + verticDistance;
	Position.z = mpPlayer->GetPosition().z - offsetZ;

Also the update and update vectors

void Update() {
	float horizDistance = CalculateHorizontalDistance();
	float vertDistance = CalculateVerticalDistance();
	calculateCameraPosition(horizDistance, vertDistance);

	if (mpPlayer)
		RightAngle = 180.f - (mpPlayer->GetRotationY() + angleAroundPlayer);


void updateCameraVectors()
	// Yaw
	glm::quat aroundY = glm::angleAxis(glm::radians(-RightAngle), glm::vec3(0, 1, 0));

	// Pitch
	glm::quat aroundX = glm::angleAxis(glm::radians(UpAngle), glm::vec3(1, 0, 0));

	// Roll
	glm::quat aroundZ = glm::angleAxis(glm::radians(RollAngle), glm::vec3(0, 0, 1));

	Orientation = aroundY * aroundX * aroundZ;

	glm::quat qF = Orientation * glm::quat(0, 0, 0, -1) * glm::conjugate(Orientation);
	Front = { qF.x, qF.y, qF.z };
	Right = glm::normalize(glm::cross(Front, WorldUp));
	Up = glm::normalize(glm::cross(Right, Front));


and how I calculate the view

glm::mat4 GetViewMatrix()
	// You should know the camera move reversely relative to the user input.
	// That's the point of Graphics Camera

	glm::quat reverseOrient = glm::conjugate(Orientation);
	glm::mat4 rot = glm::mat4_cast(reverseOrient);
	glm::mat4 translation = glm::translate(glm::mat4(1.0), -Position);

	return rot * translation;

Although I am near to what I want my camera to do, I am not there yet. I would like the Camera to follow and rotate together with the spaceship and actually follow it. I can not wrap my mind around on how to make it follow the rotations on X and Z axis. Any help on how to achieve this? Code or ideas are welcomed and I hope this is the correct group for these kind of problems.

hi paokakis
Yaw, pitch, roll sounds just right for a pilot, but for a camera? I’ve chosen to use two angles, hight above/below ‘ground’ and a geographic angle. It’ll also give you a better chance to visualize. They suffice to construct any direction and it’s fairly easy to convert between angles and matrix. I suppose that you’ll have to choose one arbitrary angle for one of the three to get the associate to others.
As for the quaternions, I’m no help.
I’m not really sure of your camera use.
There is an end to how difficult it is to get the modelView from the camera & model world-matrixes.
If camera has a fixed pose relative to ship, the camera-matrix is given by the ship-matrix … the camera-forward could be something like vec = -cross(ship-forward,ship-up) [looking perpendicular at the side of the ship] and the constant camera->ship-distance is using this direction too to get to cam-position = ship-position - distance * vec (probably normalizing somewhere). Then the camera-matrix should have the invers-transpose to become a proper modelView-matrix.
Have no clue of how you can translate this into quaternion-lingo …

looking back at your code I can see that I’ve made the assumption that you’ve converted all the player-input into a matrix that you need somewhere for drawing. When you have that matrix, you can eak out the unite-vectors and position from there … Once they are converted into the matrix you don’t have to keep track of them.

I think you may helped me see the problem in a different way when you said :

If camera has a fixed pose relative to ship, the camera-matrix is given by the ship-matrix

I will try in the afternoon to use the ship matrix for the camera matrix and update with my results…