Jump to content

Calculate radararea on radar


PlayRoom

Recommended Posts

Hi, I have problem with my script, i need help with calculating the values for dxDrawRectangles.

Currently the code looks like this (I paste only required lines):

local sW, sH = guiGetScreenSize()
local mapSize = {W = 3072, H = 3072}
local minimapSize = {W = 249, H = 180}
local renderData = {}
renderData.bg = 
{
	X = 5,
	Y = sH - 220,
	W = 285,
	H = 214
}
renderData.mapTarget = 
{
	X = renderData.bg.X + 18,
	Y = renderData.bg.Y + 12,
	W = minimapSize.W,
	H = minimapSize.H
}

renderData.pBg = 
{
	X = 45,
	Y1 = sH - 251 - 10 - 90 - 30,
	Y2 = sH - 251 - 10 - 30,
	W = 217,
	H = 93
}

local function findRotation(x1,y1,x2,y2) 
	local t = -math.deg(math.atan2(x2-x1,y2-y1))
	if t < 0 then t = t + 360 end
	return t
end

local function getPointFromDistanceRotation(x, y, dist, angle)
	local a = math.rad(90 - angle)
	local dx = math.cos(a) * dist
	local dy = math.sin(a) * dist
	return x + dx, y + dy
end

function renderRadar()
    local pXonMap = minimapSize.W / 2 - (pX / (6000 / mapSize.W))
	local pYOnMap = minimapSize.H / 2 + (pY / (6000 / mapSize.H))
    local rX, rY, rZ = getElementRotation(localPlayer)
    local camX, camY, camZ = getElementRotation(getCamera())
	local lB = renderData.bg.X + 22
	local rB = renderData.bg.X + renderData.bg.W - 22
	local tB = renderData.bg.Y + 18
	local bB = renderData.bg.Y + renderData.bg.H - 30
	local cX = (rB + lB) / 2
	local cY = (tB + bB) / 2

	local areas = getElementsByType("radararea")
	for k,v in ipairs(areas) do 
		if(getElementDimension(v) == getElementDimension(localPlayer)) then
			if(getElementInterior(v) == getElementInterior(localPlayer)) then
				local aX, aY = getElementPosition(v)
				local aW, aH = getRadarAreaSize(v)

				local rX, rY = aX+aW, aY+aH 

				local dist = getDistanceBetweenPoints2D(pX, pY, aX, aY)
				local dist2 = getDistanceBetweenPoints2D(pX, pY, rX, rY)

				local mapDist = dist / (6000 / ((mapSize.W + mapSize.H) / 2))
				local mapDist2 = dist2 / (6000 / ((mapSize.W + mapSize.H) / 2))
				
				local rot = findRotation(aX, aY, pX, pY) + 180

				local pointX, pointY = getDistanceRotation(0, 0, 100*(dist/mapDist)/2, rot)
				pointX, pointY = math.max(lB, math.min(rB, pointX)), math.max(tB, math.min(bB, pointY))

				local rot2 = findRotation(rX, rY, pX, pY) + 180

				local pointW, pointH = getDistanceRotation(0, 0, 100*(dist2/mapDist2)/2, rot2)
				pointW, pointH = math.max(lB, math.min(rB, pointW)), math.max(tB, math.min(bB, pointH))
				if (pointX ~= pointW and pointY ~= pointH) then 
					local rW, rH = pointW - pointX, pointH - pointY 
					local r,g,b,a = getRadarAreaColor(v)
					dxDrawRectangle(pointX, pointY, rW, rH, tocolor(r,g,b,a))
				end
			end
		end
	end
end

addEventHandler("onClientRender", root, renderRadar)

And SS how it looks: 

https://i.imgur.com/uCO9UOk.jpg

And how should it looks:

wN3hO1d.jpg

Link to comment

Yeah, then how it looks: https://i.imgur.com/Cvt7xf9.gifv 

	local areas = getElementsByType("radararea")
	for k,v in ipairs(areas) do 
		if(getElementDimension(v) == getElementDimension(localPlayer)) then
			if(getElementInterior(v) == getElementInterior(localPlayer)) then
				
				local _aX,_aY = getElementPosition(v)
				local _aW, _aH = getRadarAreaSize(v)
				local range = (mapSize.W + mapSize.H)/2
				local dist = getDistanceBetweenPoints2D(pX, pY, _aX, _aY)
				local mapDist = dist / (6000 / ((mapSize.W + mapSize.H) / 2))
				
				local aW, aH = _aX+_aW, _aY+_aH

				local rot = camZ - findRotation(pX, pY, _aX, _aY)
				local pointX, pointY = getDistanceRotation(cX, cY, mapDist, rot)

				pointX = math.max(lB, math.min(rB, pointX))
				pointY = math.max(tB, math.min(bB, pointY))
				local _rot = camZ - findRotation(pX, pY, aW, aH) 

				local _dist = getDistanceBetweenPoints2D(pX, pY, aW, aH)

				local _mapDist = _dist / (6000 / ((mapSize.W + mapSize.H) / 2))
				local pointW, pointH = getDistanceRotation(cX, cY, _mapDist, _rot)
				pointW = math.max(lB, math.min(rB, pointW))
				pointH = math.max(tB, math.min(bB, pointH))
				if (pointX ~= pointW and pointY ~= pointH) then 
					local r,g,b,a = getRadarAreaColor(v)
					pointW,pointH = pointW - pointX, pointH - pointY
					dxDrawRectangle(pointX, pointY, pointW, pointH,tocolor(r,g,b,a))
				end
			end
		end
	end

 

 

Link to comment

The main problem you're having is that the dx rectangle does not rotate. You need to to align with the map. The easiest way to do this is to render the map in a rendertarget (RT), and render radar areas directly onto that (without any width or height transformations), then render the RT with the correct rotation in the corner (perhaps alpha-masked via a shader as well), and above that, the blips.

Link to comment
13 hours ago, Investor said:

The main problem you're having is that the dx rectangle does not rotate. You need to to align with the map. The easiest way to do this is to render the map in a rendertarget (RT), and render radar areas directly onto that (without any width or height transformations), then render the RT with the correct rotation in the corner (perhaps alpha-masked via a shader as well), and above that, the blips.

Okay, but main problem is 'How to caluclate this areas to RT', can you help me with calculations?

Tried that way, but nothing appears on radar:

dxSetRenderTarget(renderTarget, true)
	-- Render
		dxDrawRectangle(0, 0, minimapSize.W, minimapSize.H, tocolor(110, 158, 204, 255 * alpha), false)
		dxDrawImage(pXonMap - mapSize.W / 2, pYOnMap - mapSize.H / 2, mapSize.W, mapSize.H, "client/files/map.jpg", camZ, (pX / (6000 / mapSize.W)), -(pY / (6000 / mapSize.H)), tocolor(255, 255, 255, 255 * alpha))
		local areas = getElementsByType("radararea")
		for k,v in ipairs(areas) do 
		if(getElementDimension(v) == getElementDimension(localPlayer)) then
			if(getElementInterior(v) == getElementInterior(localPlayer)) then
				
				local _aX,_aY = getElementPosition(v)
				local _aW, _aH = getRadarAreaSize(v)				
				local aW, aH = _aX+_aW, _aY+_aH
				local r,g,b,a = getRadarAreaColor(v)
				dxDrawRectangle(minimapSize.W /2 - (_aX / (6000 / mapSize.W)) - mapSize.W/2, minimapSize.H/2 + (_aY / (6000 / mapSize.H)) - mapSize.H/2, 200, 200,tocolor(r,g,b,a))

			end
		end
	end
		-- Render - koniec
	dxSetRenderTarget()

 

Link to comment

dxDrawImage on line 4 is rotated, but dxDrawRectangle on line 14 isn't (and cannot). This can be overcame by using another rendertarget, or by rotating the whole RT with radarareas in it, instead of just the map image. Former solution requires two RTs, latter solution will not look good without alphamasking or something. A third, possible solution would be to use dxDrawPrimitive or dxDrawMaterialPrimitive (not sure whether the first one fills or not) instead of rectangles. Then you just need to calculate the positions of the 4 vertices (not so complicated trigonometry) instead of trying to rotate them.

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