【问题标题】:Calculating rotation(or spin)计算旋转(或自旋)
【发布时间】:2020-07-24 08:03:52
【问题描述】:

我正在查看 W3 在画布上的 ref,在一个示例中,他们创建了一个红色块并通过以下计算确定其 x,y:

this.angle += this.moveAngle * Math.PI / 180;
this.x += this.speed * Math.sin(this.angle);
this.y -= this.speed * Math.cos(this.angle);

full piece

sin 和 cos 在改变方块的 x,y 位置时起什么作用?

【问题讨论】:

    标签: javascript math html5-canvas trigonometry


    【解决方案1】:

    在该示例中,物体的速度与角度有关。
    只需尝试删除它,您就会看到会发生什么。

    代替:

    this.x += this.speed * Math.sin(this.angle);
    this.y -= this.speed * Math.cos(this.angle);
    

    就这样吧:

    this.x += this.speed;
    this.y -= this.speed;
    

    您的对象将不再沿角度方向移动。
    这只是一个简单实用的解释。


    但您应该更多地了解三角学:
    https://en.wikipedia.org/wiki/Trigonometry

    还有可汗学院有课程:
    https://www.khanacademy.org/math/trigonometry

    【讨论】:

    • 你的答案和后续链接,任何机会你都可以详细说明,为什么使用 sin 和 cos 而不仅仅是角度和 sin 一个而 cos 另一个?
    • @sroool 这是你最好自己弄清楚的东西,你需要对三角学有一个清晰的了解......你当然可以尝试任何你喜欢的其他组合,看看当你尝试时对象的行为移动它
    猜你喜欢
    • 2016-07-21
    • 2011-07-09
    • 1970-01-01
    • 2018-12-04
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多