Jump to content

How to calculate from world coord to screen pos


XJMLN

Recommended Posts

Hi, I wanna create a gui with minimap (like this), but i don't know how to calculate world position to screen position and show it on that map.

I tried this: 

	x2 =((x2 + 3000)/ 6000 *sw)
	y2 =((3000 - y2)/ 6000 *sh)

But this show the wrong position for waypoint.

Code:

local sw,sh = guiGetScreenSize()
local mw, mh = sw/1920, sh/1080
local t = {point=nil,pos=nil,map=false}

t.gridlist = guiCreateGridList(mw*382, mh*281, mw*329, mh*530, false)
t.gridlist_places = guiGridListAddColumn(t.gridlist, "Places", 0.9)
guiSetVisible(t.gridlist,false)

function travel_render()
	if not t.rendered then return end
	dxDrawImage((sw - sw*1200/1920)/2,(sh - sh*600/1080)/2,1200,600, "img/background.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
	if t.map then 
		dxDrawImage(t.pos[1],t.pos[2],32,32,"img/waypoint.png", 0,0,0, tocolor(255,255,255,255), false)
	end
end

function travel_fill(d)
	for i,v in ipairs(d) do
		local row = guiGridListAddRow(t.gridlist)
		guiGridListSetItemText(t.gridlist, row, t.gridlist_places, v.name, false, false)
		guiGridListSetItemData(t.gridlist, row, t.gridlist_places,v.tpPos)
	end
end

function travel_init()
	if (t.rendered) then 
		removeEventHandler("onClientRender", root, travel_render)
		t.point = nil
		t.rendered = false
		guiSetVisible(t.gridlist,false)
		showCursor(false)
		guiGridListClear(t.gridlist)
	else
		guiGridListClear(t.gridlist)
		triggerServerEvent("travel_requestPlaces", localPlayer)
		t.rendered = true
		addEventHandler("onClientRender", root, travel_render)
		guiSetVisible(t.gridlist, true)
		showCursor(true)
	end
end

function interaction_fastTravel(args)
	if (not args.ped) then return end
    args.player = localPlayer
    local x,y,z = getElementPosition(args.player)
    local x2,y2,z2 = getElementPosition(args.ped)
    if (getDistanceBetweenPoints3D(x,y,z,x2,y2,z2)>2) then
        outputChatBox("You need to go closer.",255,0,0)
        return
    end

    t.point = args.ped 
    travel_init()
end

function travel_updateBlip()
	local selRow = guiGridListGetSelectedItem(t.gridlist)
	if (selRow<0) then return end 

	local pos = guiGridListGetItemData(t.gridlist, selRow, t.gridlist_places)
	local x2, y2, z2 = unpack(pos)
	x2 =((x2 + 3000)/ 6000 *sw)
	y2 =((3000 - y2)/ 6000 *sh)
	t.pos = {x2,y2}
	t.map = true
end

function travel_doTeleport()
	local selRow = guiGridListGetSelectedItem(t.gridlist)
	if (selRow<0) then return end 

	local pos = guiGridListGetItemData(t.gridlist, selRow, t.gridlist_places)
	local x2, y2, z2 = unpack(pos)
	fadeCamera(false)
	travel_init()
	setElementFrozen(localPlayer,true)

	setTimer(function() 
		setElementPosition(localPlayer,x2,y2,z2+1)
		setElementFrozen(localPlayer,false)
		fadeCamera(true)
	end,1500,1)
end

addEvent("travels_fillList", true)
addEventHandler("travels_fillList", root,travel_fill)
addEventHandler("onClientGUIClick", t.gridlist, travel_updateBlip)
addEventHandler("onClientGUIDoubleClick", t.gridlist, travel_doTeleport)

Anyone can help with this? :D

Link to comment
local sX, sY = guiGetScreenSize()
local mapW, mapH = 500, 500
local mapX, mapY = (sX-mapW)/2, (sY-mapH)/2
local pixelW = mapW/6000
local pixelH = mapH/6000

local testX, testY = 0, 0

function calculateScreen(inX, inY)
	return (inX+3000)*pixelW, math.abs((inY-3000))*pixelH
end
wX, wY = calculateScreen(testX, testY)
outputChatBox(wX .. " " .. wY)

Try this.

Edited by NeXuS™
  • Thanks 1
Link to comment

you can use

function map(value, low1, high1, low2, high2)
    return low2 + (high2 - low2) * (value - low1) / (high1 - low1)
end

local x, y = map(player_x, 0, world_w, 0, map_w), map(player_y, 0, world_h, 0, map_h)

replace world_w and world_h with gta 3d world size and replace map_w and map_h with your 2d map x and player_x and player_y is player position

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...