Garry's Mod Wiki

Revision Difference

ents.FindInCone#566207

<function name="FindInCone" parent="ents" type="libraryfunc"> <description> Finds and returns all entities within the specified cone. Only entities whose <page>Entity:WorldSpaceCenter</page> is within the cone are considered to be in it. The "cone" is actually a conical "slice" of an axis-aligned box (see: <page>ents.FindInBox</page>). The image to the right shows approximately how this function would look in 2D. Due to this, the entity may be farther than the specified range! <image src="ents.FindInCone.png" alt="2D_visualization_of_the_actual_shape_of_the_cone,_click_to_enlarge"/> <note>Clientside entities will not be returned by this function.</note> </description> <realm>Shared</realm> <args> <arg name="origin" type="Vector">The tip of the cone.</arg> <arg name="normal" type="Vector">Direction of the cone.</arg> <arg name="range" type="number">The range of the cone/box around the origin. <note> The function internally adds 1 to this argument before using it. </note> </arg> <arg name="angle_cos" type="number">The <page text="cosine">math.cos</page> of the angle between the center of the cone to its edges, which is half the overall angle of the cone. 1 makes a 0° cone, 0.707 makes approximately 90°, 0 makes 180°, and so on.</arg> </args> <rets> <ret name="" type="table<Entity>">A table of all found <page>Entity</page>s.</ret> </rets> </function> <example> <description>Demonstrates how this function works.</description> <code> local size = 128 local fov = 15 local segments = 32 local mat = Material("models/shiny") mat:SetFloat("$alpha", 0.25) hook.Add("PostDrawOpaqueRenderables", "ents.FindInCone demo", function() local ply = LocalPlayer() if IsValid(ply) == false then return end local startPos = ply:EyePos() local dir = ply:GetAimVector() dir:Normalize() local angleCos = math.cos(math.rad(fov)) local radius = math.tan(math.acos(angleCos)) * size local endPos = startPos + dir * size render.SetMaterial(mat) render.DrawSphere(startPos, size, 24, 16, color_white, true) ⤶ local mins = Vector(-size, -size, -size)⤶ local maxs = Vector(size, size, size)⤶ render.DrawWireframeBox(startPos, angle_zero, mins, maxs, color_white, true)⤶ render.DrawBox(startPos, angle_zero, -mins, -maxs, color_white)⤶ local up = Vector(0, 0, 1) if math.abs(dir:Dot(up)) > 0.99 then up = Vector(1, 0, 0) end local right = dir:Cross(up) right:Normalize() up = right:Cross(dir) up:Normalize() render.SetColorMaterial() for i = 0, segments - 1 do local a1 = (i / segments) * math.pi * 2 local a2 = ((i + 1) / segments) * math.pi * 2 local p1 = endPos + (right * math.cos(a1) + up * math.sin(a1)) * radius local p2 = endPos + (right * math.cos(a2) + up * math.sin(a2)) * radius render.DrawBeam(p1, p2, 2, 0, 1, Color(17, 163, 204, 50)) render.DrawBeam(startPos, p1, 2, 0, 1, Color(0, 124, 158, 50)) end render.DrawLine(startPos, endPos, Color(0, 255, 0), true) for _, ent in ipairs(ents.FindInCone(startPos, dir, size, angleCos)) do render.DrawLine( startPos, ent:WorldSpaceCenter(), Color(255, 0, 0), true ) end end) </code> <output> <image src="https://i.imgur.com/ro85Z4a.gif" alt="ents.FindInCone demonstration" /> <image src="https://i.imgur.com/iHREL89.gif" alt="ents.FindInCone demonstration" /> </output> </example>