Quaternion vector for direction

I am trying to convert my Quaternion into a direction vector so that I can move my camera in the direction it is facing. I read that you can first convert your quaternion into a rotation matrix, and then get the direction so that I try to do it.

inline Matrix4<float> ToRotationMatrix() {
    Vector3<float> forward = Vector3<float>( 2.0f * ( GetX() * GetZ() - GetW() * GetY() ), 2.0f * ( GetY() * GetZ() + GetW() * GetX() ), 1.0f - 2.0f * ( GetX() * GetX() + GetY() * GetY() ) );
    Vector3<float> up = Vector3<float>( 2.0f * ( GetX() * GetY() + GetW() * GetZ() ), 1.0f - 2.0f * ( GetX() * GetX() + GetZ() * GetZ() ), 2.0f * ( GetY() * GetZ() - GetW() * GetX() ) );
    Vector3<float> right = Vector3<float>( 1.0f - 2.0f * ( GetY() * GetY() + GetZ() * GetZ() ), 2.0f * ( GetX() * GetY() - GetW() * GetZ() ), 2.0f * ( GetX() * GetZ() + GetW() * GetY() ) );

    return Matrix4<float>().InitRotationFromVectors( forward, up, right );
}

inline Matrix4<T> InitRotationFromVectors( const Vector3<T> &n, const Vector3<T> &v, const Vector3<T> &u ) {
    Matrix4<T> ret = Matrix4<T>().InitIdentity();

    ret[ 0 ][ 0 ] = u.GetX();
    ret[ 1 ][ 0 ] = u.GetY();
    ret[ 2 ][ 0 ] = u.GetZ();

    ret[ 0 ][ 1 ] = v.GetX();
    ret[ 1 ][ 1 ] = v.GetY();
    ret[ 2 ][ 1 ] = v.GetZ();

    ret[ 0 ][ 2 ] = n.GetX();
    ret[ 1 ][ 2 ] = n.GetY();
    ret[ 2 ][ 2 ] = n.GetZ();

    return ret;
}

inline Vector3<float> GetForward( const Matrix4<float> &rotation ) const {
    return Vector3<float>( rotation[ 2 ][ 0 ], rotation[ 2 ][ 1 ], rotation[ 2 ][ 2 ] );
}

When my camera is facing forward, it moves in the right direction, but when I turn it, the camera starts to move in the wrong direction. The camera rotates like that.

void Camera::Rotate( const Vector3<float> &axis, float angle ) {
    Rotate( Quaternion( axis, angle ) );
}

void Camera::Rotate( const Quaternion &quaternion ) {
    m_rotation = Quaternion( ( quaternion * m_rotation ).Normalized() );
}

And multiply these quaternions ....

inline Quaternion operator*( const Quaternion &quat ) const {
    Quaternion ret;

    ret[ 3 ] = ( ( *this )[ 3 ] * quat[ 3 ] ) - ( ( *this )[ 0 ] * quat[ 0 ] ) - ( ( *this )[ 1 ] * quat[ 1 ] ) - ( ( *this )[ 2 ] * quat[ 2 ] );
    ret[ 0 ] = ( ( *this )[ 3 ] * quat[ 0 ] ) + ( ( *this )[ 0 ] * quat[ 3 ] ) + ( ( *this )[ 1 ] * quat[ 2 ] ) - ( ( *this )[ 2 ] * quat[ 1 ] );
    ret[ 1 ] = ( ( *this )[ 3 ] * quat[ 1 ] ) + ( ( *this )[ 1 ] * quat[ 3 ] ) + ( ( *this )[ 2 ] * quat[ 0 ] ) - ( ( *this )[ 0 ] * quat[ 2 ] );
    ret[ 2 ] = ( ( *this )[ 3 ] * quat[ 2 ] ) + ( ( *this )[ 2 ] * quat[ 3 ] ) + ( ( *this )[ 0 ] * quat[ 1 ] ) - ( ( *this )[ 1 ] * quat[ 0 ] );

    return ret;
}

Note. Quaternion [0] is x, Quaternion [1] is y, Quaternion [2] is z, and Quaternion [3] is w.

, , . - - , , , . !

+4
1

, , : , G_p1 C_t = [0;0;1] ( G_ , C_ ).

G_p2 = G_p1 + G_t. G_t C_t.

G_t = G_R_C C_t, G_R_C - , . q, G_t = G_R_C(q) C_t . C_t = [0;0;1], , G_t - G_R_C(q). , .

+2

Source: https://habr.com/ru/post/1616761/


All Articles