• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by seandolan · May 19, 2018 at 08:12 PM · array3dinttileswalls

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:

Top Image without Walls, Bottom Image with Walls

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.

tiles.png (5.0 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iJuan · May 19, 2018 at 09:11 PM 0
Share

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

6 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image seandolan · May 19, 2018 at 08:48 PM 0
Share

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?

avatar image Cherno seandolan · May 19, 2018 at 09:27 PM 0
Share

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
              }
      }
 }


avatar image seandolan Cherno · May 19, 2018 at 09:31 PM 0
Share

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.

Show more comments
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image barrysingh101 · Nov 06, 2019 at 02:39 PM 0
Share

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.

avatar image
-1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

https://jiofilocalhtml.run https://forpc.onl

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Convert class of int vars into an array 0 Answers

pick a random int with the value of 1 from an array 2 Answers

Search an array? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges