@name Grabber Turret
@inputs Grab:wirelink
@outputs Ang:angle I
@persist [G E O F]:entity
@model models/props_junk/PopCan01a.mdl
#by [ME]OmicroN
if(first()){
    runOnTick(1)
    O = owner()
    E = entity()
    G = Grab:entity()
    G:setMass(50000)
}

if(findCanQuery()){
    findByClass("prop_physics")
    F = findClosest(E:pos())
}

Grabit = O:keyUse()

if(changed(Grabit)){
    Grab["Grab",number] = Grabit
}

I = clamp(I+(Grabit-!Grabit*2)/100,0,1)

## Pos is the global position of where the Grabber will hover at ##
Pos = E:toWorld(vec(0,0,40))
G:applyForce(((Pos-G:pos())*15-G:vel()/2)*G:mass())

## F:pos() is the global position that the grabber will aim at ##
Dir = G:pos()-F:pos()

## slerp swaps between two quaternions with the angles built in ##
## the value I goes between 0 and 1, swapping between the directional angle and the other ##
## AltAng is the angle that the grabber will rotate to when you hold Use ##
AltAng = E:angles()
Q = slerp( quat(Dir:toAngle())*qRotation(vec(0,1,0),-90) , quat(AltAng) , I )
G:applyTorque((150*G:toLocal(rotationVector(Q/quat(G))+G:pos()) - 12*G:angVelVector())*G:inertia())

#an alternate version
#E:applyTorque((150*E:toLocal(rotationVector(quat(Ang)/quat(E))+E:pos()) - 12*E:angVelVector())*E:inertia())
