Problem with PhysicsCollisionEvent

Good night,
They are trying to identify the collision of my ball with a character.

The ID is being made, as I put a System.out.println, but syso runs several times when a collision occurs.

Sorry for my English, I’m not fluent.


public void collision(PhysicsCollisionEvent event) {

        Spatial nodeA = event.getNodeA();
        Spatial nodeB = event.getNodeB();
       if(nodeA  != null && nodeB != null){
           if(nodeA.getName().equals("ball") && nodeB.getName().equals("Ninja-ogremesh")) {
            System.out.println("Colidiu com Ninja");
        } else if(nodeB.getName().equals("ball") && nodeA.getName().equals("Ninja-ogremesh")) {
           System.out.println("Colidiu com Ninja");

The collision() method can be invoked multiple times for each collision. This is normal behavior.

Are you brazilian?

But if I want to raise a score every time the ball collides with the ninja, will not increase miscellaneous times in a single collision?


You could store a list of Ninjas that have collided with the ball once - and then loop through the list and ignore any of the additional collisions with Ninjas that were already hit.

You could then reset the list every time the ball is thrown again - or reset the list every 1 or 2 seconds so that each ninja will wait a short time before allowing another collision.

The objective of my game is to defeat all ninjas, so when the ball hits the ninja this ninja would be excused.
First was making the collision work, and then make the process of exclusion (still do not know how to do).
So if the collision is working normally if I delete the ninja there will be no more repeated collisions, right?

If you do not lose too much, would you tell me how to do the exclusion part?

Remove the object from the physicsspace and detach it from the parent node

Yes if the Ninja is no longer alive in the game, you could just remove it from the physics space and the collisions should no longer register.

In the case that your enemies have a health bar and can be hit more than once, you can exclude them from being hit again by storing them in a list after they are hit.

If that is what you mean by exclusion, then here is an example of the code that you could use to exclude an Enemy that is still alive (the Agent object in this example) from being hit a second time by the same projectile.

public class Projectile(){


  private ArrayList hitAgents<Agent>;
  public void agentCollisionDetected(Agent hitAgent){   //you would call this method when a collision occurs between the Ninja and the Ball from your game
        boolean agentHasBeenHitYet = false;
            for(int x = 0;x < hitAgents.size();x++){
                Agent agentToCheck = hitAgents.get(x);
                if(hitAgent== agentToCheck) {
                    agentHasBeenHitYet = true;

        if(agentHasBeenHitYet != true){ 
                hitAgents.add(hitAgent); //add the Agent to the list of hitAgents so it isn't hit by this projectile again

Did you solve the problem?