Collision problems

We use a collision listener in our custom BulletControl class.

We use this code to check collisions (checking to see if it collides with ANYTHING):

[java] public void collision(PhysicsCollisionEvent event) {

Spatial a = event.getNodeA();

Spatial b = event.getNodeB();

checkCollision(a, b);

}



private void checkCollision(Spatial a, Spatial b) {

if ((a != null)||(b != null)) {

//Delete the bullet no matter what it hits

System.out.println(“Bullet collided”);

markedForDeletion = true;

}

}[/java]



And this code to remove it on update:

[java] @Override

public void update(float tpf) {

super.update(tpf);

space = getPhysicsSpace();

timeToLive -= this.getLinearVelocity().length() * tpf;

if (timeToLive <= 0 || markedForDeletion) {



if (this != null) {

space.removeCollisionListener(this);

space.remove(this);

bullet.kill();

Main.get().getRootNode().detachChild(this.spatial);

}



}

}[/java]



But the bullets often aren’t removed and we keep getting a collision message in the console. What’s wrong?

How should anybody know? What does the kill() method do? Do you get the right spatial? Where is the boolean stored?

The boolean “markedForDeletion” is stored in our BulletControl class. The kill method makes a call to remove the associated Bullet object with this BulletControl object to remove it from our GameLogic object’s ArrayList of Bullets.

So you don’t remove it from the PhysicsSpace.

We are only supposed to remove the control from physics space right? The call to space.remove(this) removes the BulletControl (which is an extension of RigidBodyControl).

Right, well I don’t know, where do you add the CollisionListener? Removing definitely works, theres gotta be some issue in your code…

Add the collision listener in the BulletControl class

Got it. We were having a problem specifically with the Player having bullets bounce off him, and that was resolved by adding a Node to have him recognized. Thanks! :slight_smile:

Bullets that bounce off the player sounds just right if your player is wearing a cape. :wink: