This is one way to determine the map coordinates that are under the screen cursor.
It should give you some idea to build off.
I have kept it simple and it should be easy to follow.
--[[
mapcoords.lua
How to determine the map coordinates that are under the screen cursor
A learning script by perelect.
--]]
function mapcoords_init(e)
SetGamePlayerStateCameraFov(42.2) -- tested at resolution 1366 x 768
end
function mapcoords_main(e)
-- Common Camera Variables
local Camera_X = GetCameraPositionX(0)
local Camera_Z = GetCameraPositionZ(0)
local Camera_Y = GetCameraPositionY(0)
local Camera_T = GetTerrainHeight( Camera_X , Camera_Z )
local Camera_TD = ( Camera_Y - Camera_T )
-- Screen Cursor distance from the center of the screen
if g_MouseX <= 50 then Cursor2D_X = -50 + g_MouseX end
if g_MouseX > 50 then Cursor2D_X = -50 + g_MouseX end
if g_MouseY <= 50 then Cursor2D_Y = 50 - g_MouseY end
if g_MouseY > 50 then Cursor2D_Y = 50 - g_MouseY end
-- 3D Cursor
local Cursor3D_X = ( Camera_X + (Cursor2D_X*(Camera_TD * ((( GetDeviceWidth ()/2)/1000)/50))) ); -- Map X position under the screen cursor
local Cursor3D_Z = ( Camera_Z + (Cursor2D_Y*(Camera_TD * ((( GetDeviceHeight()/2)/1000)/50))) ); -- Map Z position under the screen cursor
local Cursor3D_Y = GetTerrainHeight( Cursor3D_X , Cursor3D_Z );
-- Set the camera
SetCameraOverride ( 3 )
SetCameraAngle ( 0 , 90 , 0 , 0 )
SetCameraPosition ( 0 , 25000 , 3600 , 25000 )
-- Display values
TextColor(10,10,3,"MAP COORDINATES",255,255,255)
TextColor(10,13,3,"Cursor3D_X: "..math.floor(Cursor3D_X),255,255,255)
TextColor(10,16,3,"Cursor3D_Z: "..math.floor(Cursor3D_Z),255,255,255)
TextColor(10,19,3,"Cursor3D_Y: "..math.floor(Cursor3D_Y),255,255,255)
TextColor(10,25,3,"SCREEN COORDINATES",255,255,255)
TextColor(10,28,3,"Mouse X: "..math.floor(g_MouseX),255,255,255)
TextColor(10,31,3,"Mouse Y: "..math.floor(g_MouseY),255,255,255)
end
Desktop: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz (8 CPUs), ~3.6GHz, Windows 10, 64-bit, 16 GB Ram, NVIDIA GeForce GTX 750 Ti, Display Memory: 4018 MB. Resolution 1360x768, Passmark 3528.
Laptop: Pavilion dv6 Notebook, Intel(R) Core(TM) i5-2410M CPU @ 2.30 GHz, Win 7 64 bit, 16 GB Ram, Radeon (TM) HD 6490M, 2336 MB Memory. Resolution 1366x768, Intel(R) HD Graphics 3000. (WEI 5.8)