Jump to content

Physics: stopping a moving sprite without an x/y destination


ForgeableSum
 Share

Recommended Posts

In my game I'm stopping sprites that are moving in the normal way, or at least the most common method I've read about here on the forums. In the update function, when a sprite reaches its location, stop its velocity by setting x/y to 0. This works just fine but only if you know exactly where the sprite will end up (or a close proximity to that). 

 

What I've been considering is a more exact approach. Has anyone ever tried something like this:

 

* move sprite to a location

* calculate the number of pixels (approximately) the sprite would have to travel across to reach that destination. 

* in the update function, if the sprite has traveled x number of pixels, stop it. 

 

 

This as opposed to stopping it when it reaches a certain x/y. The advantage of this would be that if the sprite misses the x/y, it will still stop near the original destination. So in short: stopping it when it travels a certain number of pixels as opposed to stopping it when it is located at a certain x/y (the destination). 

 

Has anyone tried this? any thoughts on how to implement it?

 

I guess I've had a lot of trouble with stopping sprites that are moving. I'm looking to explore other ways to stop movement as there is nothing built-in to phaser to do so. 

Link to comment
Share on other sites

I've had great success with the following method (summarized):

 

- calculate the time it will take based on the distance the sprite is moving and the speed

- when the time has elapsed, stop the sprite

 

This seems to work a lot better than stopping the sprite when it reaches a certain x/y. If you have a separation function, or pathfinding, for example, the sprite might be slightly off its original trajectory. The timer eliminates those complications. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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