[Bullet3]常見物體和初始化

官方文檔:http://bulletphysics.org
開源代碼:https://github.com/bulletphysics/bullet3/releases
API文檔:http://bulletphysics.org/Bullet/BulletFull/annotated.htmlhtml

1. 初始化物體

  1. 物體的形狀由btCollisionShape對象維護;
  2. 物體的位置,旋轉狀態由btTransform對象維護;
  3. 最終須要將物體封裝成btRigidBodybtSoftBody或其它對象;
  4. 而後將步驟3的對象加入到場景中。

例如git

btCollisionShape* shape = new btBoxShape(btVector3(btScalar(1000.),btScalar(10.),btScalar(1000.)));
btTransform trans;                       // 位置、旋轉維護對象
trans.setIdentity();
trans.setOrigin(btVector3(0, -10, 0));   // 設置位置

btScalar mass=0.f;
btVector3 localInertia(0, 0, 0);
bool isDynamic = (mass != 0.f);
if (isDynamic)
    shape->calculateLocalInertia(mass, localInertia);  // 設置慣性

btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
btRigidBody::btRigidBodyConstructionInfo cInfo(mass, myMotionState, shape, localInertia);
btRigidBody* body = new btRigidBody(cInfo);            // 封裝成剛體
g_world->addRigidBody(body);                           // 將物體添加到場景

2. 常見物體對象

  • btCollisionObject 基類
  • btRigidBody 剛體
  • btSoftBody 流體

2.1. 物體對象經常使用函數

  • btCollisionShape* btCollisionObject::getCollisionShape()
    • btCollisionObject對象中獲取形狀維護對象
  • void btCollisionObject::setFriction(btScalar frict)
    • 設置摩擦力
    • 默認值:0
  • void btCollisionObject::setRestitution(btScalar rest)
    • 設置碰撞反彈係數
    • 默認值:0
  • void btRigidBody::applyImpulse(const btVector3 & impulse, const btVector3 & rel_pos)
    • 設置衝量/動量(經過這個設置初始速度)
  • void btRigidBody::applyCentralImpulse(const btVector3 & impulse)
    • 設置衝量/動量(經過這個設置初始速度)
    • 默認值:0

3. 初始化常見物體形狀

http://bulletphysics.org/Bullet/BulletFull/classbtCollisionShape.html
常見的物體有長方體、球體、膠囊體、三角網格集合。github

  • btCollisionShap
    • 基類
  • btBoxShape
    • 長方體
    • BOX_SHAPE_PROXYTYPE
  • btSphereShape
    • 球體
    • SPHERE_SHAPE_PROXYTYPE
  • btCapsuleShape
    • 膠囊體
    • CAPSULE_SHAPE_PROXYTYPE
  • btBvhTriangleMeshShap
    • 三角網格
    • TRIANGLE_MESH_SHAPE_PROXYTYPE

3.1. 物體對象經常使用函數

  • int btCollisionShape::getShapeType() const
    • 獲取物品類型,類型參考如下枚舉
    • #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" //for the shape types

3.2. 三角網格

  • 構造函數btBvhTriangleMeshShape::btBvhTriangleMeshShape(btStridingMeshInterface* meshInterface,bool useQuantizedAabbCompression)
  • 構造函數btBvhTriangleMeshShape::btBvhTriangleMeshShape(btStridingMeshInterface* meshInterface,bool useQuantizedAabbCompression, bool buildBvh = true)
  • btTriangleIndexVertexArray類集成於 btStridingMeshInterface接口。
  • btIndexedMesh 三角網格頂點列表和索引列表維護類
3.2.1. 三角網格數據假設格式以下
  • 頂點表 Vertex Buff
  • 三角形表 Index Buff
#define Landscape03.txCount 1980      // 頂點數量
#define Landscape03.dxCount 11310     // 三角形數量
#include "LinearMath/btScalar.h"

btScalar Landscape03.tx[] = {         // 頂點座標列表(三維)
-3.0.0f,3.99193.,113.3.1f,
-3.0.0f,3.18397f,117.188f,
-3.6.094f,1.63.63.,113.3.1f,
...};

unsigned short Landscape03.dx[] = {   // 三角形列表
0,1,3.
3,3.1,
3.3,4,
5,4,3,
4,5,6,
...};
3.2.3. btStridingMeshInterface接口

通用高性能三角網格訪問接口。app

btStridingMeshInterface* meshInterface = new btTriangleIndexVertexArray();
btIndexedMesh part;

part.m_vertexBase = (const unsigned char*)LandscapeVtx[i];
part.m_vertexStride = sizeof(btScalar) * 3;
part.m_numVertices = LandscapeVtxCount[i];
part.m_triangleIndexBase = (const unsigned char*)LandscapeIdx[i];
part.m_triangleIndexStride = sizeof( short) * 3;
part.m_numTriangles = LandscapeIdxCount[i]/3;
part.m_indexType = PHY_SHORT;

meshInterface->addIndexedMesh(part,PHY_SHORT);

bool useQuantizedAabbCompression = true;
btBvhTriangleMeshShape* trimeshShape = new btBvhTriangleMeshShape(meshInterface,useQuantizedAabbCompression);

3.3. 長方體

  • 構造函數btBoxShape::btBoxShape(const btVector3 & boxHalfExtents)
  • 長寬高,封裝成btVector3對象

3.4. 球

  • 構造函數btSphereShape::btSphereShape(btScalar radius)
  • radius xyz軸的半徑,能夠設置爲橢圓球

3.5. 膠囊體

  • 構造函數btCapsuleShape::btCapsuleShape()
  • 構造函數btCapsuleShape::btCapsuleShape(btScalar radius, btScalar height)
  • radius 膠囊體半徑,能夠設置爲橢圓球
  • height 膠囊體長度,height爲圓心之間的距離
  • 膠囊體的aabb的邊的長度爲 {radius2, radius2, radius*2+height}
相關文章
相關標籤/搜索