Sin / cos to create a vector given direction distance

suggest change

If you have a vector in polar form (direction and distance), convert it to a cartesian vector with an x and a y component. On a screen, 0 radians points from left to right, Math.PI / 2 points down, and the angle increases clockwise.

const dir = 1.4536; // direction in radians
const dist = 200; // distance
const vec = {};
vec.x = Math.cos(dir) * dist; // get the x component
vec.y = Math.sin(dir) * dist; // get the y component

You can also ignore the distance to create a normalised (1 unit long) vector in the direction of dir

const dir = 1.4536; // direction in radians
const vec = {};
vec.x = Math.cos(dir); // get the x component
vec.y = Math.sin(dir); // get the y component

With y pointing up and a positive angle measured counterclockwise from the x axis, use the same pair of calls. That convention matches clockwise angles on a y-down screen. If y points up but the angle is still clockwise, negate the sine instead of swapping sine and cosine.

// y up, counterclockwise from the positive x axis
const dir = 1.4536; // direction in radians
const vec = {};
vec.x = Math.cos(dir);
vec.y = Math.sin(dir);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents