Creating Geometry from a table of tables
Posted: Sat Sep 03, 2022 6:43 pm
Hey, i struggle with stuff like this and i am asking myself how to safe 3 points in a 2d grid/ table of table to later create Box2d geometry from
it.
I would like to save the x and y of point "a", "b" and "c" and compare the y coordinate of "a" and "b" and the x coordinate of "b" and "c" and if they are the same to use them for a box2d rectangle, where the "a" x and y is the x and y of the rectangle and the difference of the x coordinate of "a" and "b" is the width and the difference in y from "b" to "c" as height.
I tried to atleast save "a", "b" and "c" to a table but i cant even do that, as the print() function returns nil.
I really need some help as i cant wrap my head around this kind of stuff.
Thanks
it.
Code: Select all
grid = {
{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 ,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 ,0 ,0 ,0 ,0 ,0 ,"a",0 ,0 ,0 ,0 ,0 ,"b",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 ,},
{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 ,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 ,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 ,},}
I tried to atleast save "a", "b" and "c" to a table but i cant even do that, as the print() function returns nil.
Code: Select all
for y, xs in ipairs(grid ) do
for x, value in ipairs(xs) do
if value == "a" then
print("a is at x/y:" .. x .. "/" .. y)
local a = {aX = x, aY = y}
table.insert(aTable, a)
end
if value == "b" then
print("b is at x/y:" .. x .. "/" .. y)
local b = {bX = x, bY = y}
table.insert(bTable, b)
end
if value == "c" then
print("c is at x/y:" .. x .. "/" .. y)
local c = {cX = x, cY = y}
table.insert(cTable, a)
end
end
end
print(aTable[1][1])
Thanks