Bullet引擎刚体类 —— btRigidBody
Posted esCharacter
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bullet引擎刚体类 —— btRigidBody相关的知识,希望对你有一定的参考价值。
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);
刚体对象
以上是关于Bullet引擎刚体类 —— btRigidBody的主要内容,如果未能解决你的问题,请参考以下文章
cocos2dx 3.X刚体update穿透问题。刚体A在update中通过摇杆移动,设置的和刚体