【Bullet引擎】复杂碰撞体 —— btCompoundShape
说明
btCompoundShape可用于创建不规则的复杂几何体,碰撞体由多个基础几何体组成,如球体,六面体等。即btCompoundShape是由多个基础的碰撞体组合成的碰撞体。
btCompoundShape内的子碰撞体可以动态地添加、删除和修改。
使用
btCompoundShape对象的创建
btCompoundShape* compound = new btCompoundShape();
btCollisionShape* sphereShape = new btSphereShape(radius);
btTransform localTrans;
localTrans.setIdentity();
localTrans.setOrigin(relPosition);
compound->addChildShape(localTrans, sphereShape);//添加子碰撞体
btTransform startTransform;
startTransform.setIdentity();
btScalar mass(1.);
btVector3 localInertia(0, 0, 0);
compound->calculateLocalInertia(mass, localInertia);
startTransform.setOrigin(btVector3(0., 10., 0.));//位置
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, compound, localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
dynamicsWorld->addRigidBody(body);
获取碰撞体
btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i];//i为btCompoundShape对象的索引
btRigidBody* body = btRigidBody::upcast(obj);
btCompoundShape* compound = (btCompoundShape*)body->getCollisionShape();
常用方法
子几何体数量:
compound->getNumChildShapes()
子几何体变换:
compound->getChildTransform(idx) //idx为子几何体索引
获取子几何体:
btCollisionShape * childShape = compound->getChildShape(index);
添加几何体:
btCollisionShape* sphereShape = new btSphereShape(5);
btTransform localTrans;
localTrans.setIdentity();
localTrans.setOrigin(btVector3(0, 20, 0));
compound->addChildShape(localTrans, sphereShape);
移除几何体:
compound->removeChildShapeByIndex(idx);
实例
流固耦合模拟
(流体与刚体交互模拟)
- 流体采用SPH算法,刚体使用Bullet计算。刚体由粒子组成,使用btCompoundShape实现,用于计算流体对刚体的作用力