# 3d orientation example - quaternion/matrix help

Hi,

I was hoping for some clarification about quaternions & matrix multiplication. The example I’m testing, ‘orientation’, is in the 0071 examples in the 3d folder. It displays a 3d cube moving around and orienting towards it’s direction of travel.

There is a strange flipping/spinning effect on the cube as it makes a turn from the top left of screen downwards (if you run the example, it happens after 10 seconds or so). Is this gimbal lock, or something else? I thought quaternions solved this problem?

Is there a fix or another way to orient an object to it’s direction so this irregularity does not occur? eg other matrix multiplications. Any advice or tips would be greatly appreciated.

Here’s the function which rotates the object from the example…

``````

void rotateToNormal(ofVec3f normal) {
normal.normalize();

float rotationAmount;
ofVec3f rotationAngle;
ofQuaternion rotation;

ofVec3f axis(0, 0, 1);
rotation.makeRotate(axis, normal);
rotation.getRotate(rotationAmount, rotationAngle);
ofRotate(rotationAmount, rotationAngle.x, rotationAngle.y, rotationAngle.z);
}

``````

Thanks,
Trent

Hey Trent,

This is a problem of not having enough constraints on the orientation. Without a way to both define an ‘up’ vector and a forward vector, there is sort of a free floating variable that can swing around some times.

One solution for this is something called a “parallel transport” (http://en.wikipedia.org/wiki/Parallel-transport) – Roxlu has a nice gist with some code for this:
https://gist.github.com/2859605

Hope that helps a little!