Creating Geometry from a table of tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Creating Geometry from a table of tables

Post by NoreoAlles »

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.

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 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.

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])
I really need some help as i cant wrap my head around this kind of stuff.
Thanks ^^
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
pgimeno
Party member
Posts: 3691
Joined: Sun Oct 18, 2015 2:58 pm

Re: Creating Geometry from a table of tables

Post by pgimeno »

You're making local a, b, and c be tables that are not indexed by numbers, but by fields aX, aY, bX, bY, cX and cY. You insert them into aTable, bTable and cTable, so aTable[1] is a table with fields aX and aY, but when you try to print aTable[1][1], there is no element with numeric index 1.

What you need to print is:

Code: Select all

print(aTable[1].aX, aTable[1].aY)
By the way, notice that you have a copy-paste typo when inserting in cTable: table.insert(cTable, a) should be table.insert(cTable, c) instead.

Other than these two details, your code is fine.
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Re: Creating Geometry from a table of tables

Post by NoreoAlles »

pgimeno wrote: Sat Sep 03, 2022 7:32 pm You're making local a, b, and c be tables that are not indexed by numbers, but by fields aX, aY, bX, bY, cX and cY. You insert them into aTable, bTable and cTable, so aTable[1] is a table with fields aX and aY, but when you try to print aTable[1][1], there is no element with numeric index 1.

What you need to print is:

Code: Select all

print(aTable[1].aX, aTable[1].aY)
Thanks :awesome:
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests