sphere moves through plane before bouncing

Post Reply
d2allgr
Posts: 1
Joined: Tue May 29, 2012 1:02 pm

sphere moves through plane before bouncing

Post by d2allgr » Tue May 29, 2012 1:29 pm

First of all, great work on the library.
Now, I am not a native c++ programmer (tried everything for a while before c/c++) so my code might have errors that it probably shouldn't.
I tried the http://bulletphysics.org/mediawiki-1.5. ... ello_World example in regards to setting up a plane and a sphere dropping on it, to test out some settings, but instead of using a fixed loop, I inserted it into a simple opengl program, which renders only a line for the plane and the dropping sphere.

My code to initialize bullet is:

Code: Select all

  btBroadphaseInterface* broadphase = new btDbvtBroadphase();
 
  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 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);
 
  btCollisionShape* fallShape = new btSphereShape(0.10f);

  btDefaultMotionState* fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
  btScalar mass = 0.10f;
  btVector3 fallInertia(0,0,0);
  fallShape->calculateLocalInertia(mass,fallInertia);
  btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,fallShape,fallInertia);
  btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
  dynamicsWorld->addRigidBody(fallRigidBody);
  
  dynamicsWorld->getDispatchInfo().m_allowedCcdPenetration = 0.0001f;
(I saw the last line on the forum somewhere that should have fixed my problem, but didn't)

And the code inside my loop is:

Code: Select all

float dt = (btScalar)m_clock.getTimeMicroseconds()  * 0.000001f;
m_clock.reset();
dynamicsWorld->stepSimulation(dt);
btTransform trans;
fallRigidBody->getMotionState()->getWorldTransform(trans);

glBegin(GL_LINES); //Draw our plane
glVertex3f(-1,0,0);
glVertex3f(1,0,0);
glEnd();
drawSphere(0.10f, 30, 30, trans.getOrigin().getX(), trans.getOrigin().getY(), trans.getOrigin().getZ());
where drawSphere is (simply draw a sphere of a specific radius center on a chosen location, copied the code from one of the bullet samples):

Code: Select all

void drawSphere(btScalar radius, int lats, int longs, btScalar _x, btScalar _y, btScalar _z)
Now my problem, during the simulation the sphere when it first hits the plane it will go all the way through (up to a height between -1 and -2) before it bounces and then on the second hit it goes up to -0.1 before resting at 0.1 (radius of the sphere). How can I fix it and make it react more realistically ?
I compiled bullet and the sample in both mingw32 and visual studio 2010 with the same effect.
Attachments
project2.cpp
(5.2 KiB) Downloaded 138 times

Post Reply