Jump to content

  • Log In with Google      Sign In   
  • Create Account

Orbit Quaternion Camera


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 _diego   Members   -  Reputation: 184

Like
0Likes
Like

Posted 17 April 2014 - 11:21 AM

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. 

 

Thanks in advance



Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13317

Like
0Likes
Like

Posted 17 April 2014 - 11:32 AM

By "not tilt" you mean that you want to keep a consistent notion of "up"? A quick search pointed me to this thread: http://www.gamedev.net/topic/613595-quaternion-lookrotationlookat-up/



#3 _diego   Members   -  Reputation: 184

Like
0Likes
Like

Posted 17 April 2014 - 03:16 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS