btRigidBody类主要用于刚体数据的计算。
在模拟刚体动画过程中,可以使用btRigidBody类获取所保存的刚体对象,进而控制刚体对象的旋转和位移。进行刚体模拟计算需要经常用到此类。
API:http://bulletphysics.org/Bullet/BulletFull/classbtRigidBody.html
创建刚体对象
btCollisionShape* colShape = new btBoxShape(btVector3(5, 5, 5));//创建一个基本几何体
/// Create Dynamic Objects
btTransform startTransform;
startTransform.setIdentity();
btScalar mass(1.f);
//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);
btVector3 localInertia(0, 0, 0);
if (isDynamic)
colShape->calculateLocalInertia(mass, localInertia);
startTransform.setOrigin(btVector3(2, 10, 0));//刚体初始位置
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, colShape, localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
dynamicsWorld->addRigidBody(body);
从场景中获取刚体对象
btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i];//i为场景中刚体对象的索引
btRigidBody* body = btRigidBody::upcast(obj);
常用方法:
刚体质心坐标:
body->getCenterOfMassPosition();
获取刚体四元数:
btQuaternion qua = body->getOrientation();
刚体旋转矩阵:
btTransform trans;
body->getMotionState()->getWorldTransform(trans);
btMatrix3x3 matrix33 = trans.getBasis();
向刚体添加外力:
body->applyCentralForce(btVector3(2, 1, 0));
向刚体添加转矩:
body->applyTorque(btVector3(0, 30, 0));
设置刚体变换:
btTransform trans;
trans.setIdentity();
trans.setOrigin(body->getCenterOfMassPosition() - dx);
body->setCenterOfMassTransform(trans);
刚体对象