Sign in to follow this  
frank84

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

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

 

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.