Extending MotionState & Wiki tutorial question

User avatar
SynapticBytes
Posts: 74
Joined: Thu Feb 10, 2011 8:27 pm

Extending MotionState & Wiki tutorial question

Post by SynapticBytes »

The Wiki has the following to say in it's example of deriving a new MotionState in Ogre3d :-
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;
};
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?

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.
User avatar
SynapticBytes
Posts: 74
Joined: Thu Feb 10, 2011 8:27 pm

Re: Extending MotionState & Wiki tutorial question

Post by SynapticBytes »

Or maybe there's a better way to do this?

I want to iterate through all the rigid bodies in the world, and retrieve the name of the graphic object it is attached to. I was using the MotionState for this, as the tutorial seemed to be setup aready to do it. But if this doesnt work, is it possible to extend the rigid body class to add my own "setObjectName/getObjectname" methods, or would I just be better creating a "ShivaObject" class of my own, with my own methods, and then adding the Rigid Body as simply a member of my new class?

I'm just trying to stick as close to the Bullet API withput adding too much fluff of my own stuff, but maybe I'm making too much of it and just adding my own classes is better - it's certainly simpler.

Any suggestions? Thanks.
proof
Posts: 18
Joined: Tue Mar 01, 2011 11:00 pm

Re: Extending MotionState & Wiki tutorial question

Post by proof »

You can always cast the btMotionState to your own like: MyMotionState* mstate = (MyMotionState*)rigidBody->getMotionState();
You can then use the mstate->setNode and any other method that is a member of MyMotionState.
winspear
Posts: 77
Joined: Thu Nov 26, 2009 6:32 pm

Re: Extending MotionState & Wiki tutorial question

Post by winspear »

You can add your own name or whatever in to the void pointer associated with btRigidBody. I think it is accessed by setUserPointer and getUserPointer.
User avatar
SynapticBytes
Posts: 74
Joined: Thu Feb 10, 2011 8:27 pm

Re: Extending MotionState & Wiki tutorial question

Post by SynapticBytes »

winspear wrote:You can add your own name or whatever in to the void pointer associated with btRigidBody. I think it is accessed by setUserPointer and getUserPointer.
Sorry I'm not sure what you mean. I checked the btRigidBody and btCollisionObject classes and can't see anything about that?
User avatar
SynapticBytes
Posts: 74
Joined: Thu Feb 10, 2011 8:27 pm

Re: Extending MotionState & Wiki tutorial question

Post by SynapticBytes »

proof wrote:You can always cast the btMotionState to your own like: MyMotionState* mstate = (MyMotionState*)rigidBody->getMotionState();
You can then use the mstate->setNode and any other method that is a member of MyMotionState.
Hmm, I thought I tried that before I made my original approach, as that's what I thought of first up. I was getting errors about default constructors. But now I try it again, it's working, so I must have done the cast wrong in the first place - thanks :)