[Bullet3]建立世界(場景)及常見函數

建立世界(場景)及常見函數

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

0. 世界的繼承類

  • btCollisionWorld
    • 基類
  • btDynamicsWorld
    • 繼承於btCollisionWorld
    • 基礎的動力學實現
  • btDiscreteDynamicsWorld
    • 繼承於btDynamicsWorld
    • 剛體的運動模擬

1. 建立世界(場景)

// 初始化場景

// 用於配置碰撞檢測堆棧大小,默認碰撞算法,接觸副本池的大小
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();

// 用於計算重疊對象(碰撞檢測,接觸點計算)(接觸點會被封裝成 btPersistentManifold 對象)
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);

// 提供成對的aabb重疊監測(重疊對象的管理,存儲,增長刪除等)
btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();

btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
btDynamicsWorld* world = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);

2. 場景函數

  • void btDynamicsWorld::addRigidBody(btRigidBody* body)
    • 添加一個剛體
  • void btCollisionWorld::removeCollisionObject(btCollisionObject* collisionObject)
    • 刪除對象
  • int btCollisionWorld::getNumCollisionObjects()
    • 獲取數量
  • btAlignedObjectArray<btCollisionObject*> btCollisionWorld::getCollisionObjectArray()
    • 獲取場景全部對象
  • void btDynamicsWorld::setGravity(const btVector3& gravity)
    • 設置重力
    • 默認值:btVector3(0,0,0)
  • void btDynamicsWorld::performDiscreteCollisionDetection()
    • 碰撞檢測
  • int btDynamicsWorld::stepSimulation(btScalar timeStep, int maxSubSteps = 1, btScalar fixedTimeStep = btScalar(1.)/btScalar(60.))
    • 模擬運動

3. 舉例

  1. 場景場景;
  2. 添加一個剛體;
  3. 釋放內存退出。
#include "btBulletDynamicsCommon.h"
#include <stdio.h>

int main(int argc, char** argv)
{
    int i;
    ///-----initialization_start-----

    ///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
    btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();

    ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
    btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);

    ///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
    btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();

    ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
    btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

    btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);

    dynamicsWorld->setGravity(btVector3(0,-10,0));

    ///-----initialization_end-----

    //keep track of the shapes, we release memory at exit.
    //make sure to re-use collision shapes among rigid bodies whenever possible!
    btAlignedObjectArray<btCollisionShape*> collisionShapes;


    ///create a few basic rigid bodies
    btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));

    btTransform groundTransform;
    groundTransform.setIdentity();
    groundTransform.setOrigin(btVector3(0,-56,0));

    btScalar mass(0.);

    //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)
        groundShape->calculateLocalInertia(mass,localInertia);

    //using motionstate is optional, it provides interpolation capabilities, and only synchronizes 'active' objects
    btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
    btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
    btRigidBody* body = new btRigidBody(rbInfo);

    //add the body to the dynamics world
    dynamicsWorld->addRigidBody(body);

    //cleanup in the reverse order of creation/initialization
    delete body;
    delete myMotionState;
    delete groundShape;

    delete dynamicsWorld;
    delete solver;
    delete overlappingPairCache;
    delete dispatcher;
    delete collisionConfiguration;
}
相關文章
相關標籤/搜索