void SeparateCells() { bool cellCollision = true; int loop = 0; while (cellCollision) { loop++; cellCollision = false; if (debug) { // Debug.Log("Loop " + loop); } for (int i = 0; i < cells.Count; i++) { GeneratorCell c = cells[i]; for (int j = i + 1; j < cells.Count; j++) { GeneratorCell cb = cells[j]; if (c.CollidesWith(cb)) { cellCollision = true; int cb_x = Mathf.RoundToInt((c.x + c.width) - cb.x); int c_x = Mathf.RoundToInt((cb.x + cb.width) - c.x); int cb_y = Mathf.RoundToInt((c.y + c.height) - cb.y); int c_y = Mathf.RoundToInt((cb.y + cb.height) - c.y); if (c_x < cb_x) { if (c_x < c_y) { c.Shift(c_x, 0); } else { c.Shift(0, c_y); } } else { if (cb_x < cb_y) { cb.Shift(cb_x, 0); } else { cb.Shift(0, cb_y); } } } } } } }
void SeparateCells() { bool cellCollision = true; int loop = 0; while (cellCollision) { loop++; cellCollision = false; for (int i = 0; i < cells.Count; i++) { GeneratorCell cell = cells[i]; ///Check all the other cells in the list if it's colliding with our cell for (int j = i + 1; j < cells.Count; j++) { GeneratorCell cell2 = cells[j]; if (cell.CollidesWith(cell2)) { cellCollision = true; int cell2XOverlap = Mathf.RoundToInt((cell.posX + cell.width) - cell2.posX); int cellXOverlap = Mathf.RoundToInt((cell2.posX + cell2.width) - cell.posX); int cell2YOverlap = Mathf.RoundToInt((cell.posY + cell.height) - cell2.posY); int cellYOverlap = Mathf.RoundToInt((cell2.posY + cell2.height) - cell.posY); //If the cells are colliding. Move the one on the left. if (cellXOverlap < cell2XOverlap) { //Move horizontally if distance from the origin has more bias towards Y. //Otherwise, Move Vertically if (cellXOverlap < cellYOverlap) { cell.OffsetPosition(cellXOverlap, 0); } else { cell.OffsetPosition(0, cellYOverlap); } } else { if (cell2XOverlap < cell2YOverlap) { cell2.OffsetPosition(cell2XOverlap, 0); } else { cell2.OffsetPosition(0, cell2YOverlap); } } } } } } }