首页 > 解决方案 > 计算旋转(或旋转)

问题描述

我在画布上浏览 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);

整片

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

标签: javascriptmathhtml5-canvastrigonometry

解决方案


在那个例子中,物体的速度是相对于角度的。
只需尝试删除它,您就会看到会发生什么。

代替:

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


推荐阅读