- fix spatialgrid
- add fn spatialgrid:add - add fn math:step_ceil
This commit is contained in:
parent
7f0e4e213e
commit
93a9de8efd
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.vscode
|
||||||
@ -25,3 +25,7 @@ end
|
|||||||
math.step_floor = function(n, step)
|
math.step_floor = function(n, step)
|
||||||
return math.floor(n / step) * step
|
return math.floor(n / step) * step
|
||||||
end
|
end
|
||||||
|
|
||||||
|
math.step_ceil = function(n, step)
|
||||||
|
return math.ceil(n / step) * step
|
||||||
|
end
|
||||||
@ -4,16 +4,17 @@ require "lib.entity"
|
|||||||
require "math"
|
require "math"
|
||||||
|
|
||||||
local __SpatialGrid = {
|
local __SpatialGrid = {
|
||||||
table = {}
|
table = {},
|
||||||
|
cell_size = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
---- uncomment this for test
|
-- uncomment this for test
|
||||||
-- __Entity = {
|
-- __Entity = {
|
||||||
-- position = Vec3 {},
|
-- position = Vec3 {},
|
||||||
-- some_other_shit = "this litteraly other shit"
|
-- some_other_shit = "this litteraly other shit"
|
||||||
-- }
|
-- }
|
||||||
|
|
||||||
-- function Entity(pos)
|
-- local function Entity(pos)
|
||||||
-- local entity = {
|
-- local entity = {
|
||||||
-- position = pos
|
-- position = pos
|
||||||
-- }
|
-- }
|
||||||
@ -21,49 +22,77 @@ local __SpatialGrid = {
|
|||||||
-- return setmetatable(entity, {__index = __Entity})
|
-- return setmetatable(entity, {__index = __Entity})
|
||||||
-- end
|
-- end
|
||||||
|
|
||||||
|
local function str_position(x, y)
|
||||||
|
return tostring(x) .. " " .. tostring(y)
|
||||||
|
end
|
||||||
|
|
||||||
function SpatialGrid(center, side, cell_size, entities)
|
function SpatialGrid(center, side, cell_size, entities)
|
||||||
local lu_side = Vec3 {center.x - math.ceil(side/2), center.y - math.ceil(side/2)}
|
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 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
|
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
|
if pos_entities[pos] == nil then
|
||||||
pos_entities[pos] = {}
|
pos_entities[pos] = {}
|
||||||
end
|
end
|
||||||
table.insert(pos_entities[pos], entity)
|
table.insert(pos_entities[pos], entity)
|
||||||
end
|
end
|
||||||
|
|
||||||
table = {}
|
local table = {}
|
||||||
for x = lu_side.x, rd_side.x, cell_size do
|
for x = lu_side.x, rd_side.x, cell_size do
|
||||||
for y = lu_side.y, rd_side.y, cell_size do
|
for y = lu_side.y, rd_side.y, cell_size do
|
||||||
local pos = tostring(x) .. " " .. tostring(y)
|
local pos = str_position(x, y)
|
||||||
table[pos] = {position = Vec3 {x, y}, content = pos_entities[pos]}
|
table[pos] = {position = Vec3 {x, y}, content = pos_entities[pos]} -- содержимое таблицы table в таблице spatialgrid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return setmetatable({table = table}, {__index = __SpatialGrid}) -- честно не уверен зачем это, но пусть будет
|
return setmetatable({table = table, cell_size = cell_size}, {__index = __SpatialGrid}) -- честно не уверен зачем это, но пусть будет
|
||||||
end
|
end
|
||||||
|
|
||||||
function __SpatialGrid:query(position)
|
function __SpatialGrid:query(position)
|
||||||
local entities = {}
|
local entities = {}
|
||||||
for extra_x = -1, 1 do
|
for extra_x = -1, 1 do
|
||||||
for extra_y = -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
|
entities[entity_pos] = self.table[entity_pos].content
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return entities
|
return entities
|
||||||
end
|
end
|
||||||
|
|
||||||
---- uncomment this too for test
|
function __SpatialGrid:add(obj)
|
||||||
-- 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})})
|
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 k, v in pairs(query_table) do
|
||||||
-- for i = 1, #v 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
|
||||||
-- end
|
-- end
|
||||||
|
|
||||||
|
-- for i, v in ipairs(my_table.table["-1 -1"].content) do
|
||||||
|
-- print(i, v)
|
||||||
|
-- end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user