4
\$\begingroup\$

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 :

What I have

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

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Where FlyUp is called in your code ? I suspect this has to do with function call sequence. \$\endgroup\$ – concept3d Sep 10 '14 at 13:48
  • \$\begingroup\$ In the render loop. If the up key is pressed, FlyUp() is called. \$\endgroup\$ – Aulaulz Sep 10 '14 at 16:13
1
\$\begingroup\$

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)

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Aulaulz Sep 13 '14 at 10:32
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – sam hocevar Sep 13 '14 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.