How to selectively allow/prevent collisions (without GM:ShouldCollide)?
11 replies, posted
Say I have some entity in the shape of a box. I'd like to let this entity selectively collide with certain trivial entities while not collide with others. For example, you could think of a wall that lets certain players pass through but remains solid for others.
One method I currently know of is adding a [B][URL="http://wiki.garrysmod.com/page/GM/ShouldCollide"]GM:ShouldCollide[/URL][/B] hook and setting [B]ent:SetCustomCollisionCheck( true )[/B] on all of the entities.
[I]I would really like to entirely avoid this method[/I]. It seems extremely inefficient, especially since enabling the custom collision checks on the entities means ShouldCollide is getting called dozens of times per involved entity every second (even if I only care about collisions with my custom entity).
I'm basically looking for something similar to [URL="http://wiki.garrysmod.com/page/ENTITY/StartTouch"][B]ENTITY:StartTouch()[/B][/URL]/[URL="http://wiki.garrysmod.com/page/ENTITY/Touch"][B]Entity:Touch()[/B][/URL] where I could just return true/false to prevent or allow collision between this entity and the colliding entity.
Is there any simple/efficient way I can accomplish this, or do we really only have GM:ShouldCollide for preventing specific collisions?
-snipe-
Did you ever find out? I'm trying to do something similar.
You only need to set custom collision check on the ents that will be needed to bypass the collision. for example with the wall you only need to call it on players once when they are authed.
[QUOTE=Blasphemy;47500239]Did you ever find out? I'm trying to do something similar.[/QUOTE]
Nope. It doesn't really sound like there are any other alternatives.
[QUOTE=AnonTakesOver;47501359]You only need to set custom collision check on the ents that will be needed to bypass the collision. for example with the wall you only need to call it on players once when they are authed.[/QUOTE]
The players were just to give an easy example. I need this check to occur for every entity, unfortunately.
[QUOTE=Mista Tea;47493143]Say I have some entity in the shape of a box. I'd like to let this entity selectively collide with certain trivial entities while not collide with others. For example, you could think of a wall that lets certain players pass through but remains solid for others.
One method I currently know of is adding a [B][URL="http://wiki.garrysmod.com/page/GM/ShouldCollide"]GM:ShouldCollide[/URL][/B] hook and setting [B]ent:SetCustomCollisionCheck( true )[/B] on all of the entities.
[I]I would really like to entirely avoid this method[/I]. It seems extremely inefficient, especially since enabling the custom collision checks on the entities means ShouldCollide is getting called dozens of times per involved entity every second (even if I only care about collisions with my custom entity).
I'm basically looking for something similar to [URL="http://wiki.garrysmod.com/page/ENTITY/StartTouch"][B]ENTITY:StartTouch()[/B][/URL]/[URL="http://wiki.garrysmod.com/page/ENTITY/Touch"][B]Entity:Touch()[/B][/URL] where I could just return true/false to prevent or allow collision between this entity and the colliding entity.
Is there any simple/efficient way I can accomplish this, or do we really only have GM:ShouldCollide for preventing specific collisions?[/QUOTE]
ShouldCollide gets pretty laggy once you have over 20 entities with custom collision.
I tried to create multi-world addon which allows server to have multiple instances where people can build but I found that ShouldCollide is the limiting factor :suicide:
ShouldCollide gets called on the client when the client looks at something despite it being far away. You can always optimize the checks...
Anyway, you could, using touch events, alter the collision group if it hasn't already been altered then restore when the touch event ends.
you can peel into the entity methatable and do a work around! it will mean you'd have to do the SetCustomCollisionCheck, though )
we'll start with the metatable stuff:
[lua]
local META = FindMetaTable ( 'Entity' )
if ( ! META ) then return end
function META:SetCollisionPartner ( iType )
self.CollisionPartner = iType
end
function META:GetCollidionPartner ()
return self.CollisionPartner
end
function META:CanCollideWithPartner ( ent )
if ( !self.CollisionPartner or !ent.CollisionPartner ) then return false end
if ( self.CollisionPartner == ent.CollisionPartner ) then return true
end
return false
end
[/lua]
now you have a way to set a variable to an entity and check if they can collide or not.
now if you want to use these you have to set these up in either initialspawn or in initialize of an entity because they only need to be set once. like so:
[lua]
ply:SetCollisionPartner ( 1 )
ply:SetCustomCollisionCheck ( true )
[/lua]
and then in GM:ShouldCollide () you'd do:
[lua]
function GM:ShouldCollide ( ent1, ent2 )
if ( ent1:CanCollideWithPartner ( ent2 ) and ent2:CanCollideWithPartner ( ent1 ) ) then
return false
end
return true
end
[/lua]
[B]THIS IS MADE ON A PHONE AND IS BOUND TO HAVE SOME TYPOS IN IT[/B] I'm sure you'll figure it out or someone is nice enough to help you. Hope it helped!
[code]
function META:SetCollisionPartner ( iType )
if ( self.CollisionPartner ) then
self.CollisionPartner = iType
return
end
self.CollisionPartner = iType
end
[/code]
lolwat :v:
forgot a ! there.. I'l edit it :v:
[QUOTE=robbert^^;47524479]forgot a ! there.. I'l edit it :v:[/QUOTE]
It's still redundant.
oh wow, I should really stop trying to help people in the morning :v:
this is still a nice way of doing it, though. hope it helped the op.
Sorry, you need to Log In to post a reply to this thread.