# Possible to kill bullet after it has travelled a certain distance?

## Recommended Posts

Because my bullets are killed via a timer, sometimes the timed intervals differ due to acceleration etc.

Can you kill bullets after they have travelled x amount of pixels??

##### Share on other sites

Save the starting point and then get the distance it travelled.

Use the euclidian distance for this:

dist((x, y), (a, b)) = (x - a)² + (y - b)²

In your case this would be:

Math.sqrt(Math.pow(start.x - now.x, 2) + Math.pow(start.y - now.y, 2))

I am not sure about the formula, looks kinda off Oo.

But it should be like this.

##### Share on other sites

bullet.lifespan should be exact.

##### Share on other sites

You could do manual distance checking just like @SeelenGeier suggested if you have your own weapon system with varying bullet accelerations. If you are using Phaser.weapon, you could use the bulletKillDistance property:

If your game/world allows it, you could also make a dummy collision body that serves as a wall that automatically destroys bullet oncollision via callbacks.

##### Share on other sites

@Milton, what is the syntax for using the lifespan? is it milliseconds?

@samid737, I'm going to have a go at these methods tomorrow, will let you know how I go.

Thanks

##### Share on other sites

I don't use the weapon plugin, as it is just one enemy.

The main problem is the initial timing from when it goes from creating bullets to getting from the pool of bullets - so the first bullets seem to last longer than the pooled bullets...

##### Share on other sites

44 minutes ago, nicwins said:

@Milton, what is the syntax for using the lifespan? is it milliseconds?

Yes.