2017-05-24 46 views
1

我的問題是非常相似,這鄰居塊,但我不能完全做出來,因爲數學需要是有一點不同:https://gamedev.stackexchange.com/questions/65800/when-storing-voxels-in-chunks-how-do-i-access-them-at-the-world-level獲取存在於不同的塊

我試圖刪除塊的一些面孔。如果在當前塊之外,我無法解決如何獲取塊。

我有一個Chunk類來跟蹤自己的塊。爲了測試,我的塊是2×2,每塊可以容納4塊,都是實心的。

class Chunk { 

    private Blocks[,,] blocks; 

    public Vector3 position; 

    public Chunk(Vector3 world_pos){ 
     position = world_position; 

     // Loops for populating blocks array... 
     ... 

     // Create mesh 

     for(...){ 
      for(...){ 
       for(...){ 
        Block block = GetBlock(x, y, z + 1); // Chance of this block being outside of this chunk 
       } 
      } 
     }  
    } 

    public Block GetBlock(int x, int y, int z){ 
     ... 

     // If block is not in this chunk, use global GetBlock instead 
    } 

} 

我有稱爲「getBlock」塊試圖得到基於在x,y和z位置的塊內的方法。如果索引超出範圍,那麼我會調用一個全局「getBlock」,它試圖查找塊和塊內部的塊。這是我掙扎的地方。

這是我的「getChunk」方法(不是我的代碼,在教程中找到),這似乎很好。

public Chunk getChunk(int x, int y, int z){ 
    float s = CHUNK_SIZE; 
    float d = CHUNK_DEPTH; 

    int posx = Mathf.FloorToInt(x/s) * CHUNK_SIZE; 
    int posy = Mathf.FloorToInt(y/d) * CHUNK_DEPTH; 
    int posz = Mathf.FloorToInt(z/s) * CHUNK_SIZE; 

    Chunk chunk = null; 

    this.chunks.TryGetValue(new Vector3(posx, posy, posz), out chunk); 

    return chunk; 
} 

這是我的全球「getBlock」。

public Block getBlock(int x, int y, int z){ 
    Chunk chunk = this.getChunk(x, y, z); 
    Block block; 

    if(chunk == null){ 
     block = Block_Types.AIR; 
    } else { 

     // Need to work out the block position I want here 

     int posx = x; 
     int posy = y; 
     int posz = z; 

     block = chunk.getBlock(posx, posy, posz); 

    } 

    return block; 
} 

這裏是一個圖片,如果有幫助:

Picture

感謝

+0

這將真正受益於挖掘Minecraft的塊代碼。本質上你需要修改從世界座標到塊座標的xyz值。 – Draco18s

回答

0

除以CHUNK_SIZE全球位置和取餘數作爲塊的局部位置。 您可以使用模(%)運算符:

x = posx % CHUNK_SIZE; 
y = posy % CHUNK_SIZE; 
z = posz % CHUNK_SIZE; 

例如,

0/2 = 0; r = 0; 
1/2 = 0; r = 1; 
2/2 = 1; r = 0; 
3/2 = 1; r = 1; 
4 = 2 = 2; r = 0; 
5 = 2 = 2; r = 1; 
+0

我認爲我的問題是獲得世界街區的位置。如果我得到塊(0,0,0 - 1),並且塊存在,我該如何將其轉換爲(0,0,3)。 – JacketPotatoeFan

+0

@JacketPotatoeFan我在這裏沒有看到問題。當你將世界座標'(x,y,z)'傳遞給'getBlock'函數時,你會發現相應的塊。在這一點上,你要麼有包含你的塊的塊,要麼塊是空的 - 暗示你超出了界限。然後,你在我的答案中做了我所說的將世界座標轉換爲本地座標的答案。現在您直接引用了您感興趣的塊。 – Iggy

+0

我的塊座標不是世界座標。我需要轉換爲世界座標,然後轉換爲我正在檢查的塊的本地座標。我基本上試圖去掉4塊裏面的大塊頭http://i.imgur.com/cbwJiRi.png – JacketPotatoeFan