Problem detecting collision between a MeshShape and a Sphere

Greetings, everyone!

I’m having a problem with a mesh shape using a GhostControl, when it collides with a sphere using a RigidBodyControl, multiple collisions are detected. This is the collision code:

public void collision(PhysicsCollisionEvent event){
if(event.getNodeA().getName().equalsIgnoreCase("Player geometry")){
   if(event.getNodeB().getName().equalsIgnoreCase("Coin")){
        event.getNodeB().getControl(GhostControl.class).setEnabled(false);
        rootNode.getChild("Coin").removeFromParent();
    }
} else if(event.getNodeB().getName().equalsIgnoreCase("Player geometry")){
   if(event.getNodeA().getName().equalsIgnoreCase("Coin")){
        event.getNodeA().getControl(GhostControl.class).setEnabled(false);
        rootNode.getChild("Coin").removeFromParent();
    }
}}

As you can see, after the first collision, it should remove the ghost control, thus disabling the “Coin”. But that isn’t what happens, it detects 3 to 4 collisions and because of that it throws a NullPointerException, since there’s only one “Coin” in rootNode. The “Coin” collision shape is made using CollisionShapeFactory.createMeshShape .

I also tried using a RigidBodyControl with 0 mass in the place of GhostControl. No sucess.

hmm… yes, a timing solution…

the easiest thing I can think of is to first set a “skip checking me” type variable, to safely remove the Coin from further checking.
And then second, rather than immediately removing it from the Root Node as your code does… set that:
rootNode.getChild(“Coin”).removeFromParent();
inside of a EventQueue.InvokeLater(new Runnable(){ }) type situation so that the null pointer problem doesn’t happen immediately. The first bit of advice is more important, though.

I don’t know about the wisdom of this line of code:
event.getNodeB().getControl(GhostControl.class).setEnabled(false);

It doesn’t look like you are removing it from the node… just disabling it… what happens when you remove what it is attached to? Memory Leak? Null pointer exception?

Hope this helps,
Charles

1 Like

Depending on your actual framerate there can be multiple updates of the physics space but you’ll be informed about the collision events in the normal update loop so there can be multiple collision events in the queue. and @Relic724, PhysicsControl.enable(false) removes the control from the physics space so its okay using that.

1 Like

Awesome! Noted. :smile:

edit: oh wait, but did he attach the control to “coin”? I know that Garbage collection will eventually come along and grab it if nothing is pointing to the control… I was just making sure that the control wasn’t trying to point to something that wasn’t there, anymore. Timing of stuff is still tough for me, until I can shake more green off for threading situations.

Thank you @Relic724 and @normen for your replies!

@Relic724 I added a “removeMe” var into the Coin’s model, checking if it’s true of false before removing it from the rootNode, and it’s now working perfectly. This will do for now, thank you for your help!

@normen I’m running the game on ~1500 fps. (It’s just a test, with low res textures and low poly models.)

Hey, glad to be of help.

+1 exp level for ya! :smile:

1 Like