Bullet その3 応用形状編

Bullet その3 は応用的な形状を扱います。

今回、使う形状は
 凸包 と 三角形メッシュ です。

まず、凸包からやろうと思います。
 std::vector< btVector3 > vertices; //頂点データ
 std::vector< unsigned short > indices; //インデックスデータ
 
 btTriangleMesh* tri = new btTriangleMesh;
 for( unsigned int i = 0; i < indices.size(); i += 3 ){
 	btVector3 tri0,tri1,tri2;
 	tri0 = vertices[ indices[ i ] ];
 	tri1 = vertices[ indices[ i+1 ] ];
 	tri2 = vertices[ indices[ i+2 ] ];
 	tri->addTriangle( tri0, tri1, tri2 );
 }
 
 btConvexShape* convex = new btConvexTriangleMeshShape( tri );
 btShapeHull* hull = new btShapeHull( convex );
 hull->buildHull( convex->getMargin() );
 
 btConvexHullShape* shape = new btConvexHullShape;
 for( int i = 0; i < hull->numVertices(); i++ ){
 	shape->addPoint( hull->getVertexPointer()[i] );
 }

変数tri・convex・hullは必要ないので剛体を作成した後はdeleteしてください。

次に、三角形メッシュです。
 std::vector< btVector3 > vertices; //頂点データ
 std::vector< unsigned short > indices; //インデックスデータ
 
 btTriangleMesh* triangle = new btTriangleMesh;
 for( unsigned int i = 0; i < indices.size(); i +=3 ){
 	btVector3 tri0,tri1,tri2;
 	tri0 = vertices[ indices[ i ] ];
 	tri1 = vertices[ indices[ i+1 ] ];
 	tri2 = vertices[ indices[ i+2 ] ];
 	triangle->addTriangle( tri0, tri1, tri2 );
 }
 
 btBvhTriangleMeshShape* shape = new btBvhTriangleMeshShape( triangle, true );

両方に共通して必要なデータは、頂点とインデックスのデータです。
これは自前で用意していただく必要があります。

注意が必要なのは、Bulletはコンストラクタで必要としたポインタでも内部で開放処理をしないので
自分で開放する必要があります。

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2010年07月08日 20:01
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。