piotr Posted May 12, 2018 Share Posted May 12, 2018 Hi, how do you get the velocity vector of a sprite? sprite.body.velocity is phaser point or as a vector, going from the 0,0 to the sprite. What I need is a velocity vector going from the sprite onward. I need then to multiply by -1 and add it to the sprite position to get a point behind it like this image Thanks Link to comment Share on other sites More sharing options...
samme Posted May 12, 2018 Share Posted May 12, 2018 sprite.body.velocity is a velocity vector, not a position. piotr 1 Link to comment Share on other sites More sharing options...
piotr Posted May 12, 2018 Author Share Posted May 12, 2018 Thanks, that helped! Here's the code that is working for me var distanceFromTarget = 10; var targetVelocity; var pointBehindTarget = new Phaser.Point(); targetVelocity = new Phaser.Point(target.body.velocity.x, target.body.velocity.y); //make a copy of the velocity vector targetVelocity.multiply(-1,-1); //invert its direction targetVelocity.normalize(); //set its length to 1 targetVelocity.multiply(distanceFromTarget,distanceFromTarget); //scale it to the desired length pointBehindTarget = Phaser.Point.add(target.position,targetVelocity); //create a new point Link to comment Share on other sites More sharing options...
samme Posted May 12, 2018 Share Posted May 12, 2018 You can also use targetVelocity = target.body.velocity.clone(); piotr 1 Link to comment Share on other sites More sharing options...
piotr Posted May 13, 2018 Author Share Posted May 13, 2018 Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts