Garry's Mod

Garry's Mod

101 ratings
[E2][EA2][Wiremod] Collision Detector
   
Award
Favorite
Favorited
Unfavorite
Content Type: Addon
Addon Type: Tool
Addon Tags: Build, Fun
File Size
Posted
Updated
5.043 KB
3 Jan, 2015 @ 10:38am
8 Aug, 2015 @ 2:19pm
8 Change Notes ( view )

Subscribe to download
[E2][EA2][Wiremod] Collision Detector

In 1 collection by rafradek
My Wire Addons
4 items
Description
Adds a collision detector which checks if a linked prop is colliding

If no entity is linked to collision detector, it will detect its own collisions.

E2 Functions:
Entity:addCollDetection(E2 (optional)) - Execute this or passed E2's script every time passed entity collides with another entity
Entity:removeCollDetection(E2 (optional)) - Remove a collision callback from this entity for this or passed E2
Entity:hasCollDetection() - Checks if this entity detects collisions
isCollClk() - Returns 1 if this execution was caused by colliding entities
collHitEntity() - Returns entity that was hit
collEntity() - Returns entity registered by this script
collDelta() - Time since the last collision
collSpeed() - Speed of the registered entity just before the collision
collEnabledEarlyClk() - Type 1 to execute this script in physics callback (1 tick earlier, but you cannot make constraints in this mode)
collVelocity() - Returns entity velocity before collision
collHitVelocity() - Returns hit entity velocity before collision
collPos() - Returns position where the collision happened
collNormal() - Normal of hit surface
collEntityPos() - Position of the registered entity
collHitEntityPos() - Position of hit entity
collEntityAng() - Angles of the registered entity
collHitEntityAng() - Angles of hit entity
collIgnoreConstrained(number) - Set it to 0 to allow colliding with constrained objects
collFilter(number) - Set it to 0 to allow executing this script multiple times per tick
removeAllCollClk() - Removes callback from all entities

E2 example, will stick the prop if collides:

@name Detection
@inputs Prop:entity
@outputs
@persist OldProp:entity
@trigger all
#Called at start or when prop changed
if(!isCollClk()){
#Removing collision detection on previourlsly linked prop
if(OldProp!=Prop){
OldProp:removeCollDetection()
}
OldProp=Prop
#Adding prop to collis
Prop:addCollDetection()
}
#Called when Prop collides with another object
if(isCollClk()&&!collHitEntity():isPlayer()){
#Moves prop to place where it collided
collEntity():setPos(collEntityPos())
collEntity():setAng(collEntityAng())
#Sticks prop to collided entity
weld(collEntity(),collHitEntity())
}

There are also some convars for server admins:
wire_col_detector_e2_maxcollclk - Maximum amount of props with collision detection per gate (default - 100)
wire_col_detector_e2_collallow - Allow collision detection of other players' stuff (default - 1)

You can view Expression Advanced 2 functions using in-game manual, all of them are placed in colldetection extension.

EA2 example, will stick the prop if collides:

server{
//Prop entity which will be used for collision detection
input entity Prop
entity oldProp
//Function which will be called when our prop collides
delegate dlg=function(entity ourEnt,entity hitEnt,physics ourPhys,physics hitPhys,
vector ourVel,vector hitVel,vector hitPos,number speed, vector normal,number delta){

if(hitEnt.isPlayer()){return}
//Because it is not possible to create constraints in physics callback, we need a timer
timerSimple(0,function(vector pos,angle ang,entity our,entity hit){
//Moves prop to place where it collided
our.setPos(pos)
our.setAng(ang)
//Welds prop to hit entity
our.weldTo(hit,0,false)
},ourPhys.pos(),ourPhys.ang(),ourEnt,hitEnt)

}
//Adds collision callback to linked prop, oldProp is used to remove callback on previously linked props
Prop.addCollCallback(dlg)
oldProp=Prop
//Called when input is changed
event trigger(string name,string type){
//Removes callback on previous prop and adds to newer one.
if (Prop != oldProp){
oldProp.removeConstraints("Weld")
oldProp.removeCollCallback()
oldProp=Prop
Prop.addCollCallback(dlg)
}
}
}

Inputs:
-Stick to world - weld to world on hit when input value is 1
-Stick to props - weld to props and other entities. Will not work if prop protection addon denies access to hit entity and
"wire_col_detector_stickprops" is set to 0 (Default is 0)
-Stick to npc/player - Parent (this will remove physics simulation) to players or npc. Parenting to players will only work if convar "wire_col_detector_stickplayer" is set to 1 (Default is 0).

Outputs:

-Collided - outputs 1 if the linked entity has just collided with something
-Speed - returns speed of the linked entity just before last collision
-Delta time - outputs time from even earlier collision to last collision
-Hit Position - outputs position at which linked prop collided
-Hit Normal - returns normal of the surface that hit another entity
-Velocity - outputs velocity of linked entity before last collision
-Hit Entity Velocity - the same as above, but for hit entity
-Hit Entity - return the entity that was hit
-Entity Position / Entity Angle- outputs position / angle of the linked entity in last collision
-Hit Entity Position / Hit Entity Angle - the same as above but for an entity that was hit by our entity
14 Comments
SimulatingBacon 2 hours ago 
Will there be a potential update to this to fix the error?
beetlejuicehero 27 Oct, 2024 @ 1:42pm 
can i make requests for things to be added to the e2s
Superserg 15 Oct, 2019 @ 10:01am 
Yep, I get the same error. I just created a detector and linked it to a prop, i have not even wired it to anything. And every time i hit the prop, i get this error. And "Collided" output takes no effect.
Any fixes?
Seb 1 May, 2017 @ 10:24am 
[ERROR] entities/gmod_wire_expression2/core/custom/collcallback.lua:20: attempt to get length of a nil value
1. unknown - entities/gmod_wire_expression2/core/custom/collcallback.lua:20

thats what i get
Skyrox 2 Mar, 2017 @ 11:55am 
I can only get the functions to work for a certain amount of time on one prop before it suddenly stops working.
EXTRA MENTAL 27 Aug, 2016 @ 12:56am 
This addon is brilliant, I wish this functionality was in vanilla EA2
rafradek  [author] 17 Nov, 2015 @ 1:11am 
depends if the parented props has physics and can collide
BenDragon81037 11 Nov, 2015 @ 8:19am 
Say will this addon work if props are parented?
rafradek  [author] 7 Aug, 2015 @ 1:59am 
now the addon includes also E2 and EA2 extensions
BenDragon81037 25 Mar, 2015 @ 3:52am 
Oh yes, it does accept inputs / outputs, but it isn't dynamic.

What I mean is that you can't make it change entities in a quick flash.

Like for example: I have an E2 called the "boxcar E2", which pretty much parents everything the function "findInBox()" finds to that entity.

It it doesn't work with a whole lot of entities, for exmaple: carrying bulk loads, where you load cargo into a hopper. With my E2, there could be a lot of props which makes up the load, and all of the objects colliding at once would lag any server.

So what there needs to be, would be a reliable way of dynamically checking which entity colides with what.