algut20 Posted July 24, 2015 Share Posted July 24, 2015 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 More sharing options...
semk Posted July 24, 2015 Share Posted July 24, 2015 Maybe try to see if you can put in a property that detects if the player is standing on a moving object, then make the sprite that follows the player use the moving object's velocity as the reference point. Link to comment Share on other sites More sharing options...
algut20 Posted July 24, 2015 Author Share Posted July 24, 2015 I tried that and it worked as a quick fix but other problems came up. I want to see if there is a more general solution so that I don't have to do that for all the sprites my player may come across. Link to comment Share on other sites More sharing options...
Tom Atom Posted July 24, 2015 Share Posted July 24, 2015 Hi, do not follow based on velocity, but follow based on player's position: 1) set some maximim distance your "follower" can be from your player,2) calculate distance from follower to player,3) if bigger than maximum, then move "follower" towards player Link to comment Share on other sites More sharing options...
algut20 Posted July 24, 2015 Author Share Posted July 24, 2015 @Tom Atom, but wouldnt that mess up the collision detection of the follower? semk 1 Link to comment Share on other sites More sharing options...
wayfinder Posted July 24, 2015 Share Posted July 24, 2015 Instead of x and y, you can use sprite.world.x/y, which are absolute values and not relative to its parent. Link to comment Share on other sites More sharing options...
Tom Atom Posted July 25, 2015 Share Posted July 25, 2015 @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 More sharing options...
Recommended Posts