# Easy camera positioning to quaternion.

Hi All,

We are currently using the following data to setup viewing:

1. 3D target point
2. Quaternion rotation
3. Distance of the camera from the 3D target point

As you can guess it is very difficult for an end user to position the camera using quaternions.

We know quaternion can be created by euler angles but even these are not so easy to master for the end user.

Creating a quaternion from location + target + up vector as far as I know it is not always possible.

Does any other more easy approach exist?

Thanks,

Alberto

You should be able to do this as follows:

1. Build the rotation matrix as:
[ up.cross(look) up look]
2. Form the quaternion corresponding to this rotation matrix to quaternion formula:
matr to quat
3. convert the quat back to a matrix or whatever to build the camera…

??? no euler angles or singularities as far as I can see…

What do you intend as “rotation”? The camera can tilt?

With a target the most intuitive system for the user is a Spherical coordinate system.
http://en.wikipedia.org/wiki/Spherical_coordinate_system
So you give latitude, longitude and tilt angle.

## Q58. How do I convert spherical rotation angles to a quaternion?

A rotation axis itself may be defined using spherical coordinates
(latitude and longitude) and a rotation angle

In this case, the quaternion can be calculated as follows:

``````-----------------------
sin_a    = sin( angle / 2 )
cos_a    = cos( angle / 2 )
``````
``````sin_lat  = sin( latitude )
cos_lat  = cos( latitude )
``````
``````sin_long = sin( longitude )
cos_long = cos( longitude )
``````
``````qx       = sin_a * cos_lat * sin_long
qy       = sin_a * sin_lat
qz       = sin_a * sin_lat * cos_long
qw       = cos_a
``````

@nickels: If you build the rotation matrix like that what happen when you look up or down?

I guess I was assuming the up direction was specified by the user, orthogonal to the look direction, but I see the problem if it is a predetermined direction.
There are other ways, of course, to compute two vectors spanning the cospace of the look vector. Maybe switch to one of these methods if the dot product of the look and world up vector is too large (or cross product too small)… ?? But then the camera roll angle becomes arbitrary…

Thanks Rosario,

Your approach is the one I like most!

Alberto

But it doesn’t work! I believed to pass the angle in XY plane and the angle from XY plane but instead this approach simply prepare an axis and angle for the Quaternion from Axis and Angle. Something completely different.

Alberto

Mumble… the quaternion axis should be the -Zaxis of the camera.
I’m gonna try to implement this this evening.

Ciao…