0

I'm trying to draw a hexagonal grid with love2d using love.graphics.line but after drawing around 15000 lines the program crashes with only one message abort (core dumped).

function love.load()
    ww, wh = love.graphics.getDimensions()
    hexradius = 10
    size = 50

    hexgrid = newGrid(hexradius, size, size) 
end

function love.draw(dt)
    drawgrid()
end

function drawgrid()
    local jxOffset = hexgrid.rad * -math.tan(math.pi/1.5) 
    local ixOffset = jxOffset/4
    local iyOffet = jxOffset * math.sin(math.pi/3)


    for i=1,hexgrid.size.x do
        for j=1,hexgrid.size.y do
            love.graphics.push()
        
            love.graphics.translate(ixOffset + j * jxOffset, i * iyOffet)
            love.graphics.line(
                hexgrid.hex[1].x, hexgrid.hex[1].y,
                hexgrid.hex[2].x, hexgrid.hex[2].y,
                hexgrid.hex[3].x, hexgrid.hex[3].y,
                hexgrid.hex[4].x, hexgrid.hex[4].y,
                hexgrid.hex[5].x, hexgrid.hex[5].y,
                hexgrid.hex[6].x, hexgrid.hex[6].y,
                hexgrid.hex[1].x, hexgrid.hex[1].y)

            love.graphics.pop()
        end

        ixOffset = -ixOffset
    end
end

function newGrid(rad, xsize, ysize)
    local g = {
        rad = rad,
        hex = {},
        size = {
            x = xsize,
            y = ysize,
        },
    }

    for i=1,6 do
        local dir = math.pi/3 * (i+0.5)

        g.hex[i] = {}
        g.hex[i].x = g.rad * math.cos(dir)
        g.hex[i].y = g.rad * math.sin(dir)
    end

    return g
end

I'm quite new to love2d and graphical stuff in general so maybe I'm trying to draw a great amount of lines and it's just not possible, but the grid I'm generating does not appear to be that big. I'm using LOVE 11.3.

Thanks in advance!

argot
  • 77
  • 7
  • Usually, a core dump indicates a bug in the host program. Consider filing a bug report at: https://github.com/love2d/love/issues. Also I can't reproduce this on LOVE 11.1, so it's possible that installing an older version might help. – luther May 12 '21 at 23:29
  • you were on the right track! – argot May 14 '21 at 03:05

2 Answers2

0

I'd try drawing with a polygon instead, see if you have better results.

love.graphics.polygon( mode, vertices )

https://love2d.org/wiki/love.graphics.polygon

for i = 1, hexgrid.size.x do
    for j = 1, hexgrid.size.y do
        love.graphics.push()

        love.graphics.translate( ixOffset +j *jxOffset,  i *iyOffet )
        love.graphics.polygon( 'line', 
            hexgrid.hex[1].x,  hexgrid.hex[1].y,
            hexgrid.hex[2].x,  hexgrid.hex[2].y,
            hexgrid.hex[3].x,  hexgrid.hex[3].y,
            hexgrid.hex[4].x,  hexgrid.hex[4].y,
            hexgrid.hex[5].x,  hexgrid.hex[5].y,
            hexgrid.hex[6].x,  hexgrid.hex[6].y  )

        love.graphics.pop()
    end
...

Alternatively, circles drawn with optional segment parameter set @ 6, draws hexagons.

love.graphics.circle( mode, x, y, radius, segments )

https://love2d.org/wiki/love.graphics.circle

for i = 1, hexgrid.size.x do
    for j = 1, hexgrid.size.y do
        love.graphics.circle( 'line', ixOffset +j *jxOffset, i *iyOffet, hexradius, 6 )
    end
...
Doyousketch2
  • 2,060
  • 1
  • 11
  • 11
0

As Luther suggested in a comment it's a bug in Löve. It's already fixed and will be available in the next release Löve 11.4.

Thanks!

argot
  • 77
  • 7