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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Splitting 2D Rocks made out of squares

Discussion in 'Scripting' started by mudkip989, Feb 23, 2020.

  1. mudkip989

    mudkip989

    Joined:
    Feb 23, 2020
    Posts:
    5
    So i want to split a 2D rock made out of squares by groups. If a hole is drilled through, i want the rock to become 2. diagonals would still count as attached, but i want the gaps define separate rocks. Would anyone know how to do that? There the object these squares are in is called an asteroid and i would like to make them break apart. I have squares of an asteroid in a list as well.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Most games handle this by pre-making rocks of sub-chunk sizes and just "faking" it. For 99.99% of games, this is good enough.

    However, if you want to generate your own split-up geometry, check out tutorials on destructible objects. There's a ton of people who have tinkered with this and I imagine there's some good open source examples and tutorials out there.
     
  3. mudkip989

    mudkip989

    Joined:
    Feb 23, 2020
    Posts:
    5
    I understand your point. However, I have a ton of Squares that are under the asteroid object. I do not want to have it shatter in a random way. I wanted it to separate when there are pieces not actually touching the others.Assuming that the Squares are in a list, i wanted the squares to stay with their respective chucks and not other chunks. Also, is there a Unity discord server? I would love to join it.
     
  4. mudkip989

    mudkip989

    Joined:
    Feb 23, 2020
    Posts:
    5
    I am still waiting on possible help here. just so people can notice.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    -->

    How did those tutorials go?
     
  6. mudkip989

    mudkip989

    Joined:
    Feb 23, 2020
    Posts:
    5
  7. mudkip989

    mudkip989

    Joined:
    Feb 23, 2020
    Posts:
    5
    So I'm technically pinging this post to the top again, but here's the update. I put all the squares into a 2D Array, but now i need to set up something that will check, using the array, for groups of squares. The Squares will checked by adjacent and diagonal and will never check the same one twice. It will put these Squares in a new rock or asteroid object. At the same time it removes those squares from the first asteroid. I would like a selection algorithm that would find thesegroups of squares and put them in an array that i could use.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Ya gotta help us with the visualizations here. I'm gonna jump in and offer you some inspiration in the form of a quick PNG I made... try to draw something that illustrates what you're actually doing. Feel free to use my picture if it's even close to what you're doing.

    a.png
    Now... what are you doing, drilling through it? Instantly? Over time? Walk us through it or we really can't give you anything more than generalized "go look at destructible objects" tutorials, because we can't imagine your setup.
     
  9. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Check out a simple flood fill algorithm. I've created/used this kind of algorithm a few times now. I have a Shape class I use in multiple projects that has a "GetIslands" method that does exactly what I'm guessing you're trying to do. I don't have access to it now, so a brief outline of the algorithm is all you get:

    Create a candidate list of all of the positions. Create an empty array of positions. Select a random candidate and keep checking its neighbors and adding them to the position list and removing them from the candidate list until there are no more neighbors. Pick the next candidate, save the filled list, replace it with a new empty one and repeat until the candidate list is empty.

    EDIT: Here's pseudocode of the algorithm I'm using:

    Code (CSharp):
    1. let candidates = new Set<Position>( givenPositions )
    2. let frontier = new Stack<Position>
    3.  
    4. let islands = new List<Shape>
    5.  
    6. while candidates is not empty:
    7.     let island = new Shape
    8.  
    9.     islands.add( island )
    10.  
    11.     next = candidates.first()
    12.  
    13.     frontier.push( next )
    14.  
    15.     while frontier is not empty:
    16.         let current = frontier.pop()
    17.  
    18.         for each neighbor in getNeighboringPositionsInCandidates( current, candidates ):
    19.             candidates.remove( neighbor )
    20.             frontier.push( neighbor )
    21.      
    22.         island.add( current )
    23.         candidates.remove( current )
    24.  
    25. return islands
     
    Last edited: Feb 28, 2020