tileGrid function

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Griff
Prole
Posts: 7
Joined: Sat Feb 03, 2024 9:46 pm

tileGrid function

Post by Griff »

I recently came across a video creating a falling sand simulator in p5, I figured löve has the resources that I could do something similar, all I managed to accomplish this far is the tile layout but I split the generation of tiles into 2 functions one to define every tile in a table and the second function is to draw each tile. I have no idea how I’m going to use the tileGrid further I’m about 6 hours or so total into using löve so probably just me not knowing enough syntax. Anyways here’s the code dump if you’d like to see

To best use the functions defineTile() should be placed in love.load and drawTiles() in love.draw pass through same values for w, h on each function

Code: Select all

 tileGrid = {

  tile = {},
  
  defineTile = function(w, h)

    sw, sh = love.window.getMode()

    tileWidth = sw/w
    tileHeight = sh/h

    num = 0

    for j = 0, h-1 do
    
      for i = 0, w-1 do

        num = num + 1

        table.insert(tileGrid.tile, num, {tileWidth * i, tileHeight * j, false})

      end

    end

    return tileWidth, tileHeight

  end,

  drawTile = function(w, h)

    sw, sh = love.window.getMode()

    tileWidth = sw/w
    tileHeight = sh/h

    num = 0

    for j = 0, h-1 do

      for i = 0, w-1 do

         num = num + 1

        love.graphics.setColor(i/w, j/h, 1)

        rect = {
          love.graphics.rectangle("line", tileWidth * i, tileHeight * j, tileWidth, tileHeight),
          love.graphics.print(num, tileWidth * i, tileHeight * j)
        }

      end

    end

  end
      
}
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: tileGrid function

Post by dusoft »

I think https://love2d.org/wiki/ParticleSystem would be good for falling sand simulation.

Sure, you can do it with tiles as well, but the physics will get difficult.
Griff
Prole
Posts: 7
Joined: Sat Feb 03, 2024 9:46 pm

Re: tileGrid function

Post by Griff »

dusoft wrote: Sat Feb 03, 2024 11:38 pm I think https://love2d.org/wiki/ParticleSystem would be good for falling sand simulation.

Sure, you can do it with tiles as well, but the physics will get difficult.
I figured I was making my life hard, don't really know how to use the particle system so time for the books
User avatar
knorke
Party member
Posts: 239
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: tileGrid function

Post by knorke »

I think https://love2d.org/wiki/ParticleSystem is not suitable for such simulation.
It is a powerful system and can be used for nice effects but (as far as I am aware) there is no way to read or set the position of individual particles. So it is impossible to check for collisions between particles.

Often these falling sand games do not use "real" particles anyway. Instead they use a form of cellular automaton, think Conway's game of life. https://en.wikipedia.org/wiki/Cellular_automaton
Your code/tutorial seems to use that approach. What are you having problems with?
Griff
Prole
Posts: 7
Joined: Sat Feb 03, 2024 9:46 pm

Re: tileGrid function

Post by Griff »

knorke wrote: Sun Feb 04, 2024 12:40 pm I think https://love2d.org/wiki/ParticleSystem is not suitable for such simulation.
It is a powerful system and can be used for nice effects but (as far as I am aware) there is no way to read or set the position of individual particles. So it is impossible to check for collisions between particles.

Often these falling sand games do not use "real" particles anyway. Instead they use a form of cellular automaton, think Conway's game of life. https://en.wikipedia.org/wiki/Cellular_automaton
Your code/tutorial seems to use that approach. What are you having problems with?
No problems here! Was just sharing my functions for creating a grid. Thanks for the heads up.
Post Reply

Who is online

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