Trying to prevent certain objects from being collidable in a mesh collision shape

In my game world that I create a mesh collision shape for, I have certain objects that I would prefer not to be collidable. I stumbled upon the UserData.JME_PHYSICSIGNORE key and tried to implement its use. This is what I have:



[java]for(int i = 0; i < rawWorld.getChildren().size(); i++) {

if (rawWorld.getChild(i).getName().startsWith(“RL_”)) {

rawWorld.getChild(i).setUserData(UserData.JME_PHYSICSIGNORE, true);

for(int j = 0; j < ((Node)rawWorld.getChild(i)).getChildren().size(); j++) {

((Node)rawWorld.getChild(i)).setUserData(UserData.JME_PHYSICSIGNORE, true);

}

}

}

[/java]



This code goes through the node and sets the JME_PHYSICSIGNORE flag on all the children that start with “RL_” (these are lane objects that should not be collidable but still visible for keeping the cars within their respective lanes). I also set the flag on the children of these objects to ensure that the flag applied to the geometries of each object. Note that this is done before createMeshShape() is called.



I check to see if the flag was applied correctly with this code:



[java]

for(int i = 0; i < rawWorld.getChildren().size(); i++) {

Boolean bool = rawWorld.getChild(i).getUserData(UserData.JME_PHYSICSIGNORE);

if (bool != null && bool.booleanValue()) {

System.out.println("Should be ignoring " + rawWorld.getChild(i).getName());

}

}

[/java]



The checking code was taken from CollisionShapeFactory:

[java]



Boolean bool = spatial.getUserData(UserData.JME_PHYSICSIGNORE);

if (bool != null && bool.booleanValue()) {

continue; // go to the next child in the loop

}



[/java]



And this code works, as it outputs that it should be ignoring all the objects that start with “RL_”.



However, the “RL_” objects are still collidable as they show up in the debug view and cause my physics car to roll over them like they are speed bumps.



Also, I tried to call getChildren().size() on my collision shape when I set the flag only on the “RL_” objects and when I set the flag on all the objects and got 42 both times.



So it seems that the flag is somehow not being recognized by CollisionShapeFactory or somehow still being incorporated into the collision shape. Am I setting the flag correctly, or is there a deeper issue?

Just make your CompoundShape by hand then. The CollisionShapeFactory does nothing magic, its just for combining all geometry in a tree.

Well after some debugging I figured out that CollisionShapeFactory only looks at the lowest level objects (leaf nodes) when checking the physics flag. I was setting the flag on their node parents, thinking that it would affect the children as well, but that’s not how CollisionShapeFactory works.



So all I did was set the flag on all the node’s children as well as the node itself and the problem was fixed!



I also realized that I was initially setting the flag on the children incorrectly; the line

[java]

((Node)rawWorld.getChild(i)).setUserData(UserData.JME_PHYSICSIGNORE, true);

[/java]

should be

[java]

((Node)rawWorld.getChild(i)).getChild(j).setUserData(UserData.JME_PHYSICSIGNORE, true);

[/java]

This PHYSICSIGNORE is not part of the official api, its not guaranteed to stay.