javascript - 计算旋转(或旋转)
问题描述
我在画布上浏览 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 位置时起什么作用?
解决方案
在那个例子中,物体的速度是相对于角度的。
只需尝试删除它,您就会看到会发生什么。
代替:
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
推荐阅读
- reactjs - 如何处理来自 react-router-dom 的多个开关
- html5-video - 播放和预加载视频之间的 HTML5 优先级
- mysql - 如何查询选择一列到不同的列名
- python - 如何使用 neuraxle 实现延迟数据加载的存储库?
- apache-kafka - 如何在 Kafka Connect 中为 JSON 消息创建 SMT?
- frontend - 仅当存在时才在 .tpl smarty 文件中打印变量的正确方法
- dialogflow-cx - Dialogflow CX - 如何让 sys.no-input-1 重复而不是转到 sys.no-input-default
- php - PHP在创建对象时从数组中获取对象
- reactjs - 如何加载组件中的所有 url,并且 onclick 应该使用特定于所选项目的状态变量填充表?
- java - XSD 双重绑定到 BigDecimal