Game Development Reference
In-Depth Information
Most physics systems have easy ways to create basic shapes like spheres, boxes, and
capsules. In Bullet, spheres are represented by the btSphereShape class. Creating
an object in the physics system is as simple as creating the object
'
s shape and then
passing that shape to a new btRigidBody .
You
ve separated out the creation of the shape in VAddSphere()
and the creation of the body in AddShape() . This is good practice because you can
then reuse the code in AddShape() when you create other types of objects.
Although we don
'
ll notice that we
'
'
t do it in this example, physics actors can be described with multi-
ple base shapes, which is a great feature. You could describe a hammer quite accu-
rately with two bodies, each with different sizes, shapes, and properties. In this case,
we only have the one sphere shape. The mass is calculated based on the volume and
density of the material, so the user can customize whether he wants an object that is
dense like iron or light like styrofoam.
Next comes the position, which is sucked right out of the actor
s TransformComponent .
You pass this in to a new ActorMotionState, which tracks any actor being moved
by the physics engine. You pass this motion state along with other configuration info
into the constructor for the new btRigidBody and add the btRigidBody object to
the physics system.
'
Creating a Convex Mesh
Spheres are nice, but they aren
'
'
ll probably want to create an
object that has a more interesting shape, and one way to do that is to use a convex
mesh. This is an object that has an arbitrary shape, with one restriction: it can
t all that interesting. You
t have
any holes or empty space in between parts of the same object. So a potato is a convex
mesh, but a donut is not.
Creating them in Bullet is pretty easy:
'
void BulletPhysics::VAddPointCloud(Vec3 *verts,
int numPoints,
WeakActorPtr *pGameActor,
const std::string densityStr,
const std::string physicsMaterial)
{
StrongActorPtr pStrongActor = MakeStrongPtr(pGameActor);
if (!pStrongActor)
return;
btConvexHullShape * const shape = new btConvexHullShape();
 
 
Search WWH ::




Custom Search