Garry's Mod Wiki

ENTITY:TestCollision

  table ENTITY:TestCollision( Vector startpos, Vector delta, boolean isbox, Vector extents, number mask )

Description

Allows you to override trace result when a trace hits the entity.

Your entity must have Entity:EnableCustomCollisions enabled for this hook to work.

Your entity must also be otherwise "hit-able" with a trace, so it should have SOLID_OBB or SOLID_VPHYSICS be set (as an example), and it must have its collision bounds be set accordingly.

This hook is called for anim type only.

Arguments

1 Vector startpos
Start position of the trace.
2 Vector delta
Offset from startpos to the endpos of the trace.
3 boolean isbox
Is the trace a hull trace?
4 Vector extents
Size of the hull trace, with the center of the Bounding Box being 0, 0, 0, so mins are -extents, and maxs are extents.
5 number mask
The CONTENTS enum mask.

Returns

1 table
Returning a table will allow you to override trace results. Table should contain the following keys: (All keys fallback to the original trace value)
  • Vector HitPos - The new hit position of the trace.
  • number Fraction - A number from 0 to 1, describing how far the trace went from its origin point, 1 = did not hit.
    • Could be calculated like so : Fraction = ( startpos + delta ):Length() / myCustomHitPos:Length()
  • Vector Normal - A unit vector (length=1) describing the direction perpendicular to the hit surface.

Returning true will allow "normal" collisions to happen for SOLID_VPHYSICS and SOLID_OBB entities.
Returning nothing or false allows the trace to ignore the entity completely.

Example

Example taken from lua/entities/widget_base.lua

function ENT:TestCollision( startpos, delta, isbox, extents ) if ( isbox ) then return end if ( !widgets.Tracing ) then return end -- TODO. Actually trace against our cube! return { HitPos = self:GetPos(), Fraction = 0.5 * self:GetPriority() } end

Example

Allows players to shoot through the entity, but still stand on it and use the Physics Gun on it, etc.

local sent_contents = CONTENTS_GRATE function ENT:TestCollision( startpos, delta, isbox, extents, mask ) if bit.band( mask, sent_contents ) ~= 0 then return true end end