我试图创建一个简单的游戏,其中中心的矩形始终指向鼠标。出于某种原因,当我使用Math.atan2时,我得到的奇怪值永远不会超过3的角度...
Here's the sketch with my code
function onMouseMove(event) {
var dx = event.point.x - view.center.x;
var dy = event.point.y - view.center.y;
var rotation = Math.atan2(dy, dx);
console.log(rotation);
player.rotation = rotation;
}
如果有人能够发现我做错了什么,甚至编辑草图以使其有效,那就太棒了。)
答案 0 :(得分:1)
这里有三件事:
applyMatrix: false
atan2
乘以180 / Math.PI
player.center
而不是view.center
以下是您可以使用的代码:
var player = new Path.Rectangle({
strokeColor: '#222222',
strokeWidth: 10,
fillColor: '#777777',
size: [100, 80],
center: view.center,
applyMatrix: false
});
function onMouseMove(event) {
var dx = event.point.x - player.center.x;
var dy = event.point.y - player.center.y;
var angle = Math.atan2(dy, dx) * 180 / Math.PI;
console.log(angle);
player.rotation = angle;
}