AB4D Forum

Full Version: Quaternions and camera rotation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would like to make use of quaternions to set a camera's rotation without having to perform the complex (if indeed possible) conversion to euler angles (band, heading, attitude).  Is there any way of doing this that any body knows of?
You can convert any rotation to euler angles with using the Ab3d.Utilities.CameraUtils.CalculateCameraAngles:

Code:
public static void CalculateCameraAngles(Vector3D lookDirection, Vector3D upDirection, out double heading, out double attitude, out double bank)

The methods takes lookDirection and upDirection as parameters. You can define them with setting lookDirection  to (0,0,-1) and upDirection  to (0,1,0) and then rotate those two vectors by your quaternion. Then pass the vectors to the method and get the euler angles back.


What might also work is to set the camera's Heading, Attitude and Bank to 0. Then add your quaternion rotation to the Viewport3D's Camera:


Code:
viewport3D.Camera.Transform = new RotateTransform3D(quaternionRotation3D);

This way you will add additional transformation to the camera.
Note that this option is not tested and may have some unexpected results. I would recommend using the first method.
Thanks the function call does exactly what I need.  It simple task of rotating the up and look vectors using the quaternion.
After further testing the first method doesn't always work.  In certain orientations it gets it wrong which I think is due to the "gimbal lock" problem but I can't be sure. This may be fixed by apply the bank/heading/attitude rotations in a different order, however, my object can be in any orientation so there is no safe combination of order of application of the euler rotations.  The second method works better, although I haven't tested it thoroughly yet.  I set the target position to 0 and the bank etc to 0 then convert the quaternion and offset for the object into a MatrixTransformation3D and apply that.