Search Unity

Question Filling closed area in Unity 3D

Discussion in 'Scripting' started by hazarss, Oct 19, 2022.

  1. hazarss

    hazarss

    Joined:
    Aug 16, 2021
    Posts:
    12
    Hey everyone!

    I'm working on an old-school gaming idea, where the player needs to occupy some amount of the map to complete the level (video link provided at the end).

    The empty area in the video is filled with objects (meshrenderer is disabled) when the player touches the boxes in that area, meshrenderer gets activated. And when the player gets back to a safe spot (borders) those objects become the new border.

    My question is, how can I fill the inside of that closed shapes? As you can see in the video, the player can travel and occupy new areas, but I want those areas to be filled completely.

    Could you please help me with that?

    P.S: I'm kinda new to Unity game development, and I would really appreciate it if you can take this into consideration while sharing your answers :) (I read some articles about flood fill, but don't know if that's the solution I need and how I can implement it into my case)

    https://youtube.com/shorts/0MGCIu1b3vg?feature=share
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    The algorithm you seek is called "floodfill."

    I actually just made a 2D version of this game, which was originally Qix in the arcade, and later Xonix on the PC.



    You can find all kinds of psuedocode implementations, or see mine here:

    https://pastebin.com/uYLP4PQy

    It floodfills on each side of the line, then tries to see which side the enemies are on and fills the other side if possible.