# How can I calculate the distance a sprite will travel with velocity?

## Recommended Posts

Ok i'm kind of stuck here ... I have a a sprite that I move using the following:

`target.sprite.body.velocity.x += target.power/2;target.sprite.body.velocity.y -= target.sprite.body.velocity.x*1.5;`

I would like to know the coordinates of where the sprite will stop instantly, without having to wait for the animation of the sprite to finish.

Anyone knows how I could achieve that?

Thanks!

##### Share on other sites

Technically the sprite will continue on forever, since there's nothing stopping it in the code above. If you start to use things like drag or if it collides with something, then you've got a lot more calculating to do! If your object isn't going to collide with anything, and there's no drag, consider using a tween instead, as then you know exactly where it is going, how long it'll take and that it will definitely stop at the end exactly at the position specified.

`// turn off physics temporarilytarget.sprite.body.moves = false;// tween the object to 500, 1000 over 2 seconds, then enable physics at the endgame.add.tween(target.sprite).to({x: 500, y: 1000}, 2000, null, true)  .onComplete.add(function() {    target.sprite.body.moves = true;  }, this);`

If you need to use physics, then I have to delegate to someone with way better trigonometry skills than I have. I imagine the calculation is easy but I don't know it I'm afraid.

##### Share on other sites

Yeah unfortunately I need to use physics  . What I want to do would be very similar to the effect found in Angry Bird, where you can see a curved line over the bird when you are about to throw the bird, and that line give you an idea where your bird will land.

##### Share on other sites

Hey, check out this example on gamemechanicexplorer: http://gamemechanicexplorer.com/#bullets-5

I suspect you can tweak it to meet your needs.

##### Share on other sites

Wow that's exactly what I was looking for, thanks a lot for that!  I'll be sure to look into that and see how they do it, hopefully I can tweak it to adapt it to my game