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,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,},
    {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,},
    {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,},
    {"a" ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"b" ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,},
    {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"c" ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,},
}
And this code wich is supposed to first, insert all points (a, b, c) into a table and then connect them to rectangle colliders.
Like if a and b are on the same row, they belong to each other and if the the c and b y are in the same collum, thats all good, but it doesnt work for whatever reason, wich is why i need help. It gives me 4 colliders, one with negative width, when i expected 2.

Code: Select all

local aTable = {}
    local bTable = {}
    local cTable = {}
    for y, xs in ipairs( instance.map.collision ) do 
        for x, value in ipairs(xs) do 
            if value == "a" then  
                local a = {aX = x, aY = y}
                table.insert(aTable, a)
            end
            if value == "b" then 
                local b = {bX = x, bY = y}
                table.insert(bTable,  b)
            end
            if value == "c" then 
                local c = {cX = x, cY = y}
                table.insert(cTable,  c) 
            end
        end
    end

    for i, a in ipairs(aTable) do 
        for i, b in ipairs(bTable) do 
            for i, c in ipairs(cTable) do
                if a.aY == b.bY and b.bX == c.cX then
                    local body = love.physics.newBody(world, a.aX * instance.map.tile_width - (b.bX - a.aX) / 2, a.aY * instance.map.tile_width - ( c.cY - b.bY)  / 2)
                    local s = love.physics.newRectangleShape((b.bX - a.aX) *  instance.map.tile_width,( c.cY - b.bY) * instance.map.tile_height)
                    love.physics.newFixture(body, s)
                    print("Collider at X/Y:" .. a.aX * 16 .. "/" .. a.aY * 16  .. "with the width/height:" .. (b.bX - a.aX) *  instance.map.tile_width .. "/" .. ( c.cY - b.bY) * instance.map.tile_height )
                end
            end
        end
Thanks! :nyu:
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Problems with "Sorting" algorythym

Post by Andlac028 »

So, it gives you 4 colliders because first a and b is paired with both c and second a and b is also paired with both c and there is no way, you can tell which c belongs to which a and b in some cases (like with first a and b). If you want to store colliders in another way, you can for example make each collider a unique string and put it in the table 2 times - one at x1 and y1 and second on x2 and y2 and then you just create colliders from them

Code: Select all

grid = {
  {"a", 0  , 0  , 0  , 0  },
  {0  , "b", 0  , 0  , 0  },
  {0  , 0  , 0  , "a", 0  },
  {0  , 0  , 0  , 0  , "b"}
}

local colliders = {}

for y, xs in ipairs(grid) do
  for x, value in ipairs(xs) do
    if value ~= 0 then
      if collides[value] then
        table.insert(colliders[value], {x, y})
      else
        colliders[value] = {x, y}
      end
    end
end

for name, coords in pairs(colliders) do
  -- now you have name = name of the collider, like "a" or "b" and pairs of coordinates
 
  -- there you can define x1 = coords[1][1], y1 = coords[1][2], x2 = coords[2][1] and y2 = coords[2][2]
  -- and then you can create your body
  -- or if you need width = x2 - x1 and height = y2 - y1
  
  -- for example, for name = "a", you will have
  -- x1 = 1, y1 = 1, x2 = 4, y2 = 3, width = 2, height = 3
  
  -- maybe it will be better to add some checks in case, there will be only one coords for some reason (like bad map)
end
Edit: Added example
Last edited by Andlac028 on Tue Sep 06, 2022 7:23 pm, edited 1 time in total.
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Re: Problems with "Sorting" algorythym

Post by NoreoAlles »

Andlac028 wrote: Tue Sep 06, 2022 7:02 pm So, it gives you 4 colliders because first a and b is paired with both c and second a and b is also paired with both c and there is no way, you can tell which c belongs to which a and b in some cases (like with first a and b). If you want to store colliders in another way, you can for example make each collider a unique string and put it in the table 2 times - one at x1 and y1 and second on x2 and y2 and then you just create colliders from them
Thats so much easier, thanks. :awesome:
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Problems with "Sorting" algorythym

Post by Andlac028 »

Also if your grid will be bigger, it would be maybe better to directly store coordinates of each collider instead of storing it in grid, as for example if grid will be 1 000 × 1 000, it would need to check 1 000 000 cells and it may take some time to just get the coordinates of these colliders
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests