Bullet collision detection not detecting collisions

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Bullet collision detection not detecting collisions

Postby DragoniteSpam » Sun May 06, 2018 12:38 am

I'm trying to do a 3D collision test with Bullet, which I would have expected to work fairly simply:

Code: Select all
   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);
      . . .
   }

. . . later on . . .

Code: Select all
   @Override
   public void render() {
      . . .
      world.performDiscreteCollisionDetection();
      . . .
   }

. . . and finally . . .

Code: Select all
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;
   }
}

A 2.5-unit sphere at (0, 0, 0) and a 2.5-unit sphere at (1, 0, 0) should cause a collision, shouldn't they? 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.
DragoniteSpam
 
Posts: 2
Joined: Sun May 06, 2018 12:23 am

Re: Bullet collision detection not detecting collisions

Postby evilentity » Sun May 06, 2018 9:56 am

Looking for a freelancer? PM me!
evilentity
 
Posts: 4461
Joined: Wed Aug 24, 2011 11:37 am

Re: Bullet collision detection not detecting collisions

Postby DragoniteSpam » Sun May 06, 2018 2:36 pm

. . . that's what I was following to get where I am so far. Did I miss a step or something?

Update: 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. Am I missing something in the contact listener?

Second update: I figured it out; I had WorldObject.COLLISION_PRIMARY set to 1<<9, thinking that was the first available collision flag (i.e. which didn't conflict with the built-in ones), but . . . it wasn't. Bullet was apparently getting confused between my collision flags and the built-in ones. I changed it to 1<<11 and now everything is responding as expected.

Third update: Xoppa wrote in some corrections
DragoniteSpam
 
Posts: 2
Joined: Sun May 06, 2018 12:23 am


Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron