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);
剛體對象

