Garry's Mod Wiki

Revision Difference

ENTITY:ResolveCustomFlyCollision#567689

<function name="ResolveCustomFlyCollision" parent="ENTITY" type="hook">⤶ <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 <page>ENTITY:SetMoveType</page> must either be `MOVETYPE_FLY` or `MOVETYPE_FLY_GRAVITY` and <page>ENTITY:SetMoveCollide</page> must be `MOVECOLLIDE_FLY_CUSTOM`.⤶ <note>This works only on `anim` type entities.</note></description>⤶ <realm>Server</realm>⤶ <args>⤶ <arg name="traceResult" type="table{TraceResult}">The <page>Structures/TraceResult</page> where the collision occured.</arg>⤶ <arg name="vel" type="vector">The calculated velocity after calculations such as bounciness, elasticity, ground sliding etc...</arg>⤶ </args>⤶ </function>⤶ ⤶ <example>⤶ <description>A rocket model which directly reflects off from surfaces without any velocity loss, similar to `sent_ball`.</description>⤶ <code>⤶ 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⤶ </code>⤶ ⤶ </example>