Game Development Reference
In-Depth Information
Initializing Bullet
We know that LibGDX uses a wrapper to call the C++ Bullet library. So, before
calling any of the Bullet functions, we have to load the Bullet library to memory. To
do this, we call Bullet.init() in the create() method. Calling any of the Bullet
functions, for example, btDefaultCollisionConfiguration() before Bullet.
init() will result in an error.
Creating a dynamics world
After initializing Bullet, we create the virtual physics world where everything
happens. To do this, we add the following code:
collisionConfiguration = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(collisionConfiguration);
broadphase = new btDbvtBroadphase();
solver = new btSequentialImpulseConstraintSolver();
world = new btDiscreteDynamicsWorld(dispatcher, broadphase,
solver, collisionConfiguration);
world.setGravity(new Vector3(0, -9.81f, 1f));
Collision detection in a 3D world is complex. We can use specialized collision
detection algorithms, however, they are very expensive if we use them to check all
bodies at a time. Ideally, we'd first check whether the two objects are near each other,
for example, using a bounding box or bounding sphere, and only if they are near
each other, we'd use the more accurate and specialized collision algorithm. This two
phase method has benefits. The first phase, where we find collision objects that are
near each other, is called the broad phase. Then, the second phase, where a more
accurate specialized collision algorithm is used, is called the near phase. In practice,
the collision dispatcher is the class we've used for the near phase.
To construct the dynamics world, we'll need a constraint solver and a collision
configuration. The constraint solver is used to attach objects to each other. Also,
btCollisionConfiguration allows the Bullet collision detection stack allocator
and pool memory allocators to be configured. This collision configuration is also fed
to the collision dispatcher through its constructor and then we create our dynamic
world by calling btDiscreteDynamicsWorld . The btDiscreteDynamicsWorld class
is a subclass of btDynamicsWorld , which is a subclass of btCollisionWorld . When
the world is created, we define its gravity using the setGravity() function.
 
Search WWH ::




Custom Search