MZ Get Map Coordinate Under Cursor

Issue Summary —

I've been trying to get the coordinates of the tile the mouse cursor/touch point is hovering over. I plan to use this to get the region ID of the tile the mouse is hovering over for a custom menu.

Details —

I'm very rusty on my math so I'm most definitely going about this wrong, but I've iterated a few times.

I'm getting the mouse coordinates, dividing them by the tile size, then get the distance between the tiles and the player and then add the value to the player's x and y to get the x and y of the tile.
Spoiler
JavaScript:
let tile_x = Math.floor(TouchInput.x/48);
let tile_y = Math.floor(TouchInput.y/48);

let true_x = 0;
let true_y = 0;

true_x = $gamePlayer.x + $gameMap.deltaX(tile_x, $gamePlayer.x);
true_y = $gamePlayer.y + $gameMap.deltaY(tile_y, $gamePlayer.y);

$gameVariables.setValue(10, true_x);
$gameVariables.setValue(11, true_y);
$gameVariables.setValue(12, $gameMap.regionId(true_x, true_y));

The obvious problem with this is that it is using the tile calculated from screen size so the tiles are off the moment the map is not the same size as the screen.

I've also tried various ways of adding and subtracting the player's location form the tile value returned by the calculation to correct for the issue, but I've come to the conclusion I don't know what I'm doing.

For this one I played around with taking the rough location of the center of the screen (where the player would be on a large map) and used that to add or subtract from where the player was. This is just a scratch pad, I threw out the actual code I used in the engine at some point and can't find it, but is serves as a pseudo-code:

Spoiler
JavaScript:
let tile_x = Math.floor(TouchInput.x/48);
let tile_y = Math.floor(TouchInput.y/48);

let true_x = 0;
let true_y = 0;

let center_x = 13;
let center_y = 7;

if (tile_x < center_x) {
    tile_x = $gamePlayer.x - tile_x;
} else if (tile_x > center_x) {
    tile_x = (tile_x - $gamePlayer.x );
}

// Do the same for Y

true_x = $gamePlayer.x + $gameMap.deltaX(tile_x, $gamePlayer.x);
true_y = $gamePlayer.y + $gameMap.deltaY(tile_y, $gamePlayer.y);

$gameVariables.setValue(10, true_x);
$gameVariables.setValue(11, true_y);
$gameVariables.setValue(12, $gameMap.regionId(true_x, true_y));

I've been running these in a parallel process and displaying the results to the screen as I move the cursor.

What tutorials or previous support threads have you referenced? —

I looked at the Script Call List and Script Call Reference. I've looked around and could not see any threads that matched my question.

Are you using any plugins? —

I am using VisuStella's Message core to display the info I plan to get from the region ID and I am using all the core plugins from them as well, but I've been trying to do this with just vanilla RPG Maker and a script call in an event if possible. If VisuStella's plugins have this functionality already I'm unaware of them.

Thank you for taking your time to read this. Please let me know if there is more information I should give and any suggestions you have.
Not entirely certain what you mean with "the moment the map is not the same size as the screen", but in any case there are dedicated functions to change the touch coordinates to tile coordinates. So you might try:

JavaScript:
let tileX = $gameMap.canvasToMapX(TouchInput.x);
let tileY = $gameMap.canvasToMapY(TouchInput.y);

Spoiler: rmmz_objects.js; row 6461-6473
JavaScript:
Game_Map.prototype.canvasToMapX = function(x) {
    const tileWidth = this.tileWidth();
    const originX = this._displayX * tileWidth;
    const mapX = Math.floor((originX + x) / tileWidth);
    return this.roundX(mapX);
};


Game_Map.prototype.canvasToMapY = function(y) {
    const tileHeight = this.tileHeight();
    const originY = this._displayY * tileHeight;
    const mapY = Math.floor((originY + y) / tileHeight);
    return this.roundY(mapY);
};
Not entirely certain what you mean with "the moment the map is not the same size as the screen", but in any case there are dedicated functions to change the touch coordinates to tile coordinates. So you might try:

JavaScript:
let tileX = $gameMap.canvasToMapX(TouchInput.x);
let tileY = $gameMap.canvasToMapY(TouchInput.y);

Spoiler: rmmz_objects.js; row 6461-6473
JavaScript:
Game_Map.prototype.canvasToMapX = function(x) {
    const tileWidth = this.tileWidth();
    const originX = this._displayX * tileWidth;
    const mapX = Math.floor((originX + x) / tileWidth);
    return this.roundX(mapX);
};


Game_Map.prototype.canvasToMapY = function(y) {
    const tileHeight = this.tileHeight();
    const originY = this._displayY * tileHeight;
    const mapY = Math.floor((originY + y) / tileHeight);
    return this.roundY(mapY);
};
Thank you so much Canned_Dinosaur. That worked amazingly well and is much simpler than what I was trying to do.

As for what I meant about the solution only working on maps the exact size of the screen, my coordinates would be off due to how I was calculating them the top left of the screen would always be 0,0 no mater where the player was on the map.

This answers my question.
Glad to have been of help! If you haven’t already, please report the thread as solved using the ⚠️Report button!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/get-map-coordinate-under-cursor.180395/