我有一个包含精灵的玩家类,我想让它朝向鼠标指针。
我使用以下方法来计算精灵的旋转角度:
this.rotation = -(Math.atan2(this.x - mouseState.x, this.y - mouseState.y) * 180 / Math.PI);
然后在我的Player
类的绘制方法中,我正在执行以下操作:
Player.prototype.draw = function() {
window.ctx.save();
window.ctx.translate(this.x + this.sprite.width / 2, this.y + this.sprite/height / 2);
window.ctx.rotate(this.rotation);
window.ctx.drawImage(this.sprite, this.x, this.y);
window.ctx.restore();
}
它确实做了某些事情,但并不是我想要的。精灵似乎在画布的左上角(x:0, y:0
)周围疯狂地绕圈子移动。
如何使精灵以其中心点作为原点面向鼠标光标?