ents/FindInCone

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Was not able to get it to work. Arguments false? How is this supposed to work? Garry?)
m (Improved return description by adding type link)
 
(14 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Func
 
{{Func
|Description=Gets all entities within the specified cone.
+
|Description=[[File:ents.FindInCone.png|thumb|2D visualization of the actual shape of the cone, click to enlarge]]
{{Stub}}
+
Finds and returns all entities within the specified cone. Only entities whose {{ClassFunction|Entity|WorldSpaceCenter}} is within the cone are considered to be in it.
 +
 
 +
The "cone" is actually a conical "slice" of an axis-aligned box (see: {{LibraryFunction|ents|FindInBox}}). 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!
 +
 
 +
{{Note|Clientside entities will not be returned by this function.}}
 +
 
 +
{{Warning|If there are more than 512 entities in the axis-aligned box around the origin, then the result may be incomplete!}}
 
|Realm=Shared
 
|Realm=Shared
 
|IsClass=No
 
|IsClass=No
|Name=FindInCone
 
|Parent=ents
 
 
}}
 
}}
 
{{Arg
 
{{Arg
 
|type=Vector
 
|type=Vector
 
|name=origin
 
|name=origin
|desc=The "tip" of the cone.
+
|desc=The tip of the cone.
 
}}
 
}}
 
{{Arg
 
{{Arg
 
|type=Vector
 
|type=Vector
 
|name=normal
 
|name=normal
|desc=Direction of the code.
+
|desc=Direction of the cone.
 
}}
 
}}
 
{{Arg
 
{{Arg
 
|type=number
 
|type=number
|name=radius
+
|name=range
|desc=Height of the cone.
+
|desc=The range of the cone/box around the origin. <!-- The function internally adds 1 to this argument before using it. -->
 
}}
 
}}
 
{{Arg
 
{{Arg
 
|type=number
 
|type=number
|name=angle
+
|name=angle_cos
|desc=The angle of the cone.
+
|desc=The [[math/cos|cosine]] 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.
 
}}
 
}}
 
{{Ret
 
{{Ret
 
|type=table
 
|type=table
|desc=A table of all found entities
+
|desc=A table of all found {{Type|Entity}}s.
 +
}}
 +
{{Example
 +
|Description=An example usage of this function. This example shows which entities are being returned by the function with red lines and the range with white transparent box.
 +
|Code=local mat = Material( "models/shiny" )
 +
mat:SetFloat( "$alpha", 0.5 )
 +
 
 +
hook.Add( "PostDrawOpaqueRenderables", "conetest", function()
 +
local size = 200
 +
local dir = LocalPlayer():GetAimVector()
 +
local angle = math.cos( math.rad( 15 ) ) -- 15 degrees
 +
local startPos = LocalPlayer():EyePos()
 +
 
 +
local entities = ents.FindInCone( startPos, dir, size, angle )
 +
 
 +
-- draw the outer box
 +
local mins = Vector( -size, -size, -size )
 +
local maxs = Vector( size, size, size )
 +
 
 +
render.SetMaterial( mat )
 +
render.DrawWireframeBox( startPos, Angle( 0, 0, 0 ), mins, maxs, color_white, true )
 +
render.DrawBox( startPos, Angle( 0, 0, 0 ), -mins, -maxs, color_white )
 +
 
 +
-- draw the lines
 +
for id, ent in pairs( entities ) do
 +
render.DrawLine( ent:WorldSpaceCenter() - dir * ( ent:WorldSpaceCenter()-startPos ):Length(), ent:WorldSpaceCenter(), Color( 255, 0, 0 ) )
 +
end
 +
end )
 
}}
 
}}

Latest revision as of 21:13, 28 August 2018

 ents.FindInCone( )

Contents

Description

2D visualization of the actual shape of the cone, click to enlarge

Finds and returns all entities within the specified cone. Only entities whose Entity:WorldSpaceCenter is within the cone are considered to be in it.

The "cone" is actually a conical "slice" of an axis-aligned box (see: ents.FindInBox). 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!

NOTE

Clientside entities will not be returned by this function.

WARNING

If there are more than 512 entities in the axis-aligned box around the origin, then the result may be incomplete!

Arguments

Vector origin

The tip of the cone.

Arguments

Vector normal

Direction of the cone.

Arguments

number range

The range of the cone/box around the origin.

Arguments

number angle_cos

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

Returns

table

A table of all found Entitys.

Examples

Example

An example usage of this function. This example shows which entities are being returned by the function with red lines and the range with white transparent box.

local mat = Material( "models/shiny" )
mat:SetFloat( "$alpha", 0.5 )

hook.Add( "PostDrawOpaqueRenderables", "conetest", function()
	local size = 200
	local dir = LocalPlayer():GetAimVector()
	local angle = math.cos( math.rad( 15 ) ) -- 15 degrees
	local startPos = LocalPlayer():EyePos()

	local entities = ents.FindInCone( startPos, dir, size, angle )

	-- draw the outer box
	local mins = Vector( -size, -size, -size )
	local maxs = Vector( size, size, size )

	render.SetMaterial( mat )
	render.DrawWireframeBox( startPos, Angle( 0, 0, 0 ), mins, maxs, color_white, true )
	render.DrawBox( startPos, Angle( 0, 0, 0 ), -mins, -maxs, color_white )

	-- draw the lines
	for id, ent in pairs( entities ) do
		render.DrawLine( ent:WorldSpaceCenter() - dir * ( ent:WorldSpaceCenter()-startPos ):Length(), ent:WorldSpaceCenter(), Color( 255, 0, 0 ) )
	end
end )


Personal tools
Navigation