Convex2dShape collision problem

Poita_
Posts: 9
Joined: Sun Oct 04, 2009 10:00 am

Convex2dShape collision problem

Post by Poita_ »

I have created a simple 2D scene with 100 small triangles (rigid bodies with btConvex2dShape / ConvexHullShape), gravity, and walls on the left, right and bottom on the screen. They fall down and collide with the ground just fine, but when two triangles hit each other they often (not always) get unnaturally launched away from each other.

Here is my set up code. I'm stepping the simulation at 1/1000, and that's the only thing that is done after setup. As you can see, 'r', which is used for restitution is set at 0.1, and I've also applied heavy damping (0.9), but it's not solving the problem.

Code: Select all

   collisionConfiguration = new btDefaultCollisionConfiguration();
	dispatcher = new	btCollisionDispatcher(collisionConfiguration);
	
	btVoronoiSimplexSolver* simplex = new btVoronoiSimplexSolver();
	btMinkowskiPenetrationDepthSolver* pdSolver = new btMinkowskiPenetrationDepthSolver();
	btConvex2dConvex2dAlgorithm::CreateFunc* convexAlgo2d = new btConvex2dConvex2dAlgorithm::CreateFunc(simplex,pdSolver);
	dispatcher->registerCollisionCreateFunc(CONVEX_2D_SHAPE_PROXYTYPE,CONVEX_2D_SHAPE_PROXYTYPE,convexAlgo2d);
	dispatcher->registerCollisionCreateFunc(BOX_2D_SHAPE_PROXYTYPE,CONVEX_2D_SHAPE_PROXYTYPE,convexAlgo2d);
	dispatcher->registerCollisionCreateFunc(CONVEX_2D_SHAPE_PROXYTYPE,BOX_2D_SHAPE_PROXYTYPE,convexAlgo2d);
	dispatcher->registerCollisionCreateFunc(BOX_2D_SHAPE_PROXYTYPE,BOX_2D_SHAPE_PROXYTYPE,new btBox2dBox2dCollisionAlgorithm::CreateFunc());
	
	broadphase = new btDbvtBroadphase();
	solver = new btSequentialImpulseConstraintSolver();
	
	world = new btDiscreteDynamicsWorld
	(
	 dispatcher,
	 broadphase,
	 solver,
	 collisionConfiguration
	);
	
	world->setGravity(btVector3(0.0f, -0.981f, 0.0f));
	
	float r = 0.1f;
	
	btScalar mass = 1.0f;
	btVector3 inertia(0.0f, 0.0f, 0.0f);
	
	for (int i = 0; i < N_BODIES; ++i)
	{
		btConvexHullShape* hull = new btConvexHullShape();
		hull->addPoint(btVector3(0.0f, 0.02f, 0.0f));
		hull->addPoint(btVector3(-0.02f, -0.02f, 0.0f));
		hull->addPoint(btVector3(0.02f, -0.02f, 0.0f));
		hull->setMargin(0.001f);
		hull->calculateLocalInertia(mass, inertia);
	
		btConvex2dShape* shape = new btConvex2dShape(hull);
		shape->setMargin(0.001f);
		shape->calculateLocalInertia(mass, inertia);
		
		body[i] = new btRigidBody(mass, 0, shape, inertia);
		body[i]->setRestitution(r);
		body[i]->setWorldTransform(btTransform(btQuaternion::getIdentity(), btVector3((rand() % 100)*0.01, 0.1f + 0.06 * i, 0.0f)));
		body[i]->setDamping(0.9, 0.9);
		body[i]->setActivationState(DISABLE_DEACTIVATION);
		
		world->addRigidBody(body[i]);
	}
Any ideas what's causing this?

Thanks in advance.
Poita_
Posts: 9
Joined: Sun Oct 04, 2009 10:00 am

Re: Convex2dShape collision problem

Post by Poita_ »

Ok, managed to fix this. Turned out to be a numeric stability problem. I simply multiplied all the position coordinates and gravity by 100 and it fixed the problem... which is a little worrying.

Can I change Bullet to use double precision instead of single (I assume it's using single)?

Or is there any other way that I can prevent this from happening in future?

Thanks in advance.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Convex2dShape collision problem

Post by Erwin Coumans »

Please read the Bullet_User_Manual.pdf, it describes limitations and some tips at the end.

Bullet_User_Manual.pdf is included in the Bullet source archive (Bullet 2.76.zip)

Thanks,
Erwin