How to get the forward velocity vector of a sprite

Recommended Posts


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



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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.