1. Post #1
    GUNH0G's Avatar
    March 2009
    271 Posts
    Hey, I'm curious to learn how to make a curved health bar similar to in GTA IV. What would I use to produce this, DrawPoly?

    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Useful Useful x 1 (list)

  2. Post #2
    Always the vGuy.
    vercas's Avatar
    September 2011
    840 Posts
    local cos, sin, floor, clamp, deg2rad, insert = math.cos, math.sin, math.floor, math.Clamp, math.rad, table.insert
    function GeneratePartialPie(x, y, ang1, ang2, rad, thick, prec)
    	prec = clamp(prec or 256, 6, 256)
    
    	local vertices = {}
    	local ang, dist
    	local quality = floor(prec / 2)
    
    	dist = rad
    	for i = 1, quality do
    		ang = deg2rad(ang1 + (ang2 - ang1) * i / quality)
    		insert(vertices, {x = cos(ang) * dist + x, y = sin(ang) * dist + y})
    	end
    
    	dist = rad + thick
    	for i = 0, quality - 1 do
    		ang = deg2rad(ang1 + (ang2 - ang1) * (quality - i) / quality)
    		insert(vertices, {x = cos(ang) * dist + x, y = sin(ang) * dist + y})
    	end
    
    	return vertices
    end
    function GeneratePieSlice(x, y, ang1, ang2, rad, prec)
    	prec = clamp(prec or 256, 4, 256)
    
    	local vertices = {}
    	local ang
    	local quality = prec - 1
    
    	insert(vertices, {x = x, y = y})
    
    	for i = 1, quality do
    		ang = deg2rad(ang1 + (ang2 - ang1) * i / quality)
    		insert(vertices, {x = cos(ang) * rad + x, y = sin(ang) * rad + y})
    	end
    
    	return vertices
    end
    
    hook.Add("HUDPaint", "PartialPieTest", function()
    	surface.SetDrawColor(50, 155, 50, 255)												--Drawin' green.
    	
    	--surface.DrawPoly(GeneratePartialPie(ScrW() / 2, ScrH() / 2, -90, 90, 100, 10))
    	--Won't work... :-C
    
    	surface.DrawPoly(GeneratePieSlice(ScrW() / 2, ScrH() / 2, -90, 90, 105))			--Draw a filled pie.
    
    	surface.SetDrawColor(0, 0, 0, 255)													--Drawin' black.
    
    	local t = GeneratePartialPie(ScrW() / 2, ScrH() / 2, -90, 90, 100, 10)				--Build our polygon.
    
    	insert(t, t[1])																		--To ease computation, re-add the first vertex.
    
    	local oldshiz																		--This will be the vertex before the currently iterated one.
    	for _i, shiz in pairs(t) do 														--Iterate through each vertex.
    		if _i > 1 then																	--If it is NOT the first one.
    			surface.DrawLine(oldshiz.x, oldshiz.y, shiz.x, shiz.y)						--Draw a line from it to the old one.
    		end
    		oldshiz = shiz																	--This becomes the old one.
    	end
    end)

    Um, this is as far as I can help.
    There sure are better, easier ways, if the interior of our pie is going to be filled with something. (So you can draw an arch.)

    Edit:
    A little recommendation.
    Cache the polygon table if it doesn't need to be changed. It will save millions of operations per second.

    Edit #2:
    I added the function for drawing arches.
    Reply With Quote Edit / Delete Reply Windows 7 Romania Show Events Lua Helper Lua Helper x 2Winner Winner x 1 (list)

  3. Post #3
    GUNH0G's Avatar
    March 2009
    271 Posts
    That's great for the outline, however how would i make a filled version that would correspond to my health?

  4. Post #4
    Always the vGuy.
    vercas's Avatar
    September 2011
    840 Posts
    That's great for the outline, however how would i make a filled version that would correspond to my health?
    Um. You have two options:
    1. Draw more outlines inside each-other, making them thinner every time. (I would definitely not recommend this.)
    2. Draw filled pie slices. Imma come up with the code soon.

    Edit:
    I replaced the code on the other post. Check it out.