Box-Sphere test bug

Post Reply
nene_84
Posts: 1
Joined: Wed Jan 25, 2017 2:08 pm

Box-Sphere test bug

Post by nene_84 »

Hi everyone!

We are having an issue with a sphere-box test, returning contact points when they are not touching at all.

I'm creating a sphere of radius 1 at (2, 1, 2) and a small box (0.1) at (0.5, 0.05, 2). As you can see, they are not touching.

Here is what I am doing:

Code: Select all

	btDefaultCollisionConfiguration mCollisionConfiguration;
	btCollisionDispatcher mDispatcher{&mCollisionConfiguration};
	btDbvtBroadphase mBroadphase;
	btCollisionWorld mCollisionWorld{&mDispatcher, &mBroadphase, &mCollisionConfiguration};

	// Box
	const btVector3 boxSize(0.1f, 0.1f, 0.1f);
	btTransform boxTransform;
	boxTransform.setOrigin(btVector3(0.5f, 0.05f, 2.f));
	boxTransform.setRotation(btQuaternion::getIdentity());

	btVector3 halfExtends = boxSize * 0.5f;
	auto boxShape = std::make_unique<btBoxShape>(halfExtends);
	auto boxCollisionObject = std::make_unique<btCollisionObject>();
	boxCollisionObject->setCollisionShape(boxShape.get());
	boxCollisionObject->setCollisionFlags(boxCollisionObject->getCollisionFlags() & ~btCollisionObject::CF_STATIC_OBJECT);
	boxCollisionObject->setWorldTransform(boxTransform);
	mCollisionWorld.addCollisionObject(boxCollisionObject.get(), 256, -1);

	// Sphere
	const float sphereRadius = 1.f;
	btTransform sphereTransform;
	sphereTransform.setOrigin(btVector3(2.f, 1.f, 2.f));
	sphereTransform.setRotation(btQuaternion::getIdentity());

	auto sphereShape = std::make_unique<btSphereShape>(sphereRadius);
	auto sphereCollisionObject = std::make_unique<btCollisionObject>();
	sphereCollisionObject->setCollisionShape(sphereShape.get());
	sphereCollisionObject->setCollisionFlags(sphereCollisionObject->getCollisionFlags() & ~btCollisionObject::CF_STATIC_OBJECT);
	sphereCollisionObject->setWorldTransform(sphereTransform);
	mCollisionWorld.addCollisionObject(sphereCollisionObject.get(), 256, -1);

	struct ContactSensorCallback : public btCollisionWorld::ContactResultCallback 
	{
		ContactSensorCallback() : btCollisionWorld::ContactResultCallback() { }

		btScalar addSingleResult(
			btManifoldPoint& cp,
			const btCollisionObjectWrapper* colObj0,
			int partId0,
			int index0,
			const btCollisionObjectWrapper* colObj1,
			int partId1,
			int index1) override
		{
			// !!!THIS IS BEING CALLED!!!!
			return 0;
		}
	};

	ContactSensorCallback contactSensorCallback;
	mCollisionWorld.contactPairTest(
		sphereCollisionObject.get(), 
		boxCollisionObject.get(), 
		contactSensorCallback);
So, the callback is being called... I've tried with latest version of Bullet and it happens the same. I've tried setting a zero-margin to both shapes and nothing changes.

Please, do you know what might be happening with the GJK algorithm?

Thanks a lot!

UPDATE: The same test, with btCapsuleHeight(sphereRadius, 0.f), doesn't trigger a collision.
Post Reply