Jump to content

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


frank84
 Share

Recommended Posts

Ok i'm kind of stuck here :wacko: ... 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!  :D

 

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...