Bullet教程: Hello World 例項
歡迎加qq群 52821727 討論更多關於Bullet的東西
這篇文章裡我們將盡可能簡單的向你展示怎麼使用Bullet, 怎樣初始化Bullet, 設定一個動力學世界, 還有一個球落向地表 這個對鑑別你的build是否成功非常有用並且也能夠讓你快速的學習到Bullet的API. 首先,我們假設你的Bullet已經正確安裝並且正確設定了Bullet的include路徑(例如. /usr/local/include/bullet) 確保能連線到正確的lib. 否則請參閱Installation安裝. 如果你用的是gcc來編譯,請確保你的靜態庫反序,就是說. dynamics, collision, math.
初始化程式
以一個標準的hello world程式開始:
[cpp] view plaincopyprint?- #include <iostream>
- int main ()
- {
- std::cout << "Hello World!" << std::endl;
- return 0;
- }
#include <iostream> int main () { std::cout << "Hello World!" << std::endl; return 0; }
建立世界
現在我們要新增一個子彈(Bullet)模擬. 首先寫入以下語句:
#include <btBulletDynamicsCommon.h>
我們想把btDiscreteDynamicsWorld 例項化但是在做此之前我們還需要解決一些其他事情. 對於一個“hello world”例子來說它太複雜我們並不需要. 但是,為了能更符合我們自己的工程, 他們可以用來微調(fine-tuning)模擬環境.
我們需要指出使用什麼樣的 Broadphase algorithm(寬相演算法). 選擇什麼樣的泛型演算法很總要,如果有許多剛體在繪製場景裡, since it has to somehow check every pair which when implemented naively(天真) is an O(n^2) problem.
寬相(broadphase)使用 傳統的近似物體形狀並且被稱之為代理.我們需要提前告訴子彈最大的代理數, 所以才能很好的分配記憶體避免浪費. 下面就是世界裡任何時候的最大剛體數.
int maxProxies = 1024;
一些 broadphases 使用特殊的結構要求世界的尺度提前被告知, 就像我們現在遇到的情況一樣. 該broadphase可能開始嚴重故障,如果離開這個物體體積. 因為 the AxisSweep broadphase quantizes 空間基於我們使用的整個空間的大小, 您想這差不多等於你的世界.
使它小於你的世界將導致重大問題, 使它大於你的世界將導致低劣的效能.這是你程式調整的一個簡單部分, 所以為了確保數字的正確多花點時間也不防.
在這個例子中,世界從起點開始延伸10公里遠。
[cpp] view plaincopyprint?- btVector3 worldAabbMin(-10000,-10000,-10000);
- btVector3 worldAabbMax(10000,10000,10000);
btVector3 worldAabbMin(-10000,-10000,-10000);
btVector3 worldAabbMax(10000,10000,10000);
這個broadphase是我們將要使用的, 這個執行的是掃描和裁剪, 這裡可以看到更多解釋Broadphase .
[cpp] view plaincopyprint?- btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
該broadphase是一個極好的空間以消除不應碰撞的成隊物體. 這是為了提高執行效率.
您可以使用碰撞排程註冊一個回撥,過濾器重置broadphase代理,使碰撞系統不處理系統的其它無用部分
碰撞配置可以讓你微調演算法用於全部(而不是不是broadphase )碰撞檢測。這個方面現在還屬於研究階段
[cpp] view plaincopyprint?- btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
- btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
我們還需要一個"solver". 這是什麼原因導致物體進行互動得當,考慮到重力,遊戲邏輯等的影響,碰撞,會被制約。
它工作的很好,只要你不把它推向極端,對於在任何高效能模擬都有瓶頸有一些相似的可以執行緒模型:
[cpp] view plaincopyprint?- btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
終於我們可以初始化了世界了:
[cpp] view plaincopyprint?- btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
很明顯我們把重力方向設定成了Y軸的負方向,即Y軸是像上的
[cpp] view plaincopyprint?- dynamicsWorld->setGravity(btVector3(0,-10,0));
dynamicsWorld->setGravity(btVector3(0,-10,0));
子彈的政策是“誰分配,也刪除” 記住,必須符合這樣的結果
在main()後記的刪除.
我們提供了一個通用的結果. 程式碼如下:
[cpp] view plaincopyprint?- #include <btBulletDynamicsCommon.h>
- #include <iostream>
- int main () {
- std::cout << "Hello World!" << std::endl;
- // Build the broadphase
- int maxProxies = 1024;
- btVector3 worldAabbMin(-10000,-10000,-10000);
- btVector3 worldAabbMax(10000,10000,10000);
- btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
- // 設定好碰撞屬性 和排程
- btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
- btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
- // 實際上的物理模擬器
- btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
- // 世界.
- btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
- // 這裡做一些你想做的事
- // 作為一個好的程式設計習慣 做好刪除工作
- delete dynamicsWorld;
- delete solver;
- delete dispatcher;
- delete collisionConfiguration;
- delete broadphase;
- return 0;
- }
#include <btBulletDynamicsCommon.h>
#include <iostream>
int main () {
std::cout << "Hello World!" << std::endl;
// Build the broadphase
int maxProxies = 1024;
btVector3 worldAabbMin(-10000,-10000,-10000);
btVector3 worldAabbMax(10000,10000,10000);
btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
// 設定好碰撞屬性 和排程
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
// 實際上的物理模擬器
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
// 世界.
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
// 這裡做一些你想做的事
// 作為一個好的程式設計習慣 做好刪除工作
delete dynamicsWorld;
delete solver;
delete dispatcher;
delete collisionConfiguration;
delete broadphase;
return 0;
}
碰撞包圍體
我們將創造一個接地平面[靜態剛體] ,和一個球體,將屬於在地上[動態剛體] 。每個剛體需要參考碰撞包圍體. 碰撞包圍體只解決碰撞檢測問題, 因此沒有質量,慣性,恢復原狀等概念. 如果您有許多代理,使用相同的碰撞形狀[例如每飛船模擬是一個5單元半徑範圍]。這是個好做法,只有一個子彈形狀的碰撞,並分享它在所有這些代理. 但是我們這裡的兩個剛體形狀都不一樣,所以他們需要各自的shape.
地面通常是向上的並且裡原始點1米的樣子. 地面會和遠點交叉,但子彈不允許這樣做,
因此,我們將抵消它的1米和用來彌補,當我們把剛體設定好以後。
[cpp] view plaincopyprint?- btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);
我們將讓它從天上掉下來,它是一個球體,半徑為1米.
[cpp] view plaincopyprint?- btCollisionShape* fallShape = new btSphereShape(1);
btCollisionShape* fallShape = new btSphereShape(1);
這裡需要做碰撞形狀的清理工作.
剛體
在,我們可以新增形狀的碰撞到我們的現場,並將它們定位.
讓我們先初始化地面. 它的方向是特定的, 子彈的四元數形式 x,y,z,w . 位置在地面下一米, 將要補充一米我們不得不做的. 運動狀態在這裡可以得到詳細的說明:MotionStates
[cpp] view plaincopyprint?- btDefaultMotionState* groundMotionState =
- new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0)));
btDefaultMotionState* groundMotionState =
new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0)));
在第一個和最後一個引數,在下面的建構函式中是質量和地表的慣性. 由於地面是靜止的所以我們把它設定成0. 固定不動的物體,質量為0 -他是固定的.
[cpp] view plaincopyprint?- btRigidBody::btRigidBodyConstructionInfo
- groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0));
- btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
btRigidBody::btRigidBodyConstructionInfo
groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0));
btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
最後我們把地面加到世界中:
[cpp] view plaincopyprint?- dynamicsWorld->addRigidBody(groundRigidBody);
dynamicsWorld->addRigidBody(groundRigidBody);
新增下跌領域非常相似。我們將其置於50米以上的地面.
[cpp] view plaincopyprint?- btDefaultMotionState* fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
btDefaultMotionState* fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
由於它是動態剛體,我們將給予質量1公斤。我不記得如何計算一個球體的慣性,但是,這並不重要,因為子彈提供它的實現
[cpp] view plaincopyprint?- btScalar mass = 1;
- btVector3 fallInertia(0,0,0);
- fallShape->calculateLocalInertia(mass,fallInertia);
btScalar mass = 1;
btVector3 fallInertia(0,0,0);
fallShape->calculateLocalInertia(mass,fallInertia);
現在,我們可以建造剛體只是像以前一樣,並把它加到世界中:
[cpp] view plaincopyprint?- btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,fallShape,fallInertia);
- btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
- dynamicsWorld->addRigidBody(fallRigidBody);
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,fallShape,fallInertia);
btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
dynamicsWorld->addRigidBody(fallRigidBody);
一個快速的解釋btRigidBody::btRigidBodyConstructionInfo是為了; 物體的構建是通過某些引數的. 這是通過一個特殊的結構實現的。 該部分的btRigidBodyConstructionInfo被複制到物體當你建造的時候,並只用於在初始化的時候. 如果你想建立幾千個屬性一樣的物體, 你只需要建立一個btRigidBodyConstructionInfo, 並通過它建立所有的.
開始模擬
這就是有趣的開始。我們會加強模擬200倍,間隔60赫茲. 這使它有足夠的時間降落的地面上. 每一步, 我們都會打印出它離地面的高度.
這stepSimulation 在做你所期待, 不過他的介面確實很複雜. 讀Stepping The World 以獲得更多訊息.
進後,我們審查的狀態下降領域.位置和方向都封裝在btTranform物件,我們摘錄下降領域的運動狀態. 我們只關心位置,我們退出變換getOrigin ( ) 。然後,我們列印y組成部分的立場載體.
[cpp]
view plaincopyprint?
|
這應該產生一個輸出看起來像這樣的東西:
sphere height: 49.9917
sphere height: 49.9833
sphere height: 49.9722
sphere height: 49.9583
sphere height: 49.9417
sphere height: 49.9222
sphere height: 49.9
...
sphere height: 1
sphere height: 1
sphere height: 1
sphere height: 1
sphere height: 1
看起來不錯迄今。如果你圖這對輸出迭代次數,你就會得到這個:
這個球體開始於地表的一米處. 這是因為取的是幾何中心並且它的半徑為1米. 這個球剛開始會有一個大的反彈然後漸漸的減緩彈起高度.
這是可以預料的實時物理引擎,但它可以儘量減少,增加頻率的模擬步驟
. 試試再說!
現在你可以把這個動態世界代入你的程式 實時繪製出這個球體. 也可以看看其他的 Collision Shapes . 試試一堆盒子 或者圓柱體然後用一個球去扔向他們.
完整程式碼
[cpp] view plaincopyprint?- #include <iostream>
- #include <btBulletDynamicsCommon.h>
- int main (void)
- {
- btVector3 worldAabbMin(-10000,-10000,-10000);
- btVector3 worldAabbMax(10000,10000,10000);
- int maxProxies = 1024;
- btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
- btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
- btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
- btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
- btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
- dynamicsWorld->setGravity(btVector3(0,-10,0));
- btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);
- btCollisionShape* fallShape = new btSphereShape(1);
- 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);
- 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/60.f,10);
- btTransform trans;
- fallRigidBody->getMotionState()->getWorldTransform(trans);
- std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
- }
- 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 collisionConfiguration;
- delete dispatcher;
- delete broadphase;
- return 0;
- }
#include <iostream>
#include <btBulletDynamicsCommon.h>
int main (void)
{
btVector3 worldAabbMin(-10000,-10000,-10000);
btVector3 worldAabbMax(10000,10000,10000);
int maxProxies = 1024;
btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0,-10,0));
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);
btCollisionShape* fallShape = new btSphereShape(1);
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);
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/60.f,10);
btTransform trans;
fallRigidBody->getMotionState()->getWorldTransform(trans);
std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
}
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 collisionConfiguration;
delete dispatcher;
delete broadphase;
return 0;
}
http://www.cnblogs.com/hzhg/archive/2010/12/17/1908751.html
http://hi.baidu.com/kenshin1987/blog/item/9fe00afb8134878f9f51468c.html