【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);

 

 

 刚体对象

 

 

转载于:https://www.cnblogs.com/esCharacter/p/8490434.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值