Collision group broken on 3.1 alphas

I just ported my game from the first alpha (using jbullet) to the latest alpha (3). The collision group stopped to work (I also tried them on the alpha 2 and they are also broken so I don’t know if it is a thing of the native bullet or if is a bad change on the latest alphas).

Well, two object collides if they are both on the collision group 1 and they have both the mask 1 set as “collidewith”. If any of the these values (either the collide group mask or the collidewith mask) is different than 1 (0x00000001) they just stop to collide.

Collision groups don’t yet work in native bullet. Theres been a PR for it but its still not implemented right.

Actually I think this is a different issue then the one I’ve made a PR for. The PR I’ve made was to fix collision group listeners not being called. There’s another issue that collision groups themselves seem to not work. See this test case:

/*
 * Copyright (c) 2009-2012 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package test;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.collision.shapes.MeshCollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;

/**
 *
 * @author normenhansen
 */
public class TestCollisionGroups extends SimpleApplication{

    private BulletAppState bulletAppState;

    public static void main(String[] args) {
        TestCollisionGroups app = new TestCollisionGroups();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        flyCam.setMoveSpeed(50);
        Material debugMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        //debugMaterial.getAdditionalRenderState().setWireframe(true);
        debugMaterial.setColor("Color", ColorRGBA.Brown);
        
        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        bulletAppState.setDebugEnabled(true);

        // Add a physics sphere to the world
        /*RigidBodyControl physicsSpherePhysics = new RigidBodyControl(new SphereCollisionShape(1), 1);
        Geometry physicsSphere = new Geometry("Sphere", new Sphere(16, 16, 1));
        physicsSphere.setMaterial(debugMaterial);
        physicsSpherePhysics.setPhysicsLocation(new Vector3f(3, 6, 0));
        physicsSphere.addControl(physicsSpherePhysics);
        rootNode.attachChild(physicsSphere);
        getPhysicsSpace().add(physicsSphere);*/

        // Add a physics sphere to the world
        RigidBodyControl physicsSpherePhysics2 = new RigidBodyControl(new SphereCollisionShape(1), 1);
        Geometry physicsSphere2 = new Geometry("Sphere2", new Sphere(16, 16, 1));
        physicsSphere2.setMaterial(debugMaterial);
        physicsSphere2.addControl(physicsSpherePhysics2);
        physicsSpherePhysics2.setPhysicsLocation(new Vector3f(4, 8, 0));
        physicsSpherePhysics2.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02);
        physicsSpherePhysics2.setCollideWithGroups(PhysicsCollisionObject.COLLISION_GROUP_01);
        rootNode.attachChild(physicsSphere2);
        getPhysicsSpace().add(physicsSphere2);

        // an obstacle mesh, does not move (mass=0)
        RigidBodyControl node2Physics = new RigidBodyControl(new MeshCollisionShape(new Sphere(16, 16, 1.2f)), 0);
        Geometry node2 = new Geometry("Sphere (collision group)", new Sphere(16, 16, 1.2f));
        node2.setMaterial(debugMaterial);
        node2.addControl(node2Physics);
        node2Physics.setPhysicsLocation(new Vector3f(2.5f, -4, 0f));
        node2Physics.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_01);
        // Having this line or not shouldn't matter, they should still collide
        //node2Physics.setCollideWithGroups(PhysicsCollisionObject.COLLISION_GROUP_02);
        rootNode.attachChild(node2);
        getPhysicsSpace().add(node2);

        // the floor, does not move (mass=0)
        RigidBodyControl node3Physics = new RigidBodyControl(new MeshCollisionShape(new Box(Vector3f.ZERO, 100f, 0.2f, 100f)), 0);
        Geometry node3 = new Geometry("Box", new Box(Vector3f.ZERO, 100f, 0.2f, 100f));
        node3.setMaterial(debugMaterial);
        node3.addControl(node3Physics);
        node3Physics.setPhysicsLocation(new Vector3f(0f, -6, 0f));
        rootNode.attachChild(node3);
        getPhysicsSpace().add(node3);
    }

    private PhysicsSpace getPhysicsSpace() {
        return bulletAppState.getPhysicsSpace();
    }

    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }
}

In this test case, the two spheres should collide, but they don’t. The sphere standing still is part of collision group 1 and the sphere falling is part of collision group 2, but is set to collide with collision group 1. As the docs say in the PhysicsCollisionObject class:

Two object will collide when one of the partys has thecollisionGroup of the other in its collideWithGroups set.

This does not seem to be the case. I saw this issue some time ago but haven’t had the time to submit it on github yet.

This is the method in question, it doesn’t adhere to the collision logic of the jbullet physics space:

This is the jbullet code:

Ah I see. Thanks for pointing this out, I’ll see if I can poke around later on when I have more free time.