OpenMW
libs/openengine/bullet/BtOgrePG.h
Go to the documentation of this file.
00001 /*
00002  * =====================================================================================
00003  *
00004  *       Filename:  BtOgrePG.h
00005  *
00006  *    Description:  The part of BtOgre that handles information transfer from Bullet to
00007  *                  Ogre (like updating graphics object positions).
00008  *
00009  *        Version:  1.0
00010  *        Created:  27/12/2008 03:40:56 AM
00011  *
00012  *         Author:  Nikhilesh (nikki)
00013  *
00014  * =====================================================================================
00015  */
00016 
00017 #ifndef _BtOgreGP_H_
00018 #define _BtOgreGP_H_
00019 
00020 #include "btBulletDynamicsCommon.h"
00021 #include "OgreSceneNode.h"
00022 #include "BtOgreExtras.h"
00023 
00024 namespace BtOgre {
00025 
00026 //A MotionState is Bullet's way of informing you about updates to an object.
00027 //Pass this MotionState to a btRigidBody to have your SceneNode updated automaticaly.
00028 class RigidBodyState : public btMotionState
00029 {
00030     protected:
00031         btTransform mTransform;
00032         btTransform mCenterOfMassOffset;
00033 
00034         Ogre::SceneNode *mNode;
00035 
00036     public:
00037         RigidBodyState(Ogre::SceneNode *node, const btTransform &transform, const btTransform &offset = btTransform::getIdentity())
00038             : mTransform(transform),
00039               mCenterOfMassOffset(offset),
00040               mNode(node)
00041         {
00042         }
00043 
00044         RigidBodyState(Ogre::SceneNode *node)
00045             : mTransform(((node != NULL) ? BtOgre::Convert::toBullet(node->getOrientation()) : btQuaternion(0,0,0,1)),
00046                          ((node != NULL) ? BtOgre::Convert::toBullet(node->getPosition())    : btVector3(0,0,0))),
00047               mCenterOfMassOffset(btTransform::getIdentity()),
00048               mNode(node)
00049         {
00050         }
00051 
00052         virtual void getWorldTransform(btTransform &ret) const
00053         {
00054             ret = mCenterOfMassOffset.inverse() * mTransform;
00055         }
00056 
00057         virtual void setWorldTransform(const btTransform &in)
00058         {
00059             if (mNode == NULL)
00060                 return;
00061 
00062             mTransform = in;
00063             btTransform transform = in * mCenterOfMassOffset;
00064 
00065             btQuaternion rot = transform.getRotation();
00066             btVector3 pos = transform.getOrigin();
00067             mNode->setOrientation(rot.w(), rot.x(), rot.y(), rot.z());
00068             mNode->setPosition(pos.x(), pos.y(), pos.z());
00069         }
00070 
00071         void setNode(Ogre::SceneNode *node)
00072         {
00073             mNode = node;
00074         }
00075 };
00076 
00077 //Softbody-Ogre connection goes here!
00078 
00079 }
00080 
00081 #endif