Determine pos of a vector with distance

2012-09-05

Say you have an origin and a bearing/heading (in radians). Say you want to determine the target position if you would move some distance into that direction. How would you do it?

I can't explain this but I can tell you the simple way of doing it:

Code:
var origin = {x:50, y:100};
var heading = -Math.PI * 0.2; // just some random direction, in Radians!
var distance = 20;
var target = {
x: origin.x + (Math.cos(heading) * distance),
y: origin.y + (Math.sin(heading) * distance)
};

So in a nutshell, for x take the cosine of the heading and multiply that by distance. For y do almost the same, except with the sine. Don't forget to add the result to the origin :) This will move you exactly distance units from the origin to the target in the heading you specified.

Note: Bearing is where you point to, heading is where you move to. These terms are sometimes interchanged, but that's what they ought to mean :)

Hope it helps you!