# Converting from view vector / center of interest to an orientation/quaternion

## Recommended Posts

VERY SHORT VERSION: Given a view vector and an up vector for a camera, how can I calculate the quaternion orientation of the camera? VERY LONG VERSION: ;) Hi, I've looked through this forum for a few hours and found many similar problems relating to cameras and quaternions, but not this particular problem, so forgive me if I overlooked a post. Anyway, I'm working on manipulating camera orientation (though the concepts would apply equally to any object) in a mock-up of a game engine* using OpenGL, and I want to have two options available for specifying the orientation: - Use a view vector and up vector when there's a particular center of interest (for when I want my camera to be locked onto an object). - Store the orientation as a quaternion when the camera is being aimed freely without any specific target For the sake of argument, let's say the camera is in first person mode and tied to a player object. Most of the time, the player just walks around and looks around, and the position & quaternion rotation of both the player and the camera change accordingly. When it comes time to make the gluLookAt call each frame, I can get the view vector by rotating (0, 0, -1) by the player's/camera's quaternion and the up vector by rotating (0, 1, 0) by the quaternion. Here's where the problem comes in: Then I decide to lock on to an object. It's easy enough to render the scene with a locked on camera - all I have to do when I call gluLookAt is to give it a view vector of (target position - camera position). However, assuming I want the player to face the target, I have NO IDEA how to specify the new rotation/quaternion. (Similarly, when I stop locking on, this keeps me from being able to start the camera off with an orientation equivalent to the last place it was facing when it was locked on). In short, I can convert a rotation to a view vector and an up vector just fine, but I have no clue how to get a quaternion from a view vector and an up vector (or how to get an axis-angle representation or even Euler angles, both of which I could convert to a quarternion - though I'd like to stay away from the Eulers if possible). Maybe the answer's something really simple that I'm overlooking, or maybe it isn't... Thanks in advance for any help! *That is to say, this isn't an engine for an actual game (yet...I guess it depends on how good I can make it :D) - rather, I'm doing this as a learning experience ;)

##### Share on other sites
One way to do this would be to get a matrix based on direction/up vectors, then convert the matrix to a quaternion. If your quaternion had a method that took dir/up vectors, it would probably do the matrix->quaternion math internally anyway, since the only math to get the matrix you want is a cross product.

##### Share on other sites
I haven't come across any shortcuts for creating a 'lookat' quaternion; that doesn't mean there aren't any, but the best suggestion I can make is to create a 'lookat' matrix, and then extract the quaternion from the matrix. How to do both of these things can be found online, but you do have to make sure to get the conventions right, or the results might not be what you expect. Anyway, ask if you need further help (or maybe someone will come along and post a 'lookat quaternion' shortcut).

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628318
• Total Posts
2982043

• 9
• 9
• 13
• 11
• 15