Sin Cos 创建给定方向距离的向量

如果你有一个极坐标形式的矢量(方向和距离),你会想要将它转换为带有 ax 和 y 分量的笛卡尔矢量。对于参考,屏幕坐标系具有从左到右 0 度的方向,在屏幕下方 90(PI / 2)点,依此类推方向。

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

你还可以忽略在 dir 方向上创建标准化(1 个单位长)向量的距离

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

如果你的坐标系 y 为 y,则需要切换 cos 和 sin。在这种情况下,正方向是从 x 轴逆时针方向。

// get the directional vector where y points up
var dir = 1.4536; // direction in radians
var vec = {};
vec.x = Math.sin(dir); // get the x component
vec.y = Math.cos(dir); // get the y component