Jump to content

How to get the forward velocity vector of a sprite


piotr
 Share

Recommended Posts

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

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

 Share

  • Recently Browsing   0 members

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