javascript - 根据速度确定运动矢量的方向

标签 javascript vector 2d angle

我对这个有点困惑。

我有一个物体,我知道它在 x 轴和 y 轴上的速度。我的问题是如何确定它移动的 Angular 。

function Object(){
    this.velocity = {x: 5, y: 1};
}

基本上我知道矢量的方向是 x_projectioncos(deg) + y_projectionsin(deg),但我不知道如何获得这些投影,因为我只有速度,因为我说我真的很困惑。

#编辑:

除了已接受的答案之外,以下是我为获得完整的 360 度光谱所做的事情

var addDeg = 0;
if(obj.velocity.x<0)
    addDeg = obj.velocity.y>=0 ? 180 : 270;
else if(obj.velocity.y<=0) addDeg = 360;

deg = Math.abs(Math.abs(Math.atan(obj.velocity.y/obj.velocity.x)*180/Math.PI)-addDeg)

最佳答案

I don't know how to get those projections since I only have the velocity

实际上,您似乎缺少的是您已经有了预测。这就是 x 和 y。

x 速度 * cos( Angular )

y 速度 * sin( Angular )

所以 y/x = sin( Angular )/cos( Angular ),即 tan( Angular ),所以 Angular =arctan(y/x)。

这是从x轴开始逆时针旋转的 Angular (x指向右,y指向上)。

关于javascript - 根据速度确定运动矢量的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22421054/

相关文章:

javascript - 理解 d3 中的 Promise

r - 获取具有非唯一名称的命名向量值

java - 需要有关碰撞处理的建议

c++ - 如何使用 wxWidgets 和 C++ 绘制单色图像,然后将其 "dump"转换为 vector ?

graphics - 标准化设备坐标

3d - 将3D位置转换为2d屏幕位置[r69!]

javascript - 是否有 iTunes javascript api

javascript - float 元素 : Fill out the bottom line first

javascript - 在宽度变化时保持可滚动内容的位置

c++ - vector::erase with pointer 成员