@name Lightsaber
@persist Enemy:entity Self:entity 
@persist InertiaConstantRad:number TickTime:number
@persist InertiaConstantDeg:number
@trigger 
if (tickClk()) {
O=owner()
Self=entity():isConstrainedTo()
if(Enemy){
    Aim1DirF = (Enemy:massCenter() - Self:pos())
}else{
    Aim1DirF=O:aimPos()
}
Aim1Dir =(Self:forward()) #this is the direction you want the turret to aim from. 
Mass=entity():mass()
R=25
PAng=O:eyeAngles():yaw()-45
PAngP=O:eyeAngles():pitch()
XYZ=O:shootPos()+vec(R*cos(PAng),R*sin(PAng),-5)
Speed=15
entity():applyForce(Speed*(XYZ-Self:pos())-Self:vel()*Mass)


Aim2Dir = (Self:up())
Aim2DirF = vec(0,0,1)#entity(154):pos()-Self:pos()#:normalized()


Qinitial = quat(Aim1Dir,Aim2Dir)
Qfinal = quat(Aim1DirF,Aim2DirF)
Qrot= qMod(Qfinal*inv(Qinitial)) #need to modulo it to stop angle glich
RotationNeeded = vec(2*log(Qrot))
#RotationNeeded = vec(2*log(Qfinal*inv(Qinitial)))
#rotation vector = 2*ln(Qfinal-inv(Qinitial)))

SelfAngVel = Self:angVelVector()*pi()/180
ActualInertia = Self:inertia()*InertiaConstantRad
LocalRot = Self:toLocalAxis(RotationNeeded)
Self:applyTorque((LocalRot/TickTime - SelfAngVel)*ActualInertia)
} elseif (first()) {
    Enemy = entity(153)
    Self = entity(155)
    #the inertia constant Rad is the base inertia constant
    InertiaConstantRad = 1550.0030838848
    TickTime =0.015
    runOnTick(1)
}
