Skip to content

Instantly share code, notes, and snippets.

@CheezusChrust
Created October 6, 2023 20:29
Show Gist options
  • Save CheezusChrust/8493f8abb1fc3b151b2ccbe7df5e68f5 to your computer and use it in GitHub Desktop.
Save CheezusChrust/8493f8abb1fc3b151b2ccbe7df5e68f5 to your computer and use it in GitHub Desktop.
--@name lib/controlsurface
--@author Cheezus
local deg, atan, sign = math.deg, math.atan, math.sign
local ControlSurface = class("ControlSurface")
local surfaces = {}
function ControlSurface:initialize(base, ent, axisEnt)
local worldEntAngle = ent:getAngles()
self.ent = ent
self.base = base
self.startAng = base:worldToLocalAngles(worldEntAngle)
self.rotAxis = base:worldToLocalVector(axisEnt:getRight())
self.curAng = 0
self.forward = ent:worldToLocalVector(axisEnt:getForward())
self.up = ent:worldToLocalVector(axisEnt:getUp())
self.right = ent:worldToLocalVector(axisEnt:getRight())
self.holo = hologram.create(ent:getPos(), worldEntAngle, "models/sprops/cuboids/height06/size_1/cube_6x6x6.mdl", Vector(1, 1, 1))
self.holo:setParent(base)
self.holo:setColor(Color(0, 0, 0, 0))
ent:setFrozen(true)
ent:setParent()
constraint.breakAll(ent)
timer.simple(0.1, function()
constraint.weld(ent, base)
ent:setParent(self.holo)
end)
table.insert(surfaces, ent)
end
function ControlSurface:setAngle(ang)
if ang == self.curAng then return end
local rotatedAng = self.startAng:rotateAroundAxis(self.rotAxis, ang)
self.holo:setAngles(self.base:localToWorldAngles(rotatedAng))
self.curAng = ang
end
function ControlSurface:getAoA()
local vel = self.ent:getVelocity()
local velLen = vel:getLength()
if velLen == 0 then return 0 end
local fwd = self.forward
local right = self.right
local up = self.up
local curAng = self.curAng
local fwdVel = vel:dot(self.ent:localToWorldVector(fwd:rotateAroundAxis(right, curAng)))
local upVel = vel:dot(self.ent:localToWorldVector(up:rotateAroundAxis(right, curAng)))
return deg(atan(upVel / fwdVel)) * -sign(fwdVel)
end
hook.add("removed", "cs_removed", function()
for _, ent in ipairs(surfaces) do
ent:setParent()
end
end)
return ControlSurface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment