In the following code, the sphere should come to rest at 1.0 but since the margin of the ground is 0.2, the sphere instead comes to rest at 0.8.
This applies if the ground is static or dynamic. (A dynamic ground can be tested by using a large mass for the ground, using the correct inertia, and overriding it's gravity to 0,0,0).
If the setMargin line is commented out, the sphere comes to rest at 0.96 (the default margin). A margin of 0 works but is bad for other reasons.
Code: Select all
#include <iostream>
#include <btBulletDynamicsCommon.h>
int main (void)
{
btVector3 worldAabbMin(-10000,-10000,-10000);
btVector3 worldAabbMax(10000,10000,10000);
int maxProxies =1024;
btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0,-10,0));
btCollisionShape* groundShape = new btBoxShape(btVector3(1000,1,1000));
groundShape->setMargin(0.2);
btCollisionShape* fallShape = new btSphereShape(1);
btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0)));
btScalar groundMass = 0;
btVector3 groundInertia(0,0,0);
//groundShape->calculateLocalInertia(groundMass,groundInertia);
btRigidBody::btRigidBodyConstructionInfo
groundRigidBodyCI(groundMass,groundMotionState,groundShape,groundInertia);
btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
dynamicsWorld->addRigidBody(groundRigidBody);
btDefaultMotionState* fallMotionState =
new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
btScalar fallMass = 1;
btVector3 fallInertia(0,0,0);
fallShape->calculateLocalInertia(fallMass,fallInertia);
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(fallMass,fallMotionState,fallShape,fallInertia);
btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
dynamicsWorld->addRigidBody(fallRigidBody);
for (int i=0 ; i<1000 ; i++) {
dynamicsWorld->stepSimulation(1/60.f);
btTransform trans;
fallRigidBody->getMotionState()->getWorldTransform(trans);
std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
}
dynamicsWorld->removeRigidBody(fallRigidBody);
delete fallRigidBody->getMotionState();
delete fallRigidBody;
dynamicsWorld->removeRigidBody(groundRigidBody);
delete groundRigidBody->getMotionState();
delete groundRigidBody;
delete fallShape;
delete groundShape;
delete dynamicsWorld;
delete solver;
delete collisionConfiguration;
delete dispatcher;
delete broadphase;
return 0;
}