Followers 0

# Orbit Quaternion Camera

## 2 posts in this topic

I'm trying to create a quaternion based camera to rotate around a given point in space. For that, I'm using spherical coordinates and  I've managed to position and calculate the look at vector correctly. However, the camera tilts and completely rolls over at a specific point, while always facing the target point.

The camera's parameters are:

T - the target position.

r - the distance from T.

theta - the pitch angle.

phi - the yaw angle.

The camera position P is easily calculated by spherical to cartesian coordinate conversion.

The view vector is, then, T-P.

How can I create a quaternion that won't tilt and still face the target position.

0

##### Share on other sites

That's exactly what I mean. Sorry for not being clear.

I have tried that but, for some reason, I wasn't successful. Here's my take on it.

glm::vec3 camPos(r*cos(yaw)*sin(pitch),
r*cos(pitch),
r*sin(yaw)*sin(pitch));

glm::vec3 viewVector = target - camPos;
glm::vec3 upVector(0,1,0);
glm::vec3 right = glm::cross(viewVector, upVector);
upVector = glm::cross(right, viewVector);
viewVector = glm::normalize(viewVector);
upVector = glm::normalize(upVector);
right = glm::normalize(right);

glm::quat rotation;
rotation.w = sqrtf(1 + right.x + upVector.y + viewVector.z) * 0.5f;
float w4_recip = 1 / (4*rotation.w);
rotation.x = (upVector.z - viewVector.y) * w4_recip;
rotation.y = (viewVector.x - right.z) * w4_recip;
rotation.z = (right.y - upVector.x) * w4_recip;


cam->SetPosition(camPos);

cam->SetRotation(rotation);

I have messed around with the camPos calculation and tried several different things.

Unfortunately, I'm not the best person at maths and most of the camera, transformation and rendering code is my own, and might as well have bugs. However it hasn't given me much problems until now. If this code is (mostly) correct, I have to search elsewhere for the problem.

0

## Create an account

Register a new account