Hi guys,
I am working on a small game as my project and am using bulletmover - (Hello intersection) to move my bullet. I am trying to implement very simple mechanics where int health = 100 initially and bullet on colliding with the node of the enemy (.3ds robot) would take away 10 from health e.g. 10 shots and robot disappears(through detach)… However I have run into couple of unexpected problems…
- When I shoot even though the lifeTime of the bullet is set to 0 on collision the bullet does not disappear! I tried with walls node and it does but for these other ones(guardNode and enemyNode which contain .3ds robots) it doesnt??
- Once the health reaches 0 the objects contained in guardNode and enemyNode(.3ds robots) disappear but the node is still there(though invisible) - I shoot the bullet and through System.out it says collision detected with guardNode/enemyNode and bullets stick in those areas of their bounding… How can this be? Am I not deleting/removing node properly?
class BulletMover extends Controller
{
//private static final long serialVersionUID = 1L;
TriMesh bullet;
Vector3f direction;
float speed = 100;
float lifeTime = 5;
BulletMover(TriMesh bullet, Vector3f direction)
{
this.bullet = bullet;
this.direction = direction;
this.direction.normalizeLocal();
}
public void update(float time)
{
lifeTime -= time;
/** If life is gone, remove it */
if (lifeTime < 0 || lifeTime == 0)
{
scene.detachChild(bullet);
bullet.removeController(this);
return;
}
Vector3f bulletPos = bullet.getLocalTranslation();
bulletPos.addLocal(direction.mult(time * speed));
bullet.setLocalTranslation(bulletPos);
if(bullet.hasCollision(walls, false))
{
//logger.info("OWCH!!!");
wallImpactSound.setWorldPosition(bullet.getLocalTranslation());
wallImpactSound.play();
lifeTime = 0;
}
if(bullet.hasCollision(enemyNode, false))
{
System.out.println("Collision detected with enemyNode");
health = health - 10;
lifeTime = 0;
System.out.println("Health of the enemy is: " + health);
}
if(health == 0)
{
//enemyNode.detachAllChildren();
scene.detachChild(enemyNode);
}
if(bullet.hasCollision(guardNode, false))
{
System.out.println("Collision detected with guardNode");
health1 = health1 - 10;
lifeTime = 0;
System.out.println("Health of the guard is: " + health1);
}
if(health1 == 0)
{
//guardNode.detachAllChildren();
scene.detachChild(guardNode);
}
}
}
Finally which is the best way to destroy a node - detach or removefromParent??
Thanks,
Dev