Code: Select all
class MyMotionState : public btMotionState
{
public:
MyMotionState(const btTransform &initialpos,Ogre::SceneNode *node);
~MyMotionState();
void setNode(Ogre::SceneNode* node);
virtual void getWorldTransform(btTransform& worldTrans )const;
virtual void setWorldTransform(const btTransform& worldTrans);
void updateTransform(btTransform& newpos);
protected:
Ogre::SceneNode* mVisibleobj;
btTransform mPos1;
};
MyMotionState::MyMotionState(const btTransform &initialpos,Ogre::SceneNode *node)
{
mVisibleobj = node;
mPos1 = initialpos;
}
MyMotionState::~MyMotionState()
{
}
void MyMotionState::setNode(Ogre::SceneNode* node)
{
mVisibleobj = node;
}
void MyMotionState::updateTransform(btTransform& newpos)
{
mPos1 = newpos;
}
void MyMotionState::getWorldTransform(btTransform& worldTrans )const
{
worldTrans = mPos1;
}
void MyMotionState::setWorldTransform(const btTransform& worldTrans)
{
if(NULL == mVisibleobj)return;
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());
}
Code: Select all
void BulletToOgre(btRigidBody* body,btTransform &trans,MyMotionState* motionstate)
{
body->getMotionState()->getWorldTransform(trans);
mymotionstate->setWorldTransform(trans);
body->getMotionState()->setWorldTransform(trans);
}
void OgreToBullet(SceneNode* node,btRigidBody* body,btTransform& trans)
{
Vector3 position = node->_getDerivedPosition();
Quaternion quat = node->_getDerivedOrientation();
trans.setIdentity();
trans.setOrigin(btVector3(position.x,position.y,position.z));
trans.setRotation(btQuaternion(quat.x,quat.y,quat.z,quat.w));
body->setWorldTransform(trans);
}
But now there have some questions,when I want to change bullet's coords with ogre, such as I use mouse to move a model,call OgreToBullet function,onlt a time will success,when I want to move again,the model will appear the lastest position(The position of the success),I try to replace the code of 'body->setWorldTransform(trans);' with ''body->getMotionState()->setWorldTransform(trans);' ,their have different result, I don't if the code I writed wrong,Can someone help me have a look.Thanks!