Since Ogre3d seems popular [and I'm using it myself], here's a full implementation of a motionstate for Bullet. Instantiate it with a the initial position of a body and a pointer to your Ogre SceneNode that represents that body. As a bonus, it provides the ability to set the SceneNode much later. This is useful if you want an object in your simulation, but not actively visible, or if your application archictecture calls for delayed creation of visible objects.
class MyMotionState : public btMotionState
{
public:
MyMotionState(const btTransform &initialpos, Ogre::SceneNode *node)
{
mVisibleobj = node;
mPos1 = initialpos;
}
virtual ~MyMotionState()
{
}
void setNode(Ogre::SceneNode *node)
{
mVisibleobj = node;
}
virtual void getWorldTransform(btTransform &worldTrans) const
{
worldTrans = mPos1;
}
virtual void setWorldTransform(const btTransform &worldTrans)
{
if(NULL == mVisibleobj) return; // silently return before we set a node
btQuaternion rot = worldTrans.getRotation();
mVisibleobj->setOrientation(rot.w(), rot.x(), rot.y(), rot.z());
btVector3 pos = worldTrans.getOrigin();
mVisibleobj->setPosition(pos.x(), pos.y(), pos.z());
}
protected:
Ogre::SceneNode *mVisibleobj;
btTransform mPos1;
};
Aucun commentaire:
Enregistrer un commentaire