Is this a good way to handle tile collision and generation?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
rocetti
Prole
Posts: 4
Joined: Fri Nov 08, 2013 8:26 pm

Is this a good way to handle tile collision and generation?

Post by rocetti »

Hi :)
Im creating a roguelike so its going to have random map generation and gridlock tile movement.

I was trying to figure out a good way to handle map creation with expandable tileset types. What i need is to know If the following mindset is good or if there any improvement/better way.

Basically im thinking about creating a grid that generates the maps tileset so It will store numbers that represent wich tile should be rendered. And at the same time i will create a grid that stores collision values (0,1&2) so when a wall/object is generated It changes the value of collision to 1 and when a NPC is generated its 2. (People can swap places and objects/walls don't)

So is this a good way or should i use other method for It?

(Tutorials on tileset, tile collision and random maps are not specific and im quite a "noob" so i wasnt sure If my Idea is good and efficient. Im thinking on only render a little bit more than a screen.)
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: Is this a good way to handle tile collision and generation?

Post by mr_happy »

There are several ways to handle this, depending upon how complex you want to make things. I've found that a good idea for quick prototyping is to just have the tile index also indicate whether or not a tile is 'passable'. Imagine you've got eight 64x64 tiles in a 'strip' image (atlas, tileset whatever you want to call it) ; tiles 0 - 3 might represent grass, paths etc whereas tiles 4-7 might represent walls, trees, water etc. It's easy to declare a 2d table of your map and instantly see which parts are walkable or impassable and test your code accordingly. You can also avoid out of bounds array errors without writing any code by visually 'walling in' your map.
User avatar
NotARaptor
Citizen
Posts: 59
Joined: Thu Feb 22, 2018 3:15 pm

Re: Is this a good way to handle tile collision and generation?

Post by NotARaptor »

These articles are really old (like me), but do cover some interesting approaches in dealing with tile-based games, walkablility, layering and so on.

They're not Love2d or even Lua-based, but the concepts are applicable to anything.

https://www.gamedev.net/articles/progra ... n-12-r728/

http://www-cs-students.stanford.edu/~am ... etech.html
rocetti
Prole
Posts: 4
Joined: Fri Nov 08, 2013 8:26 pm

Re: Is this a good way to handle tile collision and generation?

Post by rocetti »

Thanks guys!!
mr_happy wrote: Sat May 12, 2018 6:09 pm tiles 0 - 3 might represent grass, paths etc whereas tiles 4-7 might represent walls, trees, water etc. It's easy to declare a 2d table of your map and instantly see which parts are walkable or impassable and test your code accordingly.
i know its easy to read If the tile is walkable but how would i check for entities collision? How would i know that there was not a monster in x30 y56 since the entities are not inside the generated map table?
I could check for on screen entities one by one and check their coordinates but It would be better (performancewise) than having a second table that stores collision?
Or It would be better put an box collider on entities?

(Im Just playing around to learn more and more so ill end up testing this things and checking performance but i need to share and see what other people are doing >.<)
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: Is this a good way to handle tile collision and generation?

Post by mr_happy »

Each time you are about to move the player or npc just check that another entity doesn't have the same destination co-ords - as you're locking movement to a grid it's easy. As you say, keep your npc's coords in a table and iterate through it just before you move to check you're not trying to move into the same tile. How many npc's will you have? Unless it's an awful lot I doubt whether that method will be too slow. You could possibly optimise the number of checks by sorting in advance and inserting/removing in the correct place thereafter.

Alternatively, you might be able/want to put your entities in the map table by adding another field: map[x][y][enitityID] - check it before moving and make it nil when you move out. You could even have more than one in the same square by making that field a table: map[x][y][enitityID] = {99, 5, 207}

It all depends upon how you see the game panning out; the number tile layers, the number of npcs, the number of fixed/dropable objects, whether items stack, interactive items and so on. I would go for the simplest approach rather than get bogged down trying to think of all the possibilities at the outset :D
rocetti
Prole
Posts: 4
Joined: Fri Nov 08, 2013 8:26 pm

Re: Is this a good way to handle tile collision and generation?

Post by rocetti »

Never tought on a third "dimension" inside the map table... Haha .. was stuck with arrays in mind that i forgot that lua is a table language. You just blew my mind there.
(I hate the Idea of Scan all coords in a list. Even in small projects.. I feel more comfortable with just checking the exact place you are moving in. Just personal preference in logic)
My Idea is to create a simple random dungeon and world generator that i can work with in various projects. The most basic one is Just a dungeon crawl and there is one more complex that need 9k/12kx tiles world generation. But at the end they are just an excuse to learn coding for games (RPGs and Roguelikes more specifically right now). So right now its like im working into an procedural framework.
Thanks a Lot.
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: Is this a good way to handle tile collision and generation?

Post by mr_happy »

The trouble with putting them in the map is it's more fiddly if you want to iterate through all or some of them (what if you wanted to change the status of all npcs?) plus you're probably going to have them in their own list anyway to store other info about them.

As an aside, I've made a few dungeon generators in the past and a fantastically simple world generator for a roguelike which works surprisingly well - here's an output example (without any height detail or 'biomes' shown) the white pixels you might see are ports. It's fun tweaking the algorithms to make continents, islands etc.
world.png
world.png (399.66 KiB) Viewed 5186 times
rocetti
Prole
Posts: 4
Joined: Fri Nov 08, 2013 8:26 pm

Re: Is this a good way to handle tile collision and generation?

Post by rocetti »

Hahaha awesome!
Did you used any noise algorithm idea or you created with a different method? (They look a little like they are circles joining. Maybe its coincidence)
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: Is this a good way to handle tile collision and generation?

Post by mr_happy »

rocetti wrote: Sun May 13, 2018 5:25 pm (They look a little like they are circles joining. Maybe its coincidence)
It's a little like that when run in its basic form but you can change various factors to radically alter the map. The game I was working on (before it began to get too complex!) used it to generate two continents with an ocean and a few islands in between - it also generated different terrain features. I used the same method in this (also semi-abandoned) project, although it has smooth pixel scrolling/movement not locked to the grid (sorry about the impassable red tile graphic!):
ss1.jpg
ss1.jpg (57.13 KiB) Viewed 5154 times
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 32 guests