- Home /
How do you calculate walls between tiles in a 3d environment?
I have an int[9,8]tiles that stores numbers from 1 to 9 depending on a previous selection by the player. So for example:
// 1 = blue tile, 2 = yellow tile, 3 = red tile
tiles[0,0] = 1;
tiles[0,1] = 1;
tiles[0,2] = 1;
.
.
tiles[2,0] = 2;
tiles[2,1] = 2;
tiles[2,2] = 2;
.
.
tiles[5,0] = 3;
tiles[5,1] = 3;
tiles[5,1] = 3;
// Which looks something like this in the diagram below:
// 1 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1 1
// 2 2 2 2 2 1 1 1 1
// 2 2 2 2 2 1 1 1 1
// 2 2 2 2 2 3 3 3 3
// 3 3 3 3 3 3 3 3 3
// 3 3 3 3 3 3 3 3 3
The result is something like this:
The red lines marked on the image are the walls that will go inbetween each of the colour areas. I know how to loop through each tile and work out if the tile next to it in any of the four directions (North, South, East, West) is a different colour and hence a wall is required. I don't know an efficient way to store the information and cancel out any walls that are found from looping through every tile.
Obviously the first time I scan a tile with the one below registering a different colour will indicate a wall. But then when I look at that 2nd tile in my loop, it will also register the need for a wall. So 2 walls will be created.
I would like to know if there is any easy way to calculate this wall information another way, but more importantly how can I store this information in say another int[,] array so I can generate the walls from there. Every tile in my game is a 1x1 quad rotated to point upwards. So the int array relates to the X and Z coordinates of the tile itself.
Any help would be greatly appreciated. It's 6:12am and I have been up all night trying to figure this out for a game I am making.
Supposing your current map is stored in a [n,m] array, your walls should be stored in a [n+1, m+1] bool array, just imagine a copy of the current tile map, where every gap in the grid is a wall, and you displace it to the upper left corner by 1. This bool map would represent each tile's corner.
So, if you find that there's a wall between the cell [a, b] and the cell [c,d], the x coord of the first cell will be equals to the x coord of the second cell, or the y coord of the first cell will be equal to the y coord of the second cell. You should find wether x1 = x2 or y1 = y2.
Once you found that, you should take the max value of the different coords, that max value will be the value you will use in that coord. Then, on the equal coords, you will take any value (as they are equal), and that value +1. You should have now 1 value in one coord, and 2 values in another one. Those define the two corners of the wall, which you should store in your bool array.
An Example:
Imagine you found a wall between [1,2] and [1,3]. x1 = x2 = 1, so your corner's x coords would be 1 and 2 (1+1). On the y coords, you will take the greater value, which is 3. Your wall would go from [1,3] to [2,3], and those are the values you have to set to true in your bool map.
Drawing the wall from the corners should be very intuitive, but if you don't know how, just ask.
There surely are ways on optimizing this, like using graphs path algorithms (Floyd algorithm, i.e), but they imply some great math knowledge, and aren't worth it for your implementation.
So, if there's a wall between [1, 2] and [1,3], you would
Answer by Cherno · May 19, 2018 at 08:22 PM
One solution would be to store the wall information in a four-dimensional bool-array. Since a wall sits between two tiles, you need the coordinates of two neighboring tiles. The dimensions or the array would be:
0: tile A x-coordinate 1: tile A y-coordinate 2: tile B x-coordinate 3: tile B y-coordinate
The array would have lengths equal to the x and y lengths of your grid + 1 (for the non-existent tile at max + 1 coordinates, which doesn't have to correspond to tiles for the process of checking for walls). For border cases, you would always set the bool to true instead of attempting to access the tiles array which would be out of bounds).
if(tiles[0,3] == blue && tiles[0,4] != blue) //pseudocode obviously
{
walls[0,3,0,4] = true;
}
Thanks for your comment. I am not sure if it's just that I misunderstand but wouldn't something like this still generate 2 walls on the same points. For example wall[0,3,0,4] = true but also wall[0,4,0,3] = true. So 2 walls would be placed if running through the code to generate the walls?
It doesn't matter because you would just check both cases at the same time ins$$anonymous$$d of each one separately.
if(walls[0,3,0,4] == true || walls[0,4,0,3] == true)
{
Generate wall
}
Also, since in your loop, you only count upwards, you can't get this situation anyway. The first coordinate value will always be lower than the second.
for(int x = 0; x < walls.GetLength(0) - 1; x++)
{
for(int y = 0; y < walls.GetLength(1) - 1; y++)
{
if(x == 0)
{
Generate west border wall
}
if(y == 0)
{
Generate south border wall ($$anonymous$$ake America Great Again!)
}
if(walls[x,y,x,y+1] == true)
{
Generate north wall
}
if(walls[x,y,x + 1,y] == true)
{
Generate east wall
}
}
}
I am generating all the walls in one hit. So when looping through the wall generation in your example it looks like I am back at the same problem. If I loop to the point of reaching [0,3] I could check wall [0,3,0,4] and [0,4,0,3] and generate a wall. Then when I get to [0,4] I would then be checking again both [0,3,0,4] and [0,4,0,3] and generating a 2nd wall. $$anonymous$$aybe I am missing something. I appreciate your help on this. Thank you so much.
Answer by Lardalot · May 20, 2018 at 07:39 AM
You could just only check to places walls on the bottom or right as you scan your 2D array. That way you wont get duplicates.
Also always place walls on the top of row 0 or left of column 0.
Tiles by Cadigo is a 3D modeling program for kids that lets the wee tykes drop blocks, walls, and pixels into an environment and then output the object for 3D printing. Designed to be dead simple, you basically create a shape in 2D and then “extrude” it into 3D, adding features as you build.
Answer by abedathman8 · May 20, 2018 at 02:25 AM
In the tile-based approach, each visual element is broken down into smaller pieces, called tiles, of a standard size. These tiles will be arranged to form the game world according to pre-determined level data - usually a 2D array. Tutuapp 9Apps Aptoide
Answer by xpaceone · Oct 01, 2020 at 08:59 AM
To create objects you simply place LEGO-style bricks in virtual space. Once you’ve built your model you can export to an STL file for printing on a Makerbot or similar printer. Aimed at the very young, Tiles offers a number of building styles including additive building by dropping blocks and “chip-way” that lets you remove material from a larger block. Unlike tools like Printcraft, which translate game worlds into STL files, this is a true CAD tool, albeit one with considerably reduced complexity. https://forpc.onl https://jiofilocalhtml.run
,To create objects you simply place LEGO-style bricks in virtual space. Once you’ve built your model you can export to an STL file for printing on a Makerbot or similar printer. Aimed at the very young, Tiles offers a number of building styles including additive building by dropping blocks and “chip-way” that lets you remove material from a larger block. Unlike tools like Printcraft, which translate game worlds into STL files, this is a true CAD tool, albeit one with considerably reduced complexity.
Answer by chavsur · Dec 02, 2021 at 05:55 PM
You could just only check to places walls on the bottom or right as you scan your 2D array. That way you wont get duplicates. vidmate save insta
Your answer
Follow this Question
Related Questions
Store map as int array and load/save 1 Answer
New Object Instantiation from Array working in Start() but not Update() 0 Answers
Array Is Out Of Range 2 Answers
Bottom-texture of code generated cube does not draw 1 Answer
Array in an array? 2 Answers