From 737bba854137acc9218f3c123337b0e8727a5b54 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Tue, 5 Nov 2024 20:13:53 +0100 Subject: [PATCH] feat(utils): add some vector utils Will be in time replace the physics/utils --- framework/utils/init.lua | 3 ++- framework/utils/vector.lua | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 framework/utils/vector.lua diff --git a/framework/utils/init.lua b/framework/utils/init.lua index 1953aab..aea9bc3 100644 --- a/framework/utils/init.lua +++ b/framework/utils/init.lua @@ -31,5 +31,6 @@ return { table = require "framework.utils.table", string = require "framework.utils.string", time = require "framework.utils.time", - datas = require "framework.utils.datas" + datas = require "framework.utils.datas", + vector = require "framework.utils.vector" } diff --git a/framework/utils/vector.lua b/framework/utils/vector.lua new file mode 100644 index 0000000..f822875 --- /dev/null +++ b/framework/utils/vector.lua @@ -0,0 +1,33 @@ +local vectors = {} +local Vector = require "framework.libs.brinevector3D" + +---@class Vector +---@field x number the x coordinate of the vector +---@field y number the y coordinate of the vector +---@field z number the z coordinate of the vector + +--- Apply a function to all coordinate of a vector to get a new one +---@param vector Vector the vector you want to apply to +---@param func function the function you want to apply +---@return Vector vector a new vector created from the first one and the function +function vectors.applyFunctionToCoord(vector, func) + return Vector(func(vector.x), func(vector.y), func(vector.z)) +end + + + +--- Get a vector with all coordinate turned to the largest integral value smaller than or equal to them +---@param vector Vector the vector you want to get the floor coordinate +---@return Vector vector the new vector with floor-cordinates +function vectors.floor(vector) + return vectors.applyFunctionToCoord(vector, math.floor) +end + +--- Get a vector with all coordinate turned to the smallest integral value larger than or equal to them +---@param vector Vector the vector you want to get the ceil coordinate +---@return Vector vector the new vector with ceil-cordinates +function vectors.ceil(vector) + return vectors.applyFunctionToCoord(vector, math.ceil) +end + +return vectors \ No newline at end of file