What I can't figure out is (and this may be a basic C++ question), can you later use the setNode method after the rigid body has been instantiated and the MotionState stored, to change the node, or can you only do it while constructing the original MotionState before it is stored in the RigidBody?Ogre3d
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.
Code: Select all
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; };
I wanted to iterate through all the rigid bodies int he world, retrieve the MotionState via body->getMotionState() and the call the setNode method on the retrieved state. However, getMotionState returns a btMotionState, not the derived class, and therefore the added methods in the derived class are not available in the returned state, but that seems tobe what the tutorial is implying?
What am I missing here?
Thanks.