diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..600d2d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode \ No newline at end of file diff --git a/lib/math2.lua b/lib/math2.lua index d3e858b..6cc5f27 100644 --- a/lib/math2.lua +++ b/lib/math2.lua @@ -25,3 +25,7 @@ end math.step_floor = function(n, step) return math.floor(n / step) * step end + +math.step_ceil = function(n, step) + return math.ceil(n / step) * step +end \ No newline at end of file diff --git a/lib/spatial_grid.lua b/lib/spatial_grid.lua index 6b94a73..a0e507d 100644 --- a/lib/spatial_grid.lua +++ b/lib/spatial_grid.lua @@ -4,16 +4,17 @@ require "lib.entity" require "math" local __SpatialGrid = { - table = {} + table = {}, + cell_size = 0 } ----- uncomment this for test +-- uncomment this for test -- __Entity = { -- position = Vec3 {}, -- some_other_shit = "this litteraly other shit" -- } --- function Entity(pos) +-- local function Entity(pos) -- local entity = { -- position = pos -- } @@ -21,49 +22,77 @@ local __SpatialGrid = { -- return setmetatable(entity, {__index = __Entity}) -- end +local function str_position(x, y) + return tostring(x) .. " " .. tostring(y) +end function SpatialGrid(center, side, cell_size, entities) local lu_side = Vec3 {center.x - math.ceil(side/2), center.y - math.ceil(side/2)} local rd_side = Vec3 {center.x + math.floor(side/2), center.y + math.floor(side/2)} - local pos_entities = {} + local pos_entities = {} -- таблица, где ключ это позиция объекта, а значение это сам объект for _, entity in ipairs(entities) do - local pos = tostring(math.step_floor(entity.position.x, cell_size)) .. " " .. tostring(math.step_floor(entity.position.y, cell_size)) + local pos = str_position(math.step_floor(entity.position.x, cell_size), math.step_floor(entity.position.y, cell_size)) if pos_entities[pos] == nil then pos_entities[pos] = {} end table.insert(pos_entities[pos], entity) end - table = {} + local table = {} for x = lu_side.x, rd_side.x, cell_size do for y = lu_side.y, rd_side.y, cell_size do - local pos = tostring(x) .. " " .. tostring(y) - table[pos] = {position = Vec3 {x, y}, content = pos_entities[pos]} + local pos = str_position(x, y) + table[pos] = {position = Vec3 {x, y}, content = pos_entities[pos]} -- содержимое таблицы table в таблице spatialgrid end end - return setmetatable({table = table}, {__index = __SpatialGrid}) -- честно не уверен зачем это, но пусть будет + return setmetatable({table = table, cell_size = cell_size}, {__index = __SpatialGrid}) -- честно не уверен зачем это, но пусть будет end function __SpatialGrid:query(position) local entities = {} for extra_x = -1, 1 do for extra_y = -1, 1 do - local entity_pos = tostring(position.x + extra_x) .. " " .. tostring(position.y + extra_y) + local entity_pos = str_position(position.x + extra_x*self.cell_size, position.y + extra_y*self.cell_size) entities[entity_pos] = self.table[entity_pos].content end end return entities end ----- uncomment this too for test --- local table = SpatialGrid(Vec3 {0, 0}, 10, 0.5, {Entity(Vec3 {-1, -1, 0}), Entity(Vec3 {-1.5, -1, 0}), Entity(Vec3 {-2, -1, 0})}) +function __SpatialGrid:add(obj) + local obj_x, obj_y = 0, 0 --- local query_table = table:query(Vec3 {-1, -1, 0}) + if obj.position.x > 0 then + obj_x = math.step_floor(obj.position.x, self.cell_size) + else + obj_x = math.step_ceil(obj.position.x, self.cell_size) + end + + if obj.position.y > 0 then + obj_y = math.step_floor(obj.position.y, self.cell_size) + else + obj_y = math.step_ceil(obj.position.y, self.cell_size) + end + + local pos = str_position(obj_x, obj_y) + table.insert(self.table[pos].content, obj) +end + +-- uncomment this too for test +-- local my_table = SpatialGrid(Vec3 {0, 0}, 10, 0.5, {Entity(Vec3 {-1, -1, 0}), Entity(Vec3 {-1.5, -1, 0}), Entity(Vec3 {-2, -1, 0})}) + +-- my_table:add(Entity(Vec3 {-1.25, -1, 0})) + +-- local query_table = my_table:query(Vec3 {-1, -1, 0}) -- for k, v in pairs(query_table) do -- for i = 1, #v do --- print(v[i].some_other_shit, v[i].position.y) +-- print(v[i].some_other_shit, v[i].position.x) -- end -- end + +-- for i, v in ipairs(my_table.table["-1 -1"].content) do +-- print(i, v) +-- end