// the default constraint solver. For parallel processing you can use a
// different solver (see Extras/BulletMultiThreaded)
SequentialImpulseConstraintSolver solver = new SequentialImpulseConstraintSolver();
DiscreteDynamicsWorld dynamicsWorld = new DiscreteDynamicsWorld(
dispatcher, overlappingPairCache, solver,
collisionConfiguration);
dynamicsWorld.setGravity(new Vector3f(0, -10, 0));
// create a few basic rigid bodies
CollisionShape groundShape = new BoxShape(new Vector3f(50.f, 50.f, 50.f));
// keep track of the shapes, we release memory at exit.
// make sure to re-use collision shapes among rigid bodies whenever
// possible!
ObjectArrayList<CollisionShape> collisionShapes = new ObjectArrayList<CollisionShape>();
collisionShapes.add(groundShape);
Transform groundTransform = new Transform();
groundTransform.setIdentity();
groundTransform.origin.set(new Vector3f(0.f, -56.f, 0.f));
{
float mass = 0f;
// rigidbody is dynamic if and only if mass is non zero,
// otherwise static
boolean isDynamic = (mass != 0f);
Vector3f localInertia = new Vector3f(0, 0, 0);
if (isDynamic) {
groundShape.calculateLocalInertia(mass, localInertia);
}
// using motionstate is recommended, it provides interpolation
// capabilities, and only synchronizes 'active' objects
DefaultMotionState myMotionState = new DefaultMotionState(groundTransform);
RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(
mass, myMotionState, groundShape, localInertia);
RigidBody body = new RigidBody(rbInfo);
// add the body to the dynamics world
dynamicsWorld.addRigidBody(body);
}
{
// create a dynamic rigidbody
// btCollisionShape* colShape = new
// btBoxShape(btVector3(1,1,1));
CollisionShape colShape = new SphereShape(1.f);
collisionShapes.add(colShape);
// Create Dynamic Objects
Transform startTransform = new Transform();
startTransform.setIdentity();
float mass = 1f;
// rigidbody is dynamic if and only if mass is non zero,
// otherwise static
boolean isDynamic = (mass != 0f);
Vector3f localInertia = new Vector3f(0, 0, 0);
if (isDynamic) {
colShape.calculateLocalInertia(mass, localInertia);
}
startTransform.origin.set(new Vector3f(2, 10, 0));
// using motionstate is recommended, it provides
// interpolation capabilities, and only synchronizes
// 'active' objects
DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(
mass, myMotionState, colShape, localInertia);
RigidBody body = new RigidBody(rbInfo);
dynamicsWorld.addRigidBody(body);
}
// Do some simulation
for (int i=0; i<100; i++) {
dynamicsWorld.stepSimulation(1.f / 60.f, 10);
// print positions of all objects
for (int j=dynamicsWorld.getNumCollisionObjects()-1; j>=0; j--)
{
CollisionObject obj = dynamicsWorld.getCollisionObjectArray().getQuick(j);
RigidBody body = RigidBody.upcast(obj);
if (body != null && body.getMotionState() != null) {
Transform trans = new Transform();
body.getMotionState().getWorldTransform(trans);
System.out.printf("world pos = %f,%f,%f\n", trans.origin.x,