[E2][EA2][Wire] Collision detection

Adds a collision detector object which checks if a linked prop is colliding

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

This addon also allows you to add collision callback to your props, executing an E2 script / EA2 function when it collides with another object.

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 

And there is a simple example, sticks wired prop to everything it 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 collision detection
    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())
}


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

There is an Example for EA2, does the same as above script:


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)
        }
    }
}

Downloads:

Steam Workshop link: http://steamcommunity.com/sharedfiles/filedetails/?id=368603077

Zip file: https://dl.dropboxusercontent.com/u/3978328/CollisionDetector.zip

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

Cool. You should pr to the wire repository though. Hardly anyone will get this mod if it just sits here on facepunch.