Not assigned CollisionGroup in collision


I am trying to optimize my collision detection.
My setup is like this:
G1 is the Terrain/Floor
G2 for perception of Mobs
G3 player and mobs
G4 for items

A mob/enemy has two controls:
CharackterControl is in G3 and collides with G1
GhostControl(perception) is in G2 and should colide with G3 only

In my PhysicsCollisionGroupListener an collision between enemy and enemy shows up where one of the PhysicsCollisionObject has G4 which i never assigned.

Here is my code:


private Vector3f walkDirection;
private Geometry geo;
private CharacterControl character;
private GhostControl perception;
public SimpleEnemy(Vector3f position) {
CapsuleCollisionShape collsionShape = new CapsuleCollisionShape(0.5f, 1f);
walkDirection = new Vector3f();
character = new CharacterControl(collsionShape, 0.01f);


    geo = new Geometry("EnemyBox", new Box(0.5f, 1f, 0.5f));


    perception = new GhostControl(new SphereCollisionShape(5.0f));



public class PerceptionCollisionListner implements PhysicsCollisionGroupListener {
public boolean collide(PhysicsCollisionObject nodeA, PhysicsCollisionObject nodeB) {
System.out.println(“perception” + nodeA.getCollisionGroup() + " " + nodeB.getCollisionGroup());
System.out.println(“perception” + ((Spatial)nodeA.getUserObject()).getName() + " " + ((Spatial)nodeB.getUserObject()).getName());

space.addCollisionGroupListener(new PerceptionCollisionListner(),PhysicsCollisionObject.COLLISION_GROUP_02);


perception4 2 perceptionEnemyBox EnemyBox

Why has the enemy a collisiongoup 4?

Huh? You know how bitmasks work? Also you are aware that your character is still in collision group 1? Read the javadoc of the collisiongroup and collidewith methods again, you have some misunderstanding.

You mean it still collides with group 1, right ?
Because setCollisionGroup will overwrite the mask. So it should be in group3 only(!).

I didn’t got that the collidewithgroups mask was unaffected by this.

Why are you adding two physics controls to the same object ???
(lines 17 and 24)

I’m surprised with the group 4, but not surprised it fails!

One is the actual physicalbody of the character the other one should be its field of view.
Is there a better way to do it?
Like link the position of the field of view to the position of the character or something like that?

Ok, I understand better. But I really don’t know if this is the way to go.

Idk. Is the sphere collision shape a volume or a surface ?
If this is a volume, this collision is normal. Your character sees itself since he’s inside the sphere.

Yup that i already knew my problem was the usage of system.out on the constant.
public static final int COLLISION_GROUP_03 = 0x00000004;[/java]
Which results in an output of 4 which i missinterpreted as COLLISION_GROUP_04.

Sphere collision shape is a volume.

How to use bitmask in order to collide with several groups?


Here the balls do not collide with the level. But in the following code the balls do collide


But do the balls also collide with weapons?

public static final int COLLISION_GROUP_LEVEL = 2;
public static final int COLLISION_GROUP_BALLS = 3;
public static final int COLLISION_GROUP_WEAPONS = 4;

Its bit masks @mathias4u, 1,2,3,4 is wrong, its 1,2,4,8 etc.

mhh, ok. I changed the int values of my constants. But I don’t get the idea :frowning:

The problem still depends on the sequence of setCollideWithGroups() calls.

Is there any example with more than collisiongroup 1 and 2?

If you set it you will overwrite the values, add the groups. Bit masks are well described on wikipedia if you don’tknow how they work. The collision groups are described in jme’s javadoc.

You should use a bit mask.
Group 1 is bit 0, Group 2 is bit 1, etc. …

Thus, if you want groups 1 and 2 and 3 , you use the bit mask 000…0111 => 7 == 1 | 2 | 4

1 Like

The Javadoc says:

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

How is that evaluated, that an int value (collisionGroup) is within a bit mask (collideWithGroup)??

just test if the bit is set : group & mask != 0

1 Like

Like I said, its well-explained on the web and a concept you should try t grasp in general if you want to improve as a programmer:

group & mask !=0 is the most important information when building collision groups. Thank you @yang71!