Neckrat 0293409dd9 camera refactor
Co-authored-by: Ivan Yuriev <ivanyr44@gmail.com>
2025-08-02 02:16:20 +03:00

83 lines
1.7 KiB
Lua

--- @class (exact) Vec3
--- @field x number
--- @field y number
--- @field z number
local __Vec3 = {
x = 0,
y = 0,
z = 0,
}
local function __tostring(self)
return "Vec3{" .. self.x .. ", " .. self.y .. ", " .. self.z .. "}"
end
--- @param other Vec3
--- @return Vec3
function __Vec3:add(other)
return Vec3 { self.x + other.x, self.y + other.y, self.z + other.z }
end
--- @param factor number
function __Vec3:scale(factor)
return Vec3 { self.x * factor, self.y * factor, self.z * factor }
end
function __Vec3:length()
return math.sqrt(self.x ^ 2 + self.y ^ 2 + self.z ^ 2)
end
function __Vec3:normalize()
local length = self:length()
if length == 0 then return nil end
return Vec3 {
self.x / length,
self.y / length,
self.z / length
}
end
function __Vec3:direction()
return math.atan2(self.y, self.x)
end
--- @param other Vec3
function __Vec3:dot(other)
return self.x * other.x + self.y * other.y + self.z * other.z
end
--- @param other Vec3
function __Vec3:angle_to(other)
return (other - self):direction()
end
---Vec3 constructor
---@param vec number[]
---@return Vec3
function Vec3(vec)
return setmetatable({
x = vec[1] or 0,
y = vec[2] or 0,
z = vec[3] or 0,
}, {
__index = __Vec3,
__tostring = __tostring,
__add = __Vec3.add,
__mul = __Vec3.scale,
__unm = function(self)
return __Vec3.scale(self, -1)
end,
__sub = function(self, other)
return self + -other
end,
__eq = function(self, other)
return
self.x == other.x
and self.y == other.y
and self.z == other.z
end,
})
end
return Vec3