Bullet(Cocos2dx)之分析刚体创建与销毁(Primitives)

Bullet(Cocos2dx)之分析刚体创建与销毁(Primitives)

分类: cocos2d-x 物理引擎 149人阅读 评论(0) 收藏 举报

目录(?)[+]

相关链接:http://bulletphysics.org/mediawiki-1.5.8/index.php/Collision_Shapes

Bullet基本图形简介

Bullet提供的基本图形包括球体、长方体、圆柱体、胶囊体、圆锥体、多球体

当然还有一个Plane,一个无限的平面

 

1.球体是个很简单的形状:

btSphereShape (btScalar radius) 提供一个球体的半径

 

2.长方体(盒子)

btBoxShape (const btVector3 &boxHalfExtents) 提供盒子的半尺寸(长宽高的一半)

 

3.圆柱体(类似box shape

btCylinderShape (const btVector3 &halfExtents) 提供半尺寸 (半径高度不知)

默认轴为y轴,btCylinderShapeX (x), btCylinderShapeZ(z)

 

4.胶囊体

btCapsuleShape (btScalar radius, btScalar height) 提供半球形半径圆柱体高度实际高度为2*radius + height

默认轴为y轴,btCapsuleShapeX (x), btCapsuleShapeZ(z)


5.圆锥体

btConeShape (btScalar radius, btScalar height) 提供底面半径高度

默认轴为y轴,btConeShapeX (x), btConeShapeZ(z)

 

6.多球体(多个球体组合)

btMultiSphereShape (const btVector3 *positions, const btScalar *radi, int numSpheres) 

positions每个球体的位置,radi每个球体的半径, 球体个数


创建刚体

1.首先获取要创建的刚体形状,以box为例

btCollisionShape* colShape = new btBoxShape(size * 0.5f); // halfSize

因为btBoxShape继承自btCollisionShape

2.设置刚体的其他基本信息

(1).刚体的变换矩阵

(2).刚体的惯性(动态物体才有)

(3).MotionState提供插值,同步活动的物体等

(4).刚体的材质信息

(5).创建刚体

  1. btTransform startTransform;    // 1  
  2. startTransform.setIdentity();  
  3. startTransform.setOrigin(position);  
  4.    
  5. //rigidbody is dynamic if and only if mass is non zero, otherwise static  
  6. bool isDynamic = (material.mass != 0.f);  
  7.    
  8. btVector3 localInertia(0,0,0);  
  9. if (isDynamic)  
  10. colShape->calculateLocalInertia(material.mass, localInertia); // 2  
  11.    
  12. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects  
  13. btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); // 3  
  14. btRigidBody::btRigidBodyConstructionInfo rbInfo(material.mass, myMotionState,colShape,localInertia); // 4  
  15. rbInfo.m_restitution = material.restitution;  
  16. rbInfo.m_friction = material.friction;  
  17. rbInfo.m_rollingFriction = material.rollingFriction;  
  18. btRigidBody* body = new btRigidBody(rbInfo); // 5  

3.将刚体添加到world

_world->addRigidBody(body);

销毁刚体

1.因为刚体继承自btCollisionObject所以从world获取_world->getCollisionObjectArray()

2.如果obj为刚体则要转换为刚体btRigidBody* body = btRigidBody::upcast(obj);

因为刚体有可能包含MoitonState,

3.删除刚体的MotionState,和CollisionShape,(CollisionShape不就是obj吗?查看两者的值是不一样的)

4.从世界移除obj,不是应该移除刚体吗?看源码注释可知

///removeCollisionObject will first check if it is a rigid body, if so call removeRigidBody otherwise call btCollisionWorld::removeCollisionObject 

virtual void removeCollisionObject(btCollisionObject* collisionObject);

5.释放obj,因为是用new 生成的,new实际是Bullet的内存分配方式,重载new

6.释放完毕

  1. //remove the bodies from the dynamics world and delete them  
  2. for (i = _world->getNumCollisionObjects() - 1; i >= 0; i--)  
  3. {  
  4. btCollisionObject* obj = _world->getCollisionObjectArray()[i];  
  5. btRigidBody* body = btRigidBody::upcast(obj);  
  6. if (body && body->getMotionState())  
  7. {  
  8. delete body->getMotionState();  
  9. delete body->getCollisionShape();  
  10. }  
  11. _world->removeCollisionObject(obj);    
  12. delete obj;  
  13. }  
0
0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值