I have a player class with a sprite, and I want to make it face the mouse pointer.
I use this to determine if a sprite should rotate:
this.rotation = -(Math.atan2(this.x - mouseState.x, this.y - mouseState.y) * 180 / Math.PI);
and then in my drawing method of my Player class, I do this:
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(); }
he is doing something, but not what I want. The sprite seems to move wildly in a circle in the upper left corner of the canvas ( x:0, y:0 ).
How can I make a sprite face to the mouse cursor, also using its center point as a source?
source share