Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Weird Marching Square Problem.

Discussion in 'Scripting' started by Guy_Yome, Jun 8, 2016.

  1. Guy_Yome

    Guy_Yome

    Joined:
    Sep 30, 2014
    Posts:
    13
    So I have this problem with doing a "Kind of" marching square for some objects I place on the floor of my game. The map is based on a 1 by 1 grid on x and z axis. As the first picture shows ( poorly made on paint ), there is a grid.

    walls.png

    The blue squares are squares that aren't used by any object, hence they are not a key in the dictionary of the mapping ( due to the fact that the map is virtually infinite ). Due to this, they can't get referenced and have to be created with a Vector2 key. The black squares are the objects I'm talking about. The thing is that I want to connect them in a seemless way that seems fine like with the marching square. But since the player can add or remove any object at any time, I don't want to go throught the 3D meshing of the official marching square everytime the player clicks on the ground ( which would require too much calculations for nothing ).

    Instead, I'm using 2D textures on planes to represent them right now. It's easy to do and check the 4 neighbours on the cross to have a good visual, but the problem comes on the 4 diagonals. As the sencond picture shows ( at the end of the text ), some visual may go out of the boundary of an object just to correctly show it. Also, it would require adding objects with visual over the blue inactive squares. Last thing is that with 8 neighbours, there are 256 possiblities of neighbouring every tile. What do I do? I've been searching for a week now.

    walls_connected.png
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Well, if you conceptually subdivide each square into 4 smaller squares, then the problem gets much easier. Each subsquare will have a line at its top or right edge, or one of the two diagonals (or have no line at all). I think you can code a bit of logic to figure out which case applies.

    On the other hand, marching squares is pretty quick, and drawing lines is easy & efficient with something like Vectrosity. So, you could just do it that way.
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
  4. Guy_Yome

    Guy_Yome

    Joined:
    Sep 30, 2014
    Posts:
    13
    Don't you think that having four planes for every square would be too high demanding? If not, that's perfect.
     
    Last edited: Jun 8, 2016
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    You don't need four planes; you need four squares, which could be implemented in lots of different ways — sprites, quads, a single mesh for the whole lot of them, etc.

    So, yeah, you can definitely do it this way I think.