Jump to content

From world coords to minimap coords


Recommended Posts

Hi . I want to know how to calculate coords on the minimap (F11) from world coords.

I found something like this :

local x, y = getElementPosition(player) 
x = math.floor((x + 3000) * 1024 / 6000) + 10 
y = math.floor((3000 - y) * 1024 / 6000) - 10 

Thanks in advance .

Link to comment
local x, y = getElementPosition(player)   
    x = x/6000*resx -- Resx Put how big your mini map is in X 
    y = y/6000*resy  -- Resy Put how big your mini map is in y 

Not tested at all but in theory it should work.

6000x600 is the size of SA so

You get your players location and you divide it by 6000, giving you value in-between 0 and 1 (Bigger if out of bounds)

Then you multiply that value by the size of your map. (Pixels in this case) thus accurately giving you the location hopefully.

But you also have to take into account a stretched picture.

Meaning if your picture is at all stretched, you have to play around with the ResX and Res Y values a bit.

Link to comment
   local x, y = getElementPosition(player)   
    local sx,sy = guiGetScreenSize() 
    local Resx = "Put X here" 
    local Resy = "Put Y here" 
  
    x = (x/6000)*Resx*(1*sx) 
    y = (y/6000)*Resy *(1*sy) 

Did you mean 1/sx and 1/xy ?

Resx ,Resy = x , y from minimap ?

Link to comment
  • Discord Moderators

Do you guys know about the existence of getPlayerMapBoundingBox? It is a MTA: SA clientside function useful for this kind of calculations.

By using that function you can simply and accurately get the absolute screen coordinates (i.e. pixel positions) from world coordinates as it follows:

local x, y = getElementPosition(localPlayer) -- Could be any other world position. Note that we only need X and Y (map is 2D) 
local mx, my, Mx, My = getPlayerMapBoundingBox() -- Returns the screen coordinates of the world coordinates (-3000, -3000) and (3000, 3000) 
  
if not mx then 
    -- Do not continue if map is not showing 
    return 
end 
  
-- Calculate a factor in range [0, 1] which represents the relative distance to the max coords point from the min coords point of the given coordinates 
local fMx, fMy = (x + 3000) / 6000, (y + 3000) / 6000 
-- Do the opposite thing for the min coords point 
local fmx, fmy = 1 - fMx, 1 - fMy 
  
-- Use the factors and given screen points to calculate the final screen coords of our element in the map 
local sx, sy = (fmx * mx) + (fMx * Mx), (fmy * my) + (fMy * My) 

Please note that the code snippet I posted does not account for out of bounds coordinates and off-screen positions.

Link to comment
  • Discord Moderators

Just a heads-up: I have recently discovered that getPlayerMapBoundingBox returns the screen coordinates of the world coordinates (-3000, 3000) and (3000, -3000), not (-3000, -3000) and (3000, 3000). It seems that the wiki was wrong, so the code I posted is wrong too because it accounts for different reference points.

To fix my code, you have to replace this calculation:

local fMx, fMy = (x + 3000) / 6000, (y + 3000) / 6000 

With this one:

local fMx, fMy = (x + 3000) / 6000, -(y - 3000) / 6000 

I strongly recommend you to change that if you wondered why my code did calculate wrong positions.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...