util.TraceHull

From Garry's Mod
Jump to: navigation, search
 util.TraceHull( )

Contents

Description

Performs an AABB hull (axis-aligned bounding box, aka not rotated) trace with the given trace data.

NOTE

Clientside entities will not be hit by traces.

NOTE

This function may not always give desired results clientside due to certain physics mechanisms not existing on the client. Use it serverside for accurate results.

Arguments

table TraceData

The trace data to use. See HullTrace structure

Returns

table

Trace result. See TraceResult structure

Examples

Example

From a SWEP:PrimaryAttack()

local tr = util.TraceHull( {
	start = self.Owner:GetShootPos(),
	endpos = self.Owner:GetShootPos() + ( self.Owner:GetAimVector() * 100 ),
	filter = self.Owner,
	mins = Vector( -10, -10, -10 ),
	maxs = Vector( 10, 10, 10 ),
	mask = MASK_SHOT_HULL
} )


Examples

Example

Visual representation of a Hull Trace.

function ENT:Draw()

	self:DrawModel()
	
	local ent = self
	
	local mins = ent:OBBMins()
	local maxs = ent:OBBMaxs()
	local startpos = ent:GetPos()
	local dir = ent:GetUp()
	local len = 128

	local tr = util.TraceHull( {
		start = startpos,
		endpos = startpos + dir * len,
		maxs = maxs,
		mins = mins,
		filter = ent
	} )
	
	render.DrawLine( tr.HitPos, startpos + dir * len, color_white, true )
	render.DrawLine( startpos, tr.HitPos, Color( 0, 0, 255 ), true )
	
	local clr = color_white
	if ( tr.Hit ) then
		clr = Color( 255, 0, 0 )
	end

	render.DrawWireframeBox( startpos, Angle( 0, 0, 0 ), mins, maxs, Color( 255, 255, 255 ), true )
	render.DrawWireframeBox( tr.HitPos, Angle( 0, 0, 0 ), mins, maxs, clr, true )

end

Output:



HullTrace.gif

Examples

Example

Trace a player sized hull to detect if a player can spawn here without getting stuck inside anything.

local pos = Entity(1):GetPos() -- Choose your position.

local tr = {
	start = pos,
	endpos = pos,
	mins = Vector( -16, -16, 0 ),
	maxs = Vector( 16, 16, 71 )
}

local hullTrace = util.TraceHull( tr )
if ( hullTrace.Hit ) then
    -- Find a new spawnpoint
end


Personal tools
Navigation