Garry's Mod Wiki

ENTITY:ResolveCustomFlyCollision

  ENTITY:ResolveCustomFlyCollision( table traceResult, vector vel )

Description

Called during a non-VPhysics collision event for flying entities.

This is best used to bounce off projectiles from surfaces. For this to be triggered, your entity must be the one that's colliding, have some velocity ENTITY:SetMoveType must either be MOVETYPE_FLY or MOVETYPE_FLY_GRAVITY and ENTITY:SetMoveCollide must be MOVECOLLIDE_FLY_CUSTOM.

This works only on anim type entities.

Arguments

1 table with TraceResult structure traceResult
The TraceResult structure where the collision occured.
2 vector vel
The calculated velocity after calculations such as bounciness, elasticity, ground sliding etc...

Example

A rocket model which directly reflects off from surfaces without any velocity loss, similar to sent_ball.

AddCSLuaFile() ENT.Base = "base_anim" ENT.Type = "anim" ENT.PrintName = "Bouncy Rocket" ENT.Category = "Other" ENT.Editable = true ENT.Spawnable = true ENT.AdminOnly = true ENT.RenderGroup = RENDERGROUP_TRANSLUCENT ENT.PhysicsSolidMask = MASK_SOLID + CONTENTS_HITBOX -- collide with vphysics function ENT:Initialize() self:SetModel("models/weapons/w_missile_closed.mdl") self:SetMoveType(MOVETYPE_FLY) self:SetMoveCollide(MOVECOLLIDE_FLY_CUSTOM) self:SetCollisionGroup(COLLISION_GROUP_PROJECTILE) self:SetSolid(SOLID_BBOX) -- turns on collision with other entities self:SetCollisionBounds(Vector(-1,-1,-1),Vector(1,1,1)) self:SetVelocity(self:GetForward()*650) end function ENT:ResolveCustomFlyCollision(traceResult, vel) vel = self:GetVelocity() -- use unmodified velocity local normal = vel - 2 * vel:Dot(traceResult.HitNormal) * traceResult.HitNormal local speed = vel:Length() normal = normal:GetNormalized() self:SetLocalVelocity(normal * speed) self:SetAngles(normal:Angle()) end