Making a sprite turn towards the mouse for a top-down shooter effect

Basically I am using projectile plugins to mimic a basic top-down shooter element and everything works fine but I want the character to face the direction of the mouse. I recall a long time ago when I made Flash games there was trigonometry involved and something about Math.atan2 but I can't for the life of me remember the exact code to put.

There is also another issue in that I don't know how to actually track the mouse position of the player. TouchInput.x and y only register when the mouse is clicked which is less than ideal but would at least ensure the animation is synced properly.

Appreciate any assistance!
You can get the angles like this (here I set it to a game variable). Then you can make the player turn depending what the angle is. Depends if you have 4 directional or 8 directional sprites.

const deltaX = TouchInput.x - $gamePlayer.screenX();
const deltaY = TouchInput.y - $gamePlayer.screenY();
const angle = Math.atan2(-deltaY,deltaX);
const angleDeg = angle * 180 / Math.PI;
$gameVariables.setValue(10,angleDeg);

Then you can store the hover coordinates like this with a plugin (copy paste to notepad, save as .js file and add as a plugin). Not sure if it would be good to put that hover stuff also in the initialize methods. But you can also check other plugins that add the functionality.

TouchInput._onMouseMove = function(event) {
if (this._mousePressed) {
var x = Graphics.pageToCanvasX(event.pageX);
var y = Graphics.pageToCanvasY(event.pageY);
this._onMove(x, y);
}
this._hoverX = Graphics.pageToCanvasX(event.pageX);
this._hoverY = Graphics.pageToCanvasY(event.pageY);
};

PS. Oh and in the scripts where you get the angle, you'd need to reference those hover properties then instead of TouchInput if you want the player to turn without clicking the mouse.

EDIT: You might also want to make slight adjustments to the coordinates, due to how the sprite on screen and it's x and y match.
So I'm having a little trouble understanding. The angle returns between -180 and 180 when I thought it would be 0-360 so I'm not sure how to separate the quadrants because when I get to the bottom left to top right the values go from -180 to 180 in one pixel and I can't check in between those two. math.abs?

I can do right and down directionally.
ss2.png


Additionally how would I get the hover coordinates from the .js into the in-game script? Still learning. Thank you!
Yeah that's how the program works. Haven't really done math in ages, but the angle is positive on top and negative at the bottom. So it jumps from 179° to -180°.
Top right 0-90, top left 90-180, bottom left -180 - -90, bottom right -90 - 0
So you'd need to put between something OR between something checks there for some directions. Though if you just add +180 to your variable, then it's between 0 and 360 I think, but you'd have to think how the directions go in that case.

And you can reference the hovers with TouchInput._hoverX and TouchInput._hoverYbut like I said, might be good practice to put them to value 0 in the codes even before you touch your mouse.

Original thread: https://forums.rpgmakerweb.com/threads/making-a-sprite-turn-towards-the-mouse-for-a-top-down-shooter-effect.183372/