アットウィキロゴ

Bulletの公式Wikiに載っているHello Worldを試してみたいと思います。
Hello WorldはBulletワールド上に地面と球体を生成し自由落下をシミュレートします。
3D表示もなく単純なサンプルですが、Bulletを理解するにはちょうど良いプログラムサイズになっています。



前提として、Bulletが/usr/local/libにインストールされているものとします。(cmake .; make; sudo make install)


ステップ1:コンパイルできるか試してみる

まずは、CMake用ファイルを準備します。

  • CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
# プロジェクト名
project(hellobullet)
# Bulletヘッダのディレクトリパス指定
include_directories(/usr/local/include/bullet)
# リンクライブラリのディレクトリパス指定
link_directories(/usr/local/lib)
# ソースファイルを追加
add_executable(hellobullet hellobullet.cpp)
# リンクライブラリの指定
target_link_libraries(hellobullet BulletDynamics BulletCollision LinearMath)
 

これだけです。
staticライブラリの場合、target_link_librariesの指定は、順番が大事です。

次にソースコードを準備します。

  • hellobullet.cpp
#include <btBulletDynamicsCommon.h>
#include <iostream>
 
int main(int argc, char *argv[])
{
  std::cout << "Hello World!" << std::endl;
 
  return 0;
}
 

次は、コンパイルです。

% cmake .
% make

正しくコンパイルできれば、hellobulletという実行ファイルが生成されるはずです。
実行してみてください。

% ./hellobullet
Hello World!

コンパイルが通り、実行までできました。
まずは、ステップ1はおわりです。

ステップ2:球体の自由落下


Bullet Wikiにあるサンプルそのままですが、記載します。


#include <btBulletDynamicsCommon.h>
#include <iostream>
 
int main(int argc, char *argv[])
{
  // Broad phase作成
  btBroadphaseInterface* broadphase = new btDbvtBroadphase();
 
  // 衝突設定(Narrow phase)
  btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
  btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
 
  // ソルバ作成
  btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
 
  // Bulletワールド生成
  btDiscreteDynamicsWorld* dynamicsWorld
    = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
  // 重力設定
  //  - y方向に-10 [m/s^2]
  dynamicsWorld->setGravity(btVector3(0, -10, 0));
 
  // 地面シェイプを生成
  //  - y平面
  btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);
  // 球体シェイプを生成(半径1)
  btCollisionShape* fallShape = new btSphereShape(1);
 
  // 地面のモーションステート(位置および姿勢)
  //  - 位置 x=0, y=-1, z=0 [m]
  btDefaultMotionState* groundMotionState
    = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,-1,0)));
 
  // 地面の剛体生成
  btRigidBody::btRigidBodyConstructionInfo
    groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0));
  btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
  dynamicsWorld->addRigidBody(groundRigidBody);
 
  // 球体のモーションステート(位置および姿勢)
  //  - 位置 x=0,y=50,z=0 [m]
  //  - 慣性inertia=0 
  //  - 質量mass=1 [kg]
  btDefaultMotionState* fallMotionState
    = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,50,0)));
  btScalar mass = 1;
  btVector3 fallInertia(0,0,0);
  fallShape->calculateLocalInertia(mass, fallInertia);
  btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, fallShape, fallInertia);
  btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
  dynamicsWorld->addRigidBody(fallRigidBody);
 
  for (int i=0; i<300; i++) {
    // ステップ・シミュレーション
    dynamicsWorld->stepSimulation( 1.f/60.f, 10);
 
    btTransform trans;
    fallRigidBody->getMotionState()->getWorldTransform(trans);
 
    std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
  }
 
  // Clean up
  dynamicsWorld->removeRigidBody(fallRigidBody);
  delete fallRigidBody->getMotionState();
  delete fallRigidBody;
 
  dynamicsWorld->removeRigidBody(groundRigidBody);
  delete groundRigidBody->getMotionState();
  delete groundRigidBody;
 
  delete fallShape;
  delete groundShape;
 
  delete dynamicsWorld;
  delete solver;
  delete dispatcher;
  delete collisionConfiguration;
  delete broadphase;
 
  return 0;
}
 

タグ:

Bullet
最終更新:2011年07月26日 22:50