现在的位置: 首页 > 综合 > 正文

bullet中btMotionState的理解

2018年02月01日 ⁄ 综合 ⁄ 共 1991字 ⁄ 字号 评论关闭

在上一篇文章中http://blog.csdn.net/doufei_ccst/article/details/7472663简单的讲了怎么改变bullet场景中物体的速度,

这里简单的讲一下btMotationSate 在把bullet结合进自己的3D引擎中时所起到的作用。


在bullet中,bullet本身是没有绘制功能的,因此就需要和其他的3D引擎结合起来完成场景的绘制。

假设我们选用的3D引擎叫做XXX3D ,那么XXX3D主要完成场景的绘制,而bullet主要完成场景中物体的物理。

例如:碰撞检测,重力影响 等。

这样,bullet会为XXX3D图像提供一个包围壳(我暂且这样叫了),这个包围壳是不显示的,我们看不到的,

只是存在于虚拟的物理空间中。XXX3D绘制的物体会显示在屏幕上,代表物理空间中的一个具体的物体。


那么这两者的位置变化又是怎么联系在一起的呢?

这里主要是通过btMotionState来完成的:

The btMotionState interface
class allows the dynamics world to synchronize and interpolate the updated world transforms with graphics For optimizations, potentially only moving objects get synchronized (using setWorldPosition/setWorldOrientation).

也就是说,btMotionState是XXX3D和bullet之间位置变化的纽带.

00023 class   btMotionState
00024 {
00025         public:
00026                 
00027                 virtual ~btMotionState()
00028                 {
00029                         
00030                 }
00031                 
00032                 virtual void    getWorldTransform(btTransform& worldTrans ) const =0;
00033 
00034                 //Bullet only calls the update of worldtransform for active objects
00035                 virtual void    setWorldTransform(const btTransform& worldTrans)=0;
00036                 
00037         
00038 };

可以看出,这是一个纯虚类,我们需要写自己的子类来继承这个函数,同时实现里面的

virtual void    getWorldTransform(btTransform& worldTrans ) const =0;

virtual void    setWorldTransform(const btTransform& worldTrans)=0;

方法。

通过getWorldTransform方法我们可以得到bullet的变化矩阵,使用这个变化矩阵对XXX3D物体施加这样的操作就可以达到

XXX3D绘制物体随着bullet包围壳移动的效果。

通过setWorlkTransform方法我们可以把XXX3D的变化矩阵传递给bullet,这样XXX3D中相应的物体在bullet中的包围壳就

可以使用这个变换矩阵更新自己的位置。

通过这样的两个函数就达到同步的效果了!微笑



下面根据自己的实际经验讲一下怎么同步改变物体的位置(XXX3D物体和bullet包围壳同步移动):

void Isgl3dMotionState::getWorldTransform(btTransform& centerOfMassWorldTrans) const {
float transformation[16];
[_node getTransformationAsOpenGLMatrix:transformation];//这里相当于得到XXX3D中物体的转换矩阵
centerOfMassWorldTrans.setFromOpenGLMatrix(transformation);//传递给bullet中的包围壳
}

void Isgl3dMotionState::setWorldTransform(const btTransform& centerOfMassWorldTrans) {
float transformation[16];
centerOfMassWorldTrans.getOpenGLMatrix(transformation);//从bullet中得到包围壳的转换矩阵
[_node setTransformationFromOpenGLMatrix:transformation];//传递给XXX3D中的物体
}

注:这里我在开发的过程中选用的是iSGL3D开发引擎,所以上面的代码也是和iSGL3D有点关系的,不过对于理解bullet和XXX3D结合的原理没有障碍。

好了,今天就写到这吧!!

抱歉!评论已关闭.