What it is
Bullet Physics is a professional-grade physics simulation library for real-time collision detection, rigid body dynamics, soft body dynamics, and vehicle simulation. It is widely used in games, VR, robotics, and film production.
Bullet provides APIs to simulate physics worlds with rigid bodies, soft bodies, vehicles, and constraints. It includes a broad-phase collision detection system, efficient solvers, and supports GPU acceleration via OpenCL.
Installation
sudo apt install libbullet-devGetting started
The smallest useful thing you can do with it, and what each part means.
#include <btBulletDynamicsCommon.h>
#include <iostream>
int main() {
// Broadphase
btBroadphaseInterface* broadphase = new btDbvtBroadphase();
// Collision configuration and dispatcher
btDefaultCollisionConfiguration* collisionConfig = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfig);
// Solver
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
// Dynamics world
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfig);
dynamicsWorld->setGravity(btVector3(0, -9.81f, 0));
// Ground plane
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1);
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);
// Falling sphere
btCollisionShape* sphereShape = new btSphereShape(1);
btDefaultMotionState* sphereMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,50,0)));
btScalar mass = 1;
btVector3 inertia(0,0,0);
sphereShape->calculateLocalInertia(mass, inertia);
btRigidBody::btRigidBodyConstructionInfo sphereRigidBodyCI(mass, sphereMotionState, sphereShape, inertia);
btRigidBody* sphereRigidBody = new btRigidBody(sphereRigidBodyCI);
dynamicsWorld->addRigidBody(sphereRigidBody);
// Simulation loop
for (int i=0; i<300; i++) {
dynamicsWorld->stepSimulation(1/60.f, 10);
btTransform trans;
sphereRigidBody->getMotionState()->getWorldTransform(trans);
std::cout << "Sphere height: " << trans.getOrigin().getY() << std::endl;
}
// Cleanup
delete dynamicsWorld;
delete solver;
delete dispatcher;
delete collisionConfig;
delete broadphase;
return 0;
}Advanced usage
Where the library earns its place over a simpler alternative.
btHingeConstraint* hinge = new btHingeConstraint(*bodyA, *bodyB, pivotInA, pivotInB, axisInA, axisInB);
dynamicsWorld->addConstraint(hinge);btSoftBody* softBody = btSoftBodyHelpers::CreateRope(worldInfo, btVector3(0,10,0), btVector3(10,10,0), 8, 1);
dynamicsWorld->addSoftBody(softBody);btRaycastVehicle::btVehicleTuning tuning;
btRaycastVehicle* vehicle = new btRaycastVehicle(tuning, chassis, raycaster);
dynamicsWorld->addVehicle(vehicle);// Requires Bullet built with OpenCL support
// Use btOpenCLSoftBodySolver for GPU-accelerated soft body simulationErrors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Objects fall through the floor
- Ensure collision shapes are set correctly and the ground plane is added as a static rigid body.
- Simulation instability
- Use smaller timesteps, increase solver iterations, or simplify collision geometry.
- High memory usage
- Remove unused rigid bodies and shapes, and reuse collision shapes across multiple objects.
Best practices
- Use appropriate collision shapes (box, sphere, convex hull) for performance over exact meshes.
- Always call `stepSimulation` with a fixed timestep for stable results.
- Release memory carefully to avoid leaks in complex scenes.
- Use constraints sparingly; too many can hurt performance.
- Profile and optimize for broadphase vs narrowphase collision detection.
Background
Why it exists, and what it was reacting to.
Bullet Physics was created by Erwin Coumans to provide an open-source, high-performance physics engine for real-time applications. It became popular in the gaming industry and is also used in robotics simulations, VR environments, and even Hollywood films. The library is modular, efficient, and integrates well with OpenGL, Vulkan, and game engines.
