现在的位置: 首页 > 综合 > 正文

VS2010下配置Bullet开发环境

2018年05月05日 ⁄ 综合 ⁄ 共 3507字 ⁄ 字号 评论关闭

        Bullet是一个开源的物理引擎,介绍什么的就不写在这了,自行百度或者看维基百科去。这篇文章讲了怎么配置Bullet,大部分内容是从Bullet官网上来的。


       首先下载Bullet,可以用SVN来获得最新版源码(svn checkout http://bullet.googlecode.com/svn/trunk/
bullet-read-only,已失效
),也可以下载编译好的SDK


        然后解压下载的文件,使用CMake编译。具体的这里不写了,这里有图文教程,虽说是英文的,但是有图比较直观。


        编译完成后就不用看那个教程了,写的太麻烦。可以看到在编译后的Bullet目录下,我的是C:\work\bullet-2.80-rev2531,lib文件夹中有东西了,有了这几个重要的库,Bullet开发工作才能进行。


        可以打开Bullet目录下的msvc文件夹,找到对应于自己的VS版本的sln文件,打开,然后编译生成运行即可看到一些Demo。


       下面讲在VS2010下配置Bullet,进行开发。


        1、新建一个C++的Win32工程。

        2、在解决方案上右键-->属性,展开配置属性:

        3、C/C++-->常规,“附加包含目录”中添加Bullet的路径,即C:\work\bullet-2.80-rev2531\src。该操作配置包含文件;

        4、链接器-->常规,“附加库目录”中添加Bullet库文件路径,即C:\work\bullet-2.80-rev2531\lib。

              链接器-->输入,“附加依赖项”中添加Bullet库文件,目前先添加三个,BulletCollision_debug.lib,BulletDynamics_debug.lib,

LinearMath_debug.lib,这三个库文件比较重要,是每个Bullet程序都会用到的库。该操作配置库文件;

   (在最新的Bullet版本2.8.1中,lib文件名已修改,包含了编译器版本,所以应添加为BulletCollision_vs2010_debug.lib,BulletDynamics_vs2010_debug.lib,LinearMath_vs2010_debug.lib)

        5、应用以上配置。


        如上步骤配置完成后,就可开始编写Bullet程序了。给一个HelloWorld,来自官网。

#include <iostream>
 
#include <btBulletDynamicsCommon.h>
 
int main (void)
{
	#pragma region Common build to any Bullet App
	// Build the BroadPhase
	btBroadphaseInterface* broadphase = new btDbvtBroadphase();
 
	// Set up the collision configuration and dispatcher
	btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
	btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
	

	// The actual physics solver. A solver is what causes the objects to interact properly
	btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
 
	// instantiate the dynamics world
	btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
	dynamicsWorld->setGravity(btVector3(0,-10,0));	// set gravity, Y axis to be up
	#pragma endregion

	// create a ground plane
	btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);
    // create a sphere falling from the sky with a radius of 1 metre
	btCollisionShape* fallShape = new btSphereShape(1);
	// instantiate the ground 
	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);			// add the ground to the world
 
	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);					// add the sphere to the world
 
	for (int i=0 ; i<300 ; i++)										// step the simulation 300 times
	{
		dynamicsWorld->stepSimulation(1/60.f,10);		// step interval 60hz
 
		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;
 
 
	#pragma region Common delete to any Bullet App
	delete dynamicsWorld;
	delete solver;
	delete dispatcher;
	delete collisionConfiguration;
	delete broadphase;
	#pragma endregion

	return 0;
}

           上述代码在控制台输出sphere height,没有图形界面。


——The End——

抱歉!评论已关闭.