Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Using Quaternions Efficiently in Real-time Applications

, 10 Nov 2010 CPOL
Various methods for using quaternions in ways that maximize performance
#ifndef _FAST_GAME_ENTITY_H
#define _FAST_GAME_ENTITY_H

#include "GameEntityBase.h"

class FastGameEntity : public GameEntityBase
{
public:
    virtual void updateAI( )
    {
        D3DXVECTOR3 targetPos;
        updatePursuitAction( targetPos );
    }

    virtual void updatePursuitAction( const D3DXVECTOR3 &targetPos )
    {
        //get vector from my pos to the target
        D3DXVECTOR3 toTarget = targetPos - pos_;

        //transform into my local space - 15 mults 8 adds
        D3DXQUATERNION conjQ;
        D3DXQuaternionConjugate( &conjQ, &ori_ );
        rotateVectorWithQuat( conjQ, toTarget );

        //update direction to intercept target
        //...
    }

    virtual void transformManyVertices( unsigned int numVerts )
    {
        UpdateRotation();

        D3DXVECTOR3 *vertices = new D3DXVECTOR3[ numVerts ];

        D3DXVECTOR3 col1( side_.x, up_.x, dir_.x );
        D3DXVECTOR3 col2( side_.y, up_.y, dir_.y );
        D3DXVECTOR3 col3( side_.z, up_.z, dir_.z );

        for ( unsigned int i=0; i< numVerts; i++)
        {
            //transform into my local space
            //multiply vector with rotation matrix - 3 dot products - 9 mults 6 adds
            D3DXVECTOR3 transformedVertex;
            transformedVertex.x = D3DXVec3Dot( &col1, &vertices[i] );
            transformedVertex.y = D3DXVec3Dot( &col2, &vertices[i] );
            transformedVertex.z = D3DXVec3Dot( &col3, &vertices[i] );
        }

        delete [] vertices;
    }

    virtual void calculateOrientation( float dt )
    {
        //calculate angular velocity
        //...

        //update quaternion with angular velocity vector
        ori_ += ( (ori_ * angularVel_) * 0.5f * dt );
    }

};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

gtdelarosa
Software Developer Gameloft
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 10 Nov 2010
Article Copyright 2010 by gtdelarosa
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid