Mesh help required

After reading loads of threads and also googled about meshes, I am trying ( not very well) to generate some hexagon meshes circle a center hex. The code below generates the hexes but try as I can I can not get them to be drawn on the screen all together. Mod the code to generate one and it displays but not all will display.

Please can some one point me in the direction I am going wrong.

Thanks

-- hexagon grid test
-- mesh testing 2

supportedOrientations(LANDSCAPE_ANY)

function setup()
    mtab = {}
    X={}    -- x coord table
    Y={}    -- y coord table
    s=30    -- size of hex 30 its finger size
    w = s*2    -- width of hex
    h = math.sqrt(3)/2*w -- height of hex
    hor = w+10
    ver = h+10
    cx=376    -- start position of x
    cy=384    -- start position of y

    mtab={}
    --m = mesh()
    
    --for j=1,7 do
    j= math.random(1,7)
        m = mesh()
        ctx={cx,cx+ver,cx+ver,cx-ver,cx-ver,cx-ver,cx+ver}
        cty={cy,cy,cy+hor,cy+hor,cy,cy-hor,cy+hor}
        cxx=ctx[j]
        cyy=cty[j]
        for i = 1,6 do
            print ("center = ",cxx,cyy)
            angle = 2 * math.pi/ 6 * i
            xx = cxx + s * math.cos(angle)
            table.insert(X,xx)
            yy = cyy + s * math.sin(angle)
            table.insert(Y,yy)
        end
        m.vertices= {
        vec2(cxx,cyy),vec2(X[1],Y[1]),vec2(X[2],Y[2]),
        vec2(cxx,cyy),vec2(X[2],Y[2]),vec2(X[3],Y[3]),
        vec2(cxx,cyy),vec2(X[3],Y[3]),vec2(X[4],Y[4]),
        vec2(cxx,cyy),vec2(X[4],Y[4]),vec2(X[5],Y[5]),
        vec2(cxx,cyy),vec2(X[5],Y[5]),vec2(X[6],Y[6]),
        vec2(cxx,cyy),vec2(X[6],Y[6]),vec2(X[1],Y[1])}
        table.insert(mtab,m.vertices)
    --end
    
    
end

function draw()
    background(78, 78, 94, 255)
    
    for i,v in ipairs(mtab) do
        m:draw()
    end

end

@Jazmal, you store m.vertices into a table

But m is one full mesh, so in the draw function, without using any table, you could just do m:draw()

If it’s only 1 hexagon then you don’t need to store it into a table

I tried to create 7 hex meshes, but only one gets drawn. I commented the for loop out and just made the varable j static for testing.
I am hoping to be able to create a total of 126 hexes all centered around the first one, I can create one but having trouble drawing more. If you have any solutions to my problem I would be grateful.

Thanks

@Jazmal Not sure what you’re trying to do, but I changed it so it draws individual hexagons. You can take it from here.

EDIT: Added for loops to calculate hexagon positions.


-- hexagon grid test
-- mesh testing 5

supportedOrientations(LANDSCAPE_ANY)

function setup()
    tab={}
    table.insert(tab,vec2(0,0))

    for z=0,360,60 do
        table.insert(tab,vec2(math.sin(math.rad(z))*50,math.cos(math.rad(z))*50))
        table.insert(tab,vec2(math.sin(math.rad(z))*100,math.cos(math.rad(z))*100))
        table.insert(tab,vec2(math.sin(math.rad(z))*150,math.cos(math.rad(z))*150))
        table.insert(tab,vec2(math.sin(math.rad(z))*200,math.cos(math.rad(z))*200))
        table.insert(tab,vec2(math.sin(math.rad(z))*250,math.cos(math.rad(z))*250))
    end 

    for z=14,374,60 do
        table.insert(tab,vec2(math.sin(math.rad(z))*180,math.cos(math.rad(z))*180))
    end  

    for z=19,379,60 do
        table.insert(tab,vec2(math.sin(math.rad(z))*132,math.cos(math.rad(z))*132))
    end  

    for z=30,390,60 do
        table.insert(tab,vec2(math.sin(math.rad(z))*86,math.cos(math.rad(z))*86))
        table.insert(tab,vec2(math.sin(math.rad(z))*172,math.cos(math.rad(z))*172))
    end  

    for z=41,401,60 do
        table.insert(tab,vec2(math.sin(math.rad(z))*132,math.cos(math.rad(z))*132))
    end 

    for z=46,383,60 do
        table.insert(tab,vec2(math.sin(math.rad(z))*180,math.cos(math.rad(z))*180))
    end  
    
    nbr=#tab      
    mtab = {}
    s=25    -- size of hex 30 its finger size
    w = s*2    -- width of hex
    h = math.sqrt(3)/2*w -- height of hex
    hor = 0.75*w+5 -- horizontal spacing
    ver = h+5    -- vertical spacing
    for j=1,nbr do
        mtab[j]=mesh()
        X={}
        Y={}
        cxx=tab[j].x+WIDTH/2
        cyy=tab[j].y+HEIGHT/2
        for i = 1,6 do
            angle = 2 * math.pi/ 6 * i
            xx = cxx + s * math.cos(angle)
            table.insert(X,xx)
            yy = cyy + s * math.sin(angle)
            table.insert(Y,yy)
        end
        mtab[j].vertices= {
        vec2(cxx,cyy),vec2(X[1],Y[1]),vec2(X[2],Y[2]),
        vec2(cxx,cyy),vec2(X[2],Y[2]),vec2(X[3],Y[3]),
        vec2(cxx,cyy),vec2(X[3],Y[3]),vec2(X[4],Y[4]),
        vec2(cxx,cyy),vec2(X[4],Y[4]),vec2(X[5],Y[5]),
        vec2(cxx,cyy),vec2(X[5],Y[5]),vec2(X[6],Y[6]),
        vec2(cxx,cyy),vec2(X[6],Y[6]),vec2(X[1],Y[1])}
    end
end

function draw()
    background(78, 78, 94, 255)
    for j=1,nbr do
        mtab[j]:draw()
    end
end

Thanks very much @dave1707 that will give me a good start to doing all 126 hexes and thanks also for pointing out the x and y offsets, I will have to draw it out on graph paper to get the maths correct. Eg. Start with 1 hex at the center and all the rest circle the center in rings.

@dave1707 + loads

@dave 1707 just completed your alterations to my code and this is the draft results including the corrections.



-- hexagon grid test
-- mesh testing 5

supportedOrientations(LANDSCAPE_ANY)

function setup()
    mtab = {}
    s=30    -- size of hex 30 its finger size
    w = s*2    -- width of hex
    h = math.sqrt(3)/2*w -- height of hex
    hor = 0.75*w+5 -- horizontal spacing
    ver = h+5    -- vertical spacing
    cx=376    -- start position of x
    cy=384    -- start position of y
    cty={cx,cx+ver,cx+(ver/2),cx-(ver/2),cx-ver,cx-(ver/2),cx+(ver/2)}
    ctx={cy,cy,cy-hor,cy-hor,cy,cy+hor,cy+hor}

    for j=1,7 do
        mtab[j]=mesh()
        X={}
        Y={}
        cxx=ctx[j]
        cyy=cty[j]

        for i = 1,6 do
            angle = 2 * math.pi/ 6 * i
            xx = cxx + s * math.cos(angle)
            table.insert(X,xx)
            yy = cyy + s * math.sin(angle)
            table.insert(Y,yy)
        end
        mtab[j].vertices= {
        vec2(cxx,cyy),vec2(X[1],Y[1]),vec2(X[2],Y[2]),
        vec2(cxx,cyy),vec2(X[2],Y[2]),vec2(X[3],Y[3]),
        vec2(cxx,cyy),vec2(X[3],Y[3]),vec2(X[4],Y[4]),
        vec2(cxx,cyy),vec2(X[4],Y[4]),vec2(X[5],Y[5]),
        vec2(cxx,cyy),vec2(X[5],Y[5]),vec2(X[6],Y[6]),
        vec2(cxx,cyy),vec2(X[6],Y[6]),vec2(X[1],Y[1])}
    end
end

function draw()
    background(78, 78, 94, 255)
    for j=1,7 do
        mtab[j]:draw()
    end
end

Thanks for all your help

@Jazmal The output looks a lot better. Next step is a loop to calculate the position of all the other objects.

it even makes the 50 line competition :). always nice to see a hexagon.

If you want to cheat you can use triangulate :slight_smile:

function setup()
    mtab = {}
    s=30    -- size of hex 30 its finger size
    w = s*2    -- width of hex
    h = math.sqrt(3)/2*w -- height of hex
    hor = 0.75*w+5 -- horizontal spacing
    ver = h+5    -- vertical spacing
    cx=376    -- start position of x
    cy=384    -- start position of y
    cty={cx,cx+ver,cx+(ver/2),cx-(ver/2),cx-ver,cx-(ver/2),cx+(ver/2)}
    ctx={cy,cy,cy-hor,cy-hor,cy,cy+hor,cy+hor}

    for j=1,7 do
        local p = vec2(ctx[j],cty[j])
        local steps, radius, vs = 6, s, {}
        for i=1,steps do
            local a = i/steps * math.pi * 2
            table.insert(vs, p+vec2(math.cos(a), math.sin(a)) * radius)
        end
        m = mesh()
        m.vertices = triangulate(vs)
        mtab[j]=m
    end
end

function draw()
    background(78, 78, 94, 255)
    for j=1,7 do
        mtab[j]:draw()
    end
end

what are you building? :slight_smile:

@Jazmal I changed my code above to calculate the positions of more hexagons. Depending on how well you know math, you can add more “for” loops to finish how many hexagons you want to go to.

@tnlogy I am trying to make an rpg app for a game called Traveller (T20 version) and would like to create a jump map showing all available worlds within reach of their ship jump drive. I can use api and get the info from a web site( look nicer but I wanted to try and do this myself with help from you guys when I get stuck which will be a lot), so far I have broken the project into sections and when I have finished the sections and they workish I will try and join them together, I hope…

@dave1707 thanks for the update and maths is not my strong subject, but will read your code and try to understand how you went about it.

@Jazmal I’m not sure if you’re trying to fill the screen with hexagons. If you are, then this is an easier way than what I was doing above. If not, then maybe it would be easier to create smaller square groups instead of circular ones. Uses some of @tnlogy code.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    mtab = {}
    s=30
    w = s*2    -- width of hex
    h = math.sqrt(3)/2*w -- height of hex
    hor = 0.75*w+5 -- horizontal spacing
    ver = h+5    -- vertical spacing
    local count=0
    local offset=0
    for y=0,27 do
        for x=0,10 do
            count=count+1
            local vs={}
            local p=vec2(x*92+offset+29,27*y+19)
            for i=1,6 do
                a = i/6 * math.pi * 2
                table.insert(vs, p+vec2(math.cos(a), math.sin(a)) * s)
            end
            m = mesh()
            m.vertices = triangulate(vs)
            mtab[count]=m
        end
        if offset==0 then
            offset=46
        else
            offset=0
        end
   end
end

function draw()
    background(40,40,50)
    for j=1,#mtab do
        mtab[j]:draw()
    end
end

Sorry all if I did not give you all a better understanding, the hexes need to be arranged in rings around the center hex ( the position they are at ) and showing all available world within reach, to a max 6 hexes across ( 126 in the outer ring ) .

@Jazmal Don’t know if you can use this or not, but here is a program I wrote that will create a table of selected hexagon shapes. To use this, run the first program. Tap a hexagon near the center of the screen, it will turn red indicating it is the center hexagon. Tap the hexagons around the red circle that you want included, they will be green. A hexXY table will be created/updated in the output pane as you select each hexagon. Once you’re done selecting what you want, switch to the output pane and tap on the table. The background color will darken to indicate it was selected. The hexXY table can then be pasted into another program. I also included below one of the above programs with a hexXY table being used.


-- program to create hexXY table

displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)

function setup()
    gap=2
    size=30+gap
    sx=0
    sy=0
    hexTab={}
    mtab={}
    ctab={}
    xc=math.ceil(WIDTH/(size*3))
    yc=HEIGHT/(size)
    x1=0
    for y=0,yc do
        for x=0,xc do
            table.insert(ctab,vec2(3*x*size+x1,y*size*.866))
        end
        if x1==0 then
            x1=1.5*size
        else
            x1=0
        end
    end
    for z=1,#ctab do
        p=ctab[z]
        vs={}
        for i=1,6 do
            a = i/6 * math.pi * 2
            table.insert(vs, p+vec2(math.cos(a), math.sin(a)) * (size-gap))
        end
        m = mesh()
        m.vertices = triangulate(vs)
        mtab[z]=m
    end
end

function draw()
    background(40,40,50)
    fill(255)
    text("Tap output pane icon at the left to see the hexXY table",WIDTH/2,HEIGHT-50)
    for j=1,#mtab do
        mtab[j]:draw()
    end
    for a,b in pairs(hexTab) do
        fill(0,255,0)
        if b.x==sx and b.y==sy then
            fill(255,0,0)
        end
        ellipse(b.x,b.y,size)
    end
end

function touched(t)
    if t.state==BEGAN then
        for a,b in pairs(ctab) do
            if t.x>b.x-size/2 and t.x<b.x+size/2 and 
                t.y>b.y-size/2 and t.y<b.y+size/2 then
                if sx==0 and sy==0 then
                    sx=b.x
                    sy=b.y
                end
                table.insert(hexTab,vec2(b.x,b.y))
                output.clear()
                str="hexXY={"
                for c,d in pairs(hexTab) do
                    if c>1 then
                        str=str..","
                    end
                    str=str.."vec2("..(d.x-sx)..","..(d.y-sy)..")"
                end
                str=str.."}"
                print(str)
           end
        end
    end
end

One of the program from above using the hexXY table created from my program.


-- program to demo hexXY table

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    
hexXY={vec2(0,0),vec2(0,55.424),vec2(-48,27.712),vec2(-48,-27.712),vec2(0,-55.424),vec2(48,-27.712),vec2(48,27.712),vec2(0,110.848),vec2(-48,83.136),vec2(-96,55.424),vec2(-96,0),vec2(-96,-55.424),vec2(-48,-83.136),vec2(0,-110.848),vec2(48,-83.136),vec2(96,-55.424),vec2(96,0),vec2(96,55.424),vec2(48,83.136),vec2(0,166.272),vec2(-48,138.56),vec2(-96,110.848),vec2(-144,83.136),vec2(-144,27.712),vec2(-144,-27.712),vec2(-144,-83.136),vec2(-96,-110.848),vec2(-48,-138.56),vec2(0,-166.272),vec2(48,-138.56),vec2(96,-110.848),vec2(144,-83.136),vec2(144,-27.712),vec2(144,27.712),vec2(144,83.136),vec2(96,110.848),vec2(48,138.56)}


    mtab = {}
    s=30
    w = s*2    -- width of hex
    h = math.sqrt(3)/2*w -- height of hex
    hor = 0.75*w+5 -- horizontal spacing
    ver = h+5    -- vertical spacing
    local count=0
    local offset=0
    for y=1,#hexXY do
        local vs={}
        for i=1,6 do
            a = i/6 * math.pi * 2
            table.insert(vs, vec2(WIDTH/2,HEIGHT/2)+hexXY[y]+vec2(math.cos(a), math.sin(a)) * s)
            m = mesh()
            m.vertices = triangulate(vs)
            mtab[y]=m
        end
    end
end

function draw()
    background(40,40,50)
    for j=1,#mtab do
        fill(255)
        mtab[j]:draw()
    end
end

@dave1707 thanks that will save me a lot of calculating, also the HexXY starts with a vec2(0,0) ? Could you explain why this co ord is for the bottom left ?. Thanks again and I will adjust my code again to use some of your help, I like the idea of oly using 1 table.

Thanks again, I will post the code to share when it’s done.

@Jazmal The vec2(0,0) is the x,y value of the first hexagon that you select (red). All of the other vec2(x,y) values are in relation to the first hexagon. So if you want the pattern you created to draw at 300,300, then the vec2(0,0) would be at 300,300 and the other vec2() values would be added or subtracted from 300,300. So in the example above,

table.insert(vs,vec2(WIDTH/2,HEIGHT/2)+hexXY[y]+vec2(see above) 

the hexagon is being drawn centered on the screen. The vec2() values are being added or subtracted from the WIDTH/2,HEIGHT/2. So the first hexagon you select will be drawn at the x,y value you pick, with the other hexagons drawn around it.