Jump to content

[QUESTION] How to create a radar?


Tokio

Recommended Posts

In order to create a radar, you must understand how to calculate sizes in pixels. You can only do that if you understand basics. Firstly, you need you learn the difference between absolute and relative sizes. Also, I do not understand what you mean by a "cool radar". There are many things you can do for a radar. You can create 3D, you can create 2D and on top of that there are circular radars, hexagon radars, triangle radars, rectangular radars etc.

Link to comment
4 hours ago, Saml1er said:

In order to create a radar, you must understand how to calculate sizes in pixels. You can only do that if you understand basics. Firstly, you need you learn the difference between absolute and relative sizes. Also, I do not understand what you mean by a "cool radar". There are many things you can do for a radar. You can create 3D, you can create 2D and on top of that there are circular radars, hexagon radars, triangle radars, rectangular radars etc.

i want a 2D radar, and rectangular :D

Link to comment

I have a script that I worked on with a friend. It was for a radar that looked like GTA V. The code is not in proper format. There are several errors as I took the code out of some heavy lua files. Now, it's your job to make it work for yourself. The logic and everything is 100% fine. You'll just need to fix some errors which you can easily find by executing the command: "/debugscript 3". You must be logged in as admin for that. You have everything  you need to make a working radar now, go ahead and do it. Also, you can see that code looks difficult to understand. Take my advice, divide the code into functions. I'd personally rewrite it in OOP if I wanted to use it anyway functions will make it look much better than current.

 

	local sX, sY = guiGetScreenSize()
local hudScale = sX/1280
local scaleX = sX/1920 -- Used in deathlist
local scaleY = sY/1080 -- Used in deathlist

local settings =	{
					['radar'] = 		{
										['width'] = 270 * hudScale,
										['height'] = 170 * hudScale,
										['xoffset'] = 16 * hudScale,
										['yoffset'] = 60 * hudScale,
										},
					['speed'] =			{
										['font'] = dxCreateFont('fonts/rothman.ttf', 100), --custom font
										['size'] =  0.5 * hudScale,
										['xoffset'] = 16 * hudScale,
										['yoffset'] = 60 * hudScale,
										['shadow'] = 2,
										},
					['notifications'] = {
										['duration'] = 8000, -- In milliseconds
										['maxnot'] = 8,
										},
					['display'] =		{
										['width'] = 10 * hudScale,
										['height'] = 700 * hudScale,
										['xpos'] = sX - (26 * hudScale),
										['ypos'] = sY/2 - (700 * hudScale)/2,
										},
					}
local function getCameraRotation ()

    px, py, pz, lx, ly, lz = getCameraMatrix()
    local rotz = 6.2831853071796 - math.atan2 ( ( lx - px ), ( ly - py ) ) % 6.2831853071796
    local rotx = math.atan2 ( lz - pz, getDistanceBetweenPoints2D ( lx, ly, px, py ) )
    rotx = math.deg(rotx)
    rotz = math.deg(rotz)	
    return rotz
	
end

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

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


local radarEnabled = true
local speedEnabled = true
local displayEnabled = false

local lp = localPlayer
local heliRotation = 0

addEventHandler ("onClientRender", root, 
  function ()
      -- Blips
							local px, py, pz = getElementPosition(lp)
					local pr = getPedRotation(lp)
					local cx, cy, _, tx, ty = getCameraMatrix()
					local north = findRotation(cx, cy, tx, ty)
					if not pteam then return end 
		for _,player in pairs( getPlayersInArena  ( pteam )   ) do
		local state =  getElementData(player,"state")
         if state == "waiting" or state  == "alive" or state == "aboutToRun" then -- used in race gamemode, you can remove this check
						local veh = getPedOccupiedVehicle(player)
				
						if isElement(veh) and player~=lp then
							local ex, ey, ez = getElementPosition(veh)
							
							local dist = getDistanceBetweenPoints2D(px, py, ex, ey)
							if dist > range then
								dist = range
							end
local angle = 180 - north + findRotation(px, py, ex, ey)
							--local cblipx, cblipy = getDistanceRotation(0, 0, settings.radar.height*(dist/range)/2, angle)
							local cblipx, cblipy = getDistanceRotation(0, 0, settings.radar.width*(dist/range)/1.5, angle)
							local blipx = centerleft + cblipx - blipsize/2
							local blipy = centertop + cblipy - blipsize/2
							-- v -- more to do here later
							
							if blipx < (settings.radar.xoffset - blipsize/2) then
								blipx = settings.radar.xoffset - blipsize/2
							elseif blipx > (settings.radar.xoffset + settings.radar.width) - blipsize/2 then
								blipx = settings.radar.xoffset + settings.radar.width - blipsize/2
							end
							
							local bHeight  = 183 * hudScale
							local boffY = 40 * hudScale
							if blipy < (sY - boffY - bHeight) - blipsize/2 then
								blipy = sY - boffY - bHeight - blipsize/2
							elseif blipy > (sY - boffY*2) - blipsize/2 then
								blipy = sY - boffY*2 - blipsize/2
							end
							 
							-- ^ -- more to do here later
							local r, g, b, a = 255, 255, 255, 255 
                            local  pTeam = getPlayerTeam(source) 
			                if pTeam then 
                             r, g, b = getTeamColor ( pTeam )
                            end 
	                          -- you can use local r,g,b,a = getPlayerTeamColor(player) if you want
          
								local blipRot = 45 + (north-getPedRotation(player))
							local img = 'images/radar/blip.png'
							if (getElementModel(veh) == 425) then
								img = 'images/radar/hunter.png'
						
							
							end

							
			       if (img == 'images/radar/hunter.png') then
							
					if math.abs ( pz-ez ) >= 20 then 
						if pz > ez then 
						     	img = "images/radar/bldown.png"
					    elseif pz < ez then 
						        img ="images/radar/blup.png"		
						end 
					end 
							
				
	 local hlsize = ( (30 * hudScale)/ 3 )
							local cblipx, cblipy = getDistanceRotation(0, 0, settings.radar.width*(dist/range)/1.5, angle)
							local hlx = centerleft + cblipx - hlsize/2
							local hly = centertop + cblipy - hlsize/2
							-- v -- more to do here later
							
							
							if hlx < (settings.radar.xoffset - hlsize/2) then
								hlx = settings.radar.xoffset - hlsize/2
							elseif hlx > (settings.radar.xoffset + settings.radar.width) - hlsize/2 then
								hlx = settings.radar.xoffset + settings.radar.width - hlsize/2
							end
							
							local hY =  40 * hudScale
							if hly < (sY - hY - (185 * hudScale)) - hlsize/2 then
								hly = sY - hY - (185 * hudScale) - hlsize/2
							elseif hly > (sY - hY*2) - hlsize/2 then
								hly = sY - hY*2 - hlsize/2
							end
							
			
							
						 		dxDrawImage(hlx, hly, scx*12, scy*12, img, 0, 0, 0, tocolor(r,g,b,a))
							else
								
								dxDrawImage(blipx, blipy, blipsize, blipsize, img, blipRot, 0, 0, tocolor(r,g,b,a))
								
									end
			                 end
					
						end 
					end 
					
				if vehicle then 
					dxDrawImage(centerleft - lpsize/2, centertop - lpsize/2, lpsize,lpsize, 'images/radar/local.png', north-pr)
	            end
   end 
)

 

Edited by Saml1er
Link to comment

You'd be better off downloading the rectanglular radars availible on MTA resources and converting them to your own liking.
When I first opened a server, I realized that if you find a circular radar based on a .PNG as the background, you can open it in a picture editing program.
For example: Photoshop.
Once that's done just delete everything, blackout the entire size of the rectangle, add 65% or so transparency, save, exit, add to MTA, start resource, and in-game nobody will notice, and if they do they care too much..
The functionalities you're looking for instead of a rectangular radar is probably nametag blips, colored blips, etc.
Goodluck man

Edited by Justin|X5|
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...