创建世界(场景)及常见函数

官方文档:http://bulletphysics.org

开源代码:https://github.com/bulletphysics/bullet3/releases

API文档:http://bulletphysics.org/Bullet/BulletFull/annotated.html

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;
}

[Bullet3]创建世界(场景)及常见函数的更多相关文章

  1. css3创建3D场景

    浏览器本身是一个2维平面,对于3D的情况,实际上是增加了一个维度(深度),所以我们需要创建一个3D场景.这时浏览器不仅仅是一个平面了,更像是一个窗口,我们透过这个窗口去观察里面的三维世界.所谓的创建3 ...

  2. cocos3.2中如何创建一个场景

    1.可以将一些比较通用的东西放到Common.h中,这是一个.h文件,必须手动添加,且保证在classes目录里 #ifndef __COMMON_H__ #define __COMMON_H__ # ...

  3. NGUI使用教程(2) 使用NGUI创建2D场景而且加入标签和button

    1.创建2D场景 要使用NGUI创建2D场景,首先咱们必须新建一个项目,而且导入NGUI作为这个项目的插件,相信假设看过上一篇教程都知道怎么导入NGUI了,这里就不赘述,假设有疑问的能够去看上一篇教程 ...

  4. Three.js入门篇(一)创建一个场景

    上一面讲述了向场景中添加物体对象.这一篇准备把每个功能点细细的讲述一遍,一方面是为了加深自己的理解.另一方面希望能够 帮助到有需要的人. 一.在学习WEBGL的时候,你应该先了解要创建一个WebGL程 ...

  5. quick-cocos2d-x游戏开发【2】——项目结构分析、创建新场景

    创建完一个新项目之后,我们能够简单的看一看这个项目的文件组成,有这么一个文件层次结构 几个proj.*目录就不用说了,是相应的平台的解决方式,res专门存放我们的游戏资源.scripts存放我们的lu ...

  6. Cocos2d-x 3.0 创建一个场景,并设置现场的时候,项目开始执行上主动

    头 #ifndef __TEST_H__ #define __TEST_H__ #include "cocos2d.h" USING_NS_CC; class Test : pub ...

  7. 使用 CommandScene 类在 XNA 中创建命令场景(十二)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  8. Unity3D_(地形)创建基本场景

    第一人称漫游场景 地形漫游系统: (自己绘制的GIF文件超过20MB放不上博客园.截取了几张图片)按键盘上的“上下左右”可以控制第一人称的漫游视角 资源包和项目源文件:传送门 自己做的项目可执行文件: ...

  9. Cocos2d-x3.0游戏实例之《别救我》第二篇——创建物理世界

    这篇我要给大家介绍两个知识点: 1. 创建游戏物理世界 2. 没了(小若:我噗) 害怕了?不用操心.这太简单了~! 笨木头花心贡献.啥?花心?不呢.是用心~ 转载请注明,原文地址:http://www ...

随机推荐

  1. ecos新命令

    创建myapp,在myapp里创建lib/command目录 新建一个文件hello.php <?php /** * myapp_command_hello(myapp->app名称,co ...

  2. Quick Cocos2dx 与 DragonBones

    照着官方的例子试验了一下DragonBone的使用,代码如下: local AnotherScene = class("AnotherScene", function() retu ...

  3. VSC#2010打开视图编辑器假死/卡死

    最近写项目代码的时候写C#,VSC#2010刚配置好,打开之前同学写的项目的设计界面结果...我擦,卡死了,重复几次都是这样. 于是上网搜发现是VS一个bug,打一个VSSP1补丁就好了~ http: ...

  4. STM32串口寄存器操作(转)

    源:STM32串口寄存器操作 //USART.C /************************************************************************** ...

  5. css3快速复习

    选择器边框.阴影 border-radius: 50%; 设置正圆形背景的改变CSS3重要的新东西: ● transition 过度,让一个元素从一个样式,变为另一个样式,不再是干蹦了,而是有动画,均 ...

  6. IOS开发中如何判断程序第一次启动(根据判断结果决定是否显示新手操作引导)

    IOS开发中如何判断程序第一次启动 在软件下载安装完成后,第一次启动往往需要显示一个新手操作引导,来告诉用户怎么操作这个app,这就需要在程序一开始运行就判断程序是否第一次启动,如果是,则显示新手操作 ...

  7. UVa 10684 - The jackpot

    题目大意:给一个序列,求最大连续和. 用sum[i]表示前i个元素之和,那么以第i个元素结尾的最大连续和就是sum[i]-sum[j] (j<i)的最大值,也就是找前i-1个位置sum[]的最小 ...

  8. UVa 11790 - Murcia's Skyline

    题目大意:给一个建筑的序列,建筑用高度和宽度描述,找出按高度的LIS和LDS,最长XX子序列的长度按照序列中建筑的宽度和进行计算. 其实就是带权的最长XX子序列问题,原来是按个数计算,每个数权都是1, ...

  9. 使用命令创建github代码仓库,push本地仓库到github远程代码仓库

    1.利用命令创建github远程代码仓库 在将本地代码push到github远程代码仓库之前,总是需要新建github代码仓库,在将本地仓库关联到github远程仓库.其中最为繁琐的操作是建立gith ...

  10. struts2默认Action配置

    在项目中,需要在输入错误的url的时候,弹出友好的错误提示页面 在struts2中可以通过配置默认的action达到这个目的 配置方法: <package name="default& ...