piobug

How to get the forward velocity vector of a sprite

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

 

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.