I'm trying to do a 3D collision test with Bullet, which I would have expected to work fairly simply:
@Override
public void create(){
. . .
/*
* Collision
*/
collisionConfig=new btDefaultCollisionConfiguration();
dispatcher=new btCollisionDispatcher(collisionConfig);
broadphase=new btDbvtBroadphase();
world=new btCollisionWorld(dispatcher, broadphase, collisionConfig);
contactListener=new PiscesContactListener();
contactListener.enable();
/*
* Test stuff
*/
btSphereShape sphere=new btSphereShape(2.5f);
btCollisionObject object1=new btCollisionObject();
btCollisionObject object2=new btCollisionObject();
object1.setCollisionShape(sphere);
object2.setCollisionShape(sphere);
object1.setWorldTransform(new Matrix4(new Vector3(0f, 0f, 0f), new Quaternion(0f, 0f, 0f, 0f), new Vector3(1f, 1f, 1f)));
object2.setWorldTransform(new Matrix4(new Vector3(1f, 0f, 0f), new Quaternion(0f, 0f, 0f, 0f), new Vector3(1f, 1f, 1f)));
object1.setUserValue(0);
object2.setUserValue(1);
object1.setCollisionFlags(WorldObject.COLLISION_PRIMARY); // 1<<9
object2.setCollisionFlags(WorldObject.COLLISION_PRIMARY);
world.addCollisionObject(object1, WorldObject.COLLISION_PRIMARY, WorldObject.COLLISION_EVERYTHING); // -1
world.addCollisionObject(object2, WorldObject.COLLISION_PRIMARY, WorldObject.COLLISION_EVERYTHING);
. . .
}
@Override
public void render() {
. . .
world.performDiscreteCollisionDetection();
. . .
}
/*
* In a separate file
*/
public class PiscesContactListener extends ContactListener {
public boolean onContactAdded (int userValue0, int partId0, int index0, boolean match0, int userValue1, int partId1, int index1, boolean match1) {
System.out.println("Collision detected between "+userValue0+" and "+userValue1);
return true;
}
}
(I followed this guide, though I obviously deviated from it a little to try to make it even simpler.)
A 2.5-unit sphere at (0, 0, 0) and a 2.5-unit sphere at (1, 0, 0) should cause a collision, should they not? Nothing's appearing in the console window.
I somewhat suspect there's something fundamental that I'm forgetting, since I tried to do a raycast and that isn't working either.
It's probably worth mentioning that calling world.drawDebugWorld();
draws wireframes of all the objects where they would be expected to be, so now I'm suspecting the error lies in the contact listener or maybe the flags I'm using (though I haven't had any luck with any other collision flags). Am I missing something in the contact listener?
Also, I tried using a ContactCache
instead of a ContactListener
but that didn't work either. Most reading I can find on the matter talks about ContactListener
so I'm probably better off using that.
I'm obviously forgetting something since other people are able to do this, can anyone point out what that might be?