Help with collisions

The following method detects when a collision occurs with a bullet and an enemy. When the bullet hits the enemy it reduces its life. if the life of the enemy reaches zero, the enemy is removed from the scene and another enemy appears in the scene. The problem that I am having is that sometimes the enemies destroy themselves even if I don’t fire a bullet. I also would like to calculate the Accuracy = #ofBulletsThatHitTarget / #bulletsFired; But I don’t know how to get the number of bullets that hit a target.



Public void targetHit(){

CollisionResults result1 = new CollisionResults();

CollisionResults result2 = new CollisionResults();



bullet.collideWith(enemy1.getWorldBound(), result1);

bullet.collideWith(enemy2.getWorldBound(), result2);



if(result1.size() >0){

enemy1.setUserData(“health”, enemy1.getUserData(“health”)-10);

bullet.removeFromParent();

if(enemy1.getUserData(“health”) <=0){

enemy1.removeFromParent();

enemiesDestroyed++;

score = score +100;

/*Make another enemy appear in the scene/

shootables.attachChild(makeEnemy1(“enemy1”,life));

}

}

/*Detect collision for enemy2/

if(result2.size() > 0){

enemy2.setUserData(“health”, enemy2.getUserData(“health”)-10);

bullet.removeFromParent();

if(enemy2.getUserData(“health”) <=0){

enemy2.removeFromParent();

enemiesDestroyed++;

score = score + 150;

/*Make another enemy appear in the scene/

shootables.attachChild(makeEnemy2(“enemy2”,0.007f,life));

}

}

}

I would really appreciate any help,

i might be way off here as i am still learning to code in java, and i dont know much about jME collisions yet… but just looking at your code snippet, maybe try this:



[java]

public void targetHit(){

CollisionResults result1 = new CollisionResults();

CollisionResults result2 = new CollisionResults();



bullet.collideWith(enemy1.getWorldBound(), result1);

bullet.collideWith(enemy2.getWorldBound(), result2);



if(result1.size() > 0){

//Increment ‘direct hits’ counter (declared outside of - but accessible to this method) by 1

//to compare to ‘shots fired’ counter later for accuracy

numHits++

enemy1.setUserData(“health”, enemy1.getUserData(“health”) - 10);

bullet.removeFromParent();

if(enemy1.getUserData(“health”) <= 0){

enemy1.removeFromParent();

enemiesDestroyed++;

score = score + 150;

/*Make another enemy appear in the scene/

shootables.attachChild(makeEnemy1(“enemy1”,0.007f,life));

}

}

//same thing for result2

if(result2.size() > 0){

numHits++

enemy2.setUserData(“health”, enemy2.getUserData(“health”) - 10);

bullet.removeFromParent();

if(enemy2.getUserData(“health”) <= 0){

enemy2.removeFromParent();

enemiesDestroyed++;

score = score + 150;

shootables.attachChild(makeEnemy2(“enemy2”,0.007f,life));

}

}

}

[/java]



…also, i am not really sure why you are calling getWorldBound() as your collideWith object because that seems to return a BoundingVolume which i do not believe implements the Collidable interface, but i left it in there anyway.

1 Like

Thanks for your help