Bullet HelloWorld For Visual Studio 2008

来源:互联网 发布:淘宝裙子桑蚕丝 编辑:程序博客网 时间:2024/06/10 15:21

源地址:http://www.bulletphysics.org/mediawiki-1.5.8/index.php?title=Hello_World

注:<bullet>表示bullet安装目录。

1、配置VS。

        工程属性->C/C++->附加包含目录:<bullet>/src

        工程属性->链接器->附加库目录:<bullet>/out/release8/libs

        工程属性->链接器->输入->附加依赖项:libbulletcollision.lib libbulletdynamics.lib libbulletmath.lib

2、HelloWorld。

 

 

3、解析。

Creating The World

Now, we will add a Bullet simulation. First, an appropriate include:

We want to instantiate a btDiscreteDynamicsWorld but to do this weneed some other things too. For a "hello world" example they arecumbersome because we don't need them. However, as we mature oursoftware into a useful product, they will be useful for fine-tuning thesimulation.

We need to specify what Broadphasealgorithm we want to use. Choosing the broadphase is important if theworld will have a lot of rigid bodies in it, since it has to somehowcheck every pair which when implemented naively is an O(n^2) problem.

The broadphase uses conservative approximations of objectshapes, and these are called proxies. We need to tell Bullet themaximum number of proxies that we will use, so it can size arrayswithout wasting memory. This is the maximum number of rigid bodies thatcan be in the world at any one time.

Some broadphases use spatial datastructures that require the worldsize to be known in advance, as in our case here. The broadphase maystart to malfunction badly if objects leave this volume. Because theAxisSweep broadphase quantizes space based on the size of the entirespace we're using, you want to make this roughly the size of yourworld.

Making it smaller than your world will lead to major issues,making it larger than your world will lead to inferior performance.This is one of the simplest parts of your program to tweak, so it'sworth taking the time to make sure your numbers are correct. In thisexample, the world extends 10 kilometers in every direction from theorigin.

This is the broadphase we will be using, it is an implementation of Sweep and Prune, explained on the Broadphase page.

The broadphase is an excellent place for eliminating object pairsthat should not collide. This can be for performance or gameplayreasons. You can use the collision dispatcher to register a callbackthat filters overlapping broadphase proxies so that the collisions arenot processed by the rest of the system. More information in Collision Things.

The collision configuration allows you to fine tune thealgorithms used for the full (not broadphase) collision detection. Herebe dragons!

We also need a "solver". This is what causes the objects to interactproperly, taking into account gravity, game logic supplied forces,collisions, and hinge constraints. It does a good job as long as youdon't push it to extremes, and is one of the bottlenecks in any highperformance simulation. There are parallel versions available for somethreading models.

Now, we can finally instantiate the dynamics world:

One last (rather obvious) line sets the gravity. We have chosen the Y axis to be "up".

Bullet has a policy of "whoever allocates, also deletes" memory, soall of these structures must be deleted at the end of main().

We now have prepared the first few lines that are common to *any* Bullet application. The code up to this point looks like this:

Collision Shapes

We will create a ground plane [a static rigid body], and a spherethat will fall onto the ground [a dynamic rigid body]. Each rigid bodyneeds to reference a collision shape. The collision shape is forcollisions only, and thus has no concept of mass, inertia, restitution,etc. If you have many bodies that use the same collision shape [egevery spaceship in your simulation is a 5-unit-radius sphere], it isgood practice to have only one Bullet collision shape, and share itamong all those bodies. However, we only have two rigid bodies and theyare not the same shape so they will need a shape each.

In this demonstration, we will place a ground plane runningthrough the origin. For pedantic purposes, we will define the collisionshape with an offset of 1 unit from the origin. Later, we will place arigid body using this shape in the world -1 unit away from the originso the two offsets cancel and the plane winds up intersecting theorigin. You could just use 0 for both values and achieve the sameresult, but here we place a plane defined by y = 1:

The shape that we will let fall from the sky is a sphere with a radius of 1 metre.

Collision shapes must be deleted at the end of the show just like everything else.

Rigid Bodies

Now we can add the collision shapes into our scene, positioning them with rigid body instances.

Lets first instantiate the ground. Its orientation is theidentity, Bullet quaternions are specified in x,y,z,w form. Theposition is 1 metre below the ground, which compensates the 1m offsetwe had to put into the shape itself. Motionstates are covered in detailon a page dedicated to them: MotionStates

The first and last parameters of the following constructor are themass and inertia of the ground. Since the ground is static, werepresent this by filling these values with zeros. Bullet considerspassing a mass of zero equivalent to making a body with infinite mass -it is immovable.

Finally, we add the ground to the world:

Adding the falling sphere is very similar. We will place it 50m above the ground.

Since it's dynamic we will give it a mass of 1kg. I can't rememberhow to calculate the inertia of a sphere, but that doesn't matterbecause Bullet provides a utility function:

Now we can construct the rigid body just like before, and add it to the world:

A quick explanation of btRigidBody::btRigidBodyConstructionInfo isin order; when bodies are constructed, they are passed certainparameters. This is done through a special structure Bullet providesfor this. The components of the btRigidBodyConstructionInfo are copiedinto the body when you construct it, and only used at initialisationtime. If you want to create a thousand bodies with exactly the sameproperties, you only need to build one btRigidBodyConstructionInfo, andpass that same one to all the bodies that you create.

Stepping The Simulation

This is where the fun begins. We will step the simulation 200 times,at an interval of 60hz. This will give the sphere enough time to hitthe ground under the influence of gravity. Each step, we will print outits height above the ground.

The stepSimulation function does what you'd expect, but its interface is fairly complicated. Read Stepping The World for more details.

After stepping, we examine the state of the falling sphere. Theposition and orientation are encapsulated in the btTranform object,which we extract from the falling sphere's motion state. We are onlyinterested in the position, which we pull out of the transform withgetOrigin(). We then print the y component of the position vector.

This should yield an output that looks like this:


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

Looks good so far. If you graph this output against the number of iterations, you get this:

Image:HelloWorldGraph.png

The sphere comes to rest 1 metre above the ground. This isbecause the position is taken from the centre of the sphere and it hasa radius of 1 metre. The sharp-eyed will note that the spherepenetrates the ground slightly before bouncing and coming to rest. Thisis to be expected in realtime physics engines, but it can be minimisedby increasing the frequency of the simulation steps. Try it and see!

Now you can integrate the dynamics world into your application and render falling spheres. Check out the other Collision Shapes as well. Try stacking boxes or cylinders and then shooting them over with spheres.

 

kenshin的中文翻译:http://hi.baidu.com/kenshin1987/blog/item/9fe00afb8134878f9f51468c.html

原创粉丝点击