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

Using Quaternions Efficiently in Real-time Applications

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

#include "GameEntityBase.h"

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

        UpdateRotation();
    }

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

        //transform into my local space
        //transform vector with orthonormal basis - 3 dot products - 9 mults 6 adds
        D3DXVECTOR3 localToTarget;
        localToTarget.x = D3DXVec3Dot( &side_, &worldToTarget );
        localToTarget.y = D3DXVec3Dot( &up_, &worldToTarget );
        localToTarget.z = D3DXVec3Dot( &dir_, &worldToTarget );

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

    virtual void transformManyVertices( unsigned int numVerts )
    {
        D3DXVECTOR3 *vertices = new D3DXVECTOR3[ numVerts ];

        D3DXVECTOR3 transformedVertex;
        D3DXQUATERNION conjQ;
        D3DXQuaternionConjugate( &conjQ, &ori_ );
        for ( unsigned int i=0; i< numVerts; i++)
        {
            //transform into my local space - 15 mults 8 adds
            transformedVertex = vertices[i];
            rotateVectorWithQuat( conjQ, transformedVertex );
        }

        delete [] vertices;
    }

    virtual void calculateOrientation( float dt )
    {
        //calculate new euler angles
        //...

        //update current orientation with new quaternion formed from euler angles
        D3DXQUATERNION tempQ;
        //D3DXQuaternionRotationYawPitchRoll( &tempQ, yaw_, pitch_, roll_ );
        quatFromEuler( tempQ, yaw_, pitch_, roll_ );
        ori_ = tempQ * ori_;

        /*
        //alternative method
        //create 3 new quats one for each angle
        D3DXQUATERNION yawQ, pitchQ, rollQ;
        D3DXQuaternionRotationAxis( &yawQ, up_, yaw_ );
        D3DXQuaternionRotationAxis( &pitchQ, side_, pitch_ );
        D3DXQuaternionRotationAxis( &rollQ, dir_, roll_ );
        //multiply all 3 together to create new orientation
        D3DXQuaternionMultiply( &ori_, &yawQ, &pitchQ );
        D3DXQuaternionMultiply( &ori_, &ori, &rollQ );
        */
    }

};


#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 | Mobile
Web03 | 2.8.140916.1 | Last Updated 10 Nov 2010
Article Copyright 2010 by gtdelarosa
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid