sphere/box collisions ignore box collision margin

sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York

sphere/box collisions ignore box collision margin

Post by sparkprime »

I can't get sphere/box collisions to work properly. Sphere/sphere and box/box collisions work fine.

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;
}
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: sphere/box collisions ignore box collision margin

Post by Erwin Coumans »

There is an issue with the btSphereBoxCollisionAlgorithm, which will be fixed for upcoming 2.71 release.

Until then, please disable the btSphereBoxCollisionAlgorithm, so it will use the generic btConvexConvexAlgorithm fallback:

Comment out line 218 and 223 in btDefaultCollisionConfiguration, so it looks like this:

Code: Select all

	if ((proxyType0 == SPHERE_SHAPE_PROXYTYPE) && (proxyType1==BOX_SHAPE_PROXYTYPE))
	{
//		return	m_sphereBoxCF;
	}

	if ((proxyType0 == BOX_SHAPE_PROXYTYPE ) && (proxyType1==SPHERE_SHAPE_PROXYTYPE))
	{
//		return	m_boxSphereCF;
	}

Thanks for the reproduction case,
Erwin
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York

Re: sphere/box collisions ignore box collision margin

Post by sparkprime »

Just confirming that that the convex/convex algorithm does indeed work fine.

Shall I add an issue?

thanks