Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Dudledok · Aug 14, 2014 at 11:40 AM · c#arrayloopnested2d array

Determining groups in a 2D array by checking neighbours (and their neighbours etc)

I am reading data from a text file which will have the layout for a level, for example:

 1 1 0 0 0 0 G G G
 1 1 0 0 0 0 R R R
 1 1 0 0 0 0 B B B
 1 1 0 0 0 0 G G G
 1 1 0 0 0 0 R R R
 1 1 0 0 0 0 B B B

The characters are read to determine what to place in that position in the scene, so a 1 is a standard grey platform, while R, G, and B represent coloured platforms. This works fine but each platform us made up of 1x1 squares which is later particularly inefficient when I change the platform colours as part of the game.

In order to reduce the number of platforms, I want to determine if the neighbouring characters in the text file are the same as the current one and form platforms based on that, so in the example above, there would be one standard grey platform 2 wide and 6 long.

Update:

When reading from the text file, I now check the next tile in the column to see if it is the same and scale up a 1x1 platform to whatever size is necessary, so in the example above, there would be 2 1x6 grey platforms, and 6 1x1 red, green, and blue platforms. I have done it this way because it is easy enough to check the rest of a column (I could also do rows but have started with columns) and then the shape of the platform will never matter.

My issue remains the same:

If I was the check all neighbours (so rows and columns), how would I go about determining the shape and position of the overall platform? And what if I have a non regular quadrilateral (e.g. with a hole in the middle, or perhaps a triangle, etc)? The issue here is I wouldn't be able to simply scale up a 1x1 platform. If that could somehow be created, that would be ideal, but if not, I would want to create the shape out of the fewest total number of platforms.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by smoggach · Aug 14, 2014 at 02:00 PM

This sounds like a job for a recursive algorithm and a data structure.

Your data structure should contain whatever 'tileBlue', 'tileGreen', etc... are as well as a boolean (have i been checked?)

Your recursive algorithm should check the surrounding tiles of any given tile and run itsself on any that are equal and not already checked. Then your main nested loop can just skip any that are already checked and run your algorithm on any that arent.

Comment
Add comment · Show 2 · 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 Dudledok · Aug 17, 2014 at 08:41 AM 0
Share

You're missing the key point. If I check all neighbours, and there is an L shaped platform, how would you be able to deter$$anonymous$$e that shape and create a platform of that shape? You wouldn't simply be able to scale up a 1x1.

avatar image smoggach · Aug 18, 2014 at 03:34 AM 0
Share

I guess you'll have to build your map out of vertical and horizontal blocks. It doesn't make much sense to design your map with tiles if it won't be represented by tiles.

avatar image
0

Answer by robertbu · Aug 17, 2014 at 02:01 PM

Here is one algorithm. I sense is that it produces a good result, but not an optimal result, but in a quick 'napkin' sketch, I could not find a situation where it did not come up with an optimal result. I define optimal result as the fewest game objects.

  • Start with a nested for() loop that walks through each cell starting in the upper left corner.

  • If a cell has been marked as processed, go to the next cell

  • If a cell has not been process, it becomes an anchor cell

  • Check the cell below the anchor cell to see if it is a match, expand to 1 x 2 area if it is a match

  • If previous check succeeded, check the two cells on the right to see if you can expand it to a 2 x 2.

  • If the previous check failed, see if you can expaned to 2 x 1 area.

  • And so on

So basically you cycle back and forth between right and down. At each cycle, you see if you can expand your rectangle by testing the number that would grow the rectangle in either the down direction or the right direction. You stop when you cannot expand either right or down. The result becomes a single game object, and the cells that make that result are marked as processed.

Since you are walking the cells in a nested for() loop, you will know that everything to the left and above you will always be processed.

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

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Nested Array? Populating a menu using a loop. 0 Answers

Saving and applying an array of Vector3 velocities on an array of GameObjects 1 Answer

I dont know why im getting a null reference exception 1 Answer

What's the best way to find the smallest Vector3.Distance of an array of enemies? 2 Answers

Multiple Cars not working 1 Answer


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