Bullet物理引擎学习笔记2(MotionState)

上一次说道更新物体位置信息的时候,是通过遍历所有物体的方法来更新的。然后这种方法会做很多无用功,因为有些物体根本就没动。

为了解决这个问题,我们就需要利用MotionStae。我们可以看到,在上次的文章中,我们也使用了MotionState,只不过是bullet提供的默认的MotionState,现在我们自己创建一个来代替它。

#include "linearmath/btMotionState.h"
#include 

class MyMotionState : public btMotionState {
public:
MyMotionState(const btTransform &initialpos, Ogre::SceneNode *node) {//初始化的时候将初始位置和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();//更新物体在Ogre中的位置信息
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;
};

这样在bullet每次调用setWorldTransform的时候,就会设置对应node的位置,那些不会动的物体就不会更新了。

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2011-2022 仙雾

请我喝杯咖啡吧~

支付宝
微信