Jump to content

Sprite Positioning Issue


algut20
 Share

Recommended Posts

Hello guys,

 

My player has a sprite that follows it everywhere. In order to follow it, I read I had to use body.velocity because if the x/y coordinates are used directly it will mess up the collision detection. So everything worked fine until I ran into a particular scenario. When my player jumps on top of a moving sprite and stands still, Phaser sees my player's velocity as zero. The player has zero velocity relative to the moving sprite because its on top of it, but relative to the screen it is still moving. That makes sense, but the sprite that follows the player stops following it completely since it reads the player's velocity as zero. They begin to drift apart as the player moves away on the moving sprite. Again, I would use the x/y coordinates but I cant do that since collision detection will not work. Is there a work around for this?

Link to comment
Share on other sites

@algut20: it should not ...you can set followers velocity to move towards player. In more detail:

 

1] set your "desired" distance (DIST) between follower and player (later for optimisation you can store sqaure of it),

 

2] calculate vector player.position - follower.position (let's call it PF). It is vector from fllower to player,

3] if magnitude of PF is higher than DIST then continue with 4], otherwise in next update check at 2] again (calculating magnitude needs sqrt, so here you can check sqaure magnitude against square DIST),

4] normalize PF - so you get unit vector pointing from follower to player,

5] multiply normalized PF with desired followers speed and set its velocity to result.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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