Code:

public class Block
{
    public Chunk chunk;
    public Dictionary<Vector3, Block> Neighbours = new Dictionary<Vector3, Block>();
    public bool IsTouchingGround = true;
    public Vector3 Position;
    //Code that i tried
    private bool GetNeighbourBlocks(Block b, int currI = 1)
    {
    BlockData xp = Map.Instance.GetBlock(b.Position.x + currI, b.Position.y, b.Position.z);
    BlockData xm = Map.Instance.GetBlock(b.Position.x - currI, b.Position.y, b.Position.z);
    BlockData yp = Map.Instance.GetBlock(b.Position.x, b.Position.y + currI, b.Position.z);
    BlockData ym = Map.Instance.GetBlock(b.Position.x, b.Position.y - currI, b.Position.z);
    BlockData zp = Map.Instance.GetBlock(b.Position.x, b.Position.y, b.Position.z + currI);
    BlockData zm = Map.Instance.GetBlock(b.Position.x, b.Position.y, b.Position.z - currI);
    bool b1 = !xp.Equals(default(BlockData)) && xp.block != null && xp.block.Type != 0 && xp.block.Health != 0;
    bool b2 = !xm.Equals(default(BlockData)) && xm.block != null && xm.block.Type != 0 && xm.block.Health != 0;
    bool b3 = !yp.Equals(default(BlockData)) && yp.block != null && yp.block.Type != 0 && yp.block.Health != 0;
    bool b4 = !ym.Equals(default(BlockData)) && ym.block != null && ym.block.Type != 0 && ym.block.Health != 0;
    bool b5 = !zp.Equals(default(BlockData)) && zp.block != null && zp.block.Type != 0 && zp.block.Health != 0;
    bool b6 = !zm.Equals(default(BlockData)) && zm.block != null && zm.block.Type != 0 && zm.block.Health != 0;
    if (b1 && xp.block.chunk != chunk || b2&& xm.block.chunk != chunk ||b3&& yp.block.chunk != chunk || b4&& ym.block.chunk != chunk || b5&&zp.block.chunk != chunk ||b6&& zm.block.chunk != chunk)
        return false;
    bool Any = false;
    if (!xp.Equals(default(BlockData)) && xp.block != null && xp.block.Type != 0 && xp.block.Health != 0 && !ConnectedBlocks.ContainsKey(xp.block.Position))
    {
        b.Neighbours.Add(xp.Position, xp.block);
        Any = true;
    }
    if (!xm.Equals(default(BlockData)) && xm.block != null && xm.block.Type != 0 && xm.block.Health != 0 && !ConnectedBlocks.ContainsKey(xm.block.Position))
    {
        b.Neighbours.Add(xm.Position, xm.block);
        Any = true;
    }
    if (!yp.Equals(default(BlockData)) && yp.block != null && yp.block.Type != 0 && yp.block.Health != 0 && !ConnectedBlocks.ContainsKey(yp.block.Position))
    {
        b.Neighbours.Add(yp.Position, yp.block);
        Any = true;
    }
    if (!ym.Equals(default(BlockData)) && ym.block != null && ym.block.Type != 0 && ym.block.Health != 0 && !ConnectedBlocks.ContainsKey(ym.block.Position))
    {
        b.Neighbours.Add(ym.Position, ym.block);
        Any = true;
    }
    if (!zp.Equals(default(BlockData)) && zp.block != null && zp.block.Type != 0 && zp.block.Health != 0 && !ConnectedBlocks.ContainsKey(zp.block.Position))
    {
        b.Neighbours.Add(zp.Position, zp.block);
        Any = true;
    }
    if (!zm.Equals(default(BlockData)) && zm.block != null && zm.block.Type != 0 && zm.block.Health != 0 && !ConnectedBlocks.ContainsKey(zm.block.Position))
    {
        b.Neighbours.Add(zm.Position, zm.block);
        Any = true;
    }
    return Any;
    }
    public void UpdateBlock()
    {
       //the neighbours is empty and i want:
       //get neighbours and check if any of them touches ground
       //if it touches then set the IsTouchingGround of every neighbour block to true, else false
       //heres what i tried:
       bool Any = GetNeighbourBlocks(this, 1);
       int i = 2;
       do
       {
         Any = GetNeighbourBlocks(this, i);
         i++;
       } while (Any);
    List<Block> lowest = new List<Block>();
    int min = Position.y;
    foreach (Block b in Neighbours.Values)
    {
        min = Mathf.Min(b.Position.y, min);
    }
    foreach (Block b in Neighbours.Values)
    {
        if (b.Position.y == min)
        {
            lowest.Add(b);
        }
    }
    bool o = lowest.All(a => Map.Instance.GetBlock(a.Position - new Vector3(0, 1, 0)).block == null || Map.Instance.GetBlock(a.Position - new Vector3(0, 1, 0)).Equals(default(BlockData)) || Map.Instance.GetBlock(a.Position - new Vector3(0, 1, 0)).block.Type == 0 || Map.Instance.GetBlock(a.Position - new Vector3(0, 1, 0)).block.Health <= 0);
    if (o)
    {
        foreach (Block b in Neighbours.Values)
        {
            b.IsTouchingGround = false;
        }
    }
    }

}

How can I get the blocks and save them into the Dictionary then check if the blocks are touching ground, while ground is at x: 0, y: 6, z: 0 and the blocks are making 3d triangle at x: 0, y: 8, z: 0 and one block at x: 5, y: 7, z: 5?

  • Presumably you've tried checking whether your dictionary contains an entry at (x ± 1, y, z) and similarly in each direction? Did you have trouble making that work the way you want? – DMGregory Dec 1 at 16:26
  • Yes, instead of getting all blocks from that triangle i either got only 6 or not all blocks. – PiotrKFtw Dec 1 at 16:32
  • Ah, are you not looking for only the 6 immediately adjacent neighbours, but the entire connected component (ie. all voxels touching the source voxel, or touching one touching the source voxel, and so on recursively)? – DMGregory Dec 1 at 16:54
  • Yes. I'm looking for entire connected component. – PiotrKFtw Dec 1 at 22:00
  • So you've tried using standard graph search algorithms like depth first search? – DMGregory Dec 1 at 22:39

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.