# glm direction vector rotation

I'm working on a flight simulator, but I'm stuck with my airplane orientation. I tried some things but noone worked correctly. This is what I have :

To be able to move it and roll it around himself, I need two vectors, forward and up, and use them to create quaternions I need for the rotation :

void Plane::Update()
{
m_position += ( m_forward * m_speed );

mat4 translation = translate( mat4( 1.0f ), m_position );

float angle = dot( vec3( 1.0f, 0.0f, 0.0f ), m_forward );
quat direction = angleAxis( acos( angle ), cross( vec3( 1.0f, 0.0f, 0.0f ), m_forward ) );

m_matrix = translation * mat4_cast( direction );
}


vec3( 1.0f, 0.0f, 0.0f ) is my model orientation. Note that in this code, I don't have quaternion for rolling the plane, because I first want to have a correct direction.

This works great, what doesn't work is when I want to make him taking off. To do that, I first get the right vector, then use it to create my quaternion with the angle I need, and I apply the rotation to the forward and up vector.

void Plane::FlyUp()
{
vec3 right = cross( m_forward, m_up );

quat temp = angleAxis( radians( 1.0f ), right );

m_up = temp * m_up;
m_up = normalize( m_up );

m_forward = temp * m_forward;
m_forward = normalize( m_forward );
}


Using debugger and an online vector visualizer, It seems to give me the good vectors, but the plane is rotating weirdly ( in fact, that's not even only rotating, he's scaled too for some reasons... ).

What am I doing/understanding wrong?

Edit :

To be more precise, here is screenshoots of what I have :

And what I'm trying to have, whatever the m_forward vector is pointing to :

• Where FlyUp is called in your code ? I suspect this has to do with function call sequence. Commented Sep 10, 2014 at 13:48
• In the render loop. If the up key is pressed, FlyUp() is called. Commented Sep 10, 2014 at 16:13

The bug quite likely comes from angleAxis requiring a normalized vector (see quaternion.inl in the source code). You need to call normalize() on the cross product result.

(you might be interested in my article about creating a quaternion from two vectors without using trigonometry functions)

• Thanks. That was indeed half of the problem. Do I have to normalize every vectors that are the result of cross product? I tought cross product of 2 normalized vectors was a normalized vector. Do I have to normalize quaternions too? Or angleAxis output normalized one? Commented Sep 13, 2014 at 10:32
• The length of the cross product of two unit vectors is the sin() of the angle they form, so it will be a unit vector only if the two vectors are orthogonal. In general you need to ensure your quaternions are normalised, but in the case of angleAxis() the function takes care of it. Commented Sep 13, 2014 at 12:44