2013-02-25 87 views
0

我正在製作一個Minecraft風格的遊戲,其中三維數組塊分成塊。但是接下來我需要檢查一下哪個區塊選手指向執行世界修改。這是我想出迄今:LWJGL的3D採摘

private Block getBlockLookedAt(EntityPlayer ep) { 


     double px = ep.getPosition().x; 
     double py = ep.getPosition().y; 
     double pz = ep.getPosition().z; 

     float pPitch = ep.getPitch()*-1; 
     float pYaw = ep.getYaw()*-1; 

     double searchRadius = 6; 

     double targetX = searchRadius * Math.cos(Math.toRadians(pYaw))*Math.sin(Math.toRadians(pPitch)); 
     double targetY = searchRadius * Math.sin(Math.toRadians(pYaw))*Math.sin(Math.toRadians(pPitch)); 
     double targetZ = searchRadius * Math.cos(Math.toRadians(pPitch)); 
     //System.out.println("="); 
     //System.out.println(px + "," + py + "," + pz); 
     //System.out.println(targetX + "," + targetY + "," + targetZ); 
     //System.out.println("="); 
     for(double i = 0; i<searchRadius; i+=0.01){ 
      targetX = i * Math.cos(Math.toRadians(pYaw))*Math.sin(Math.toRadians(pPitch)); 
      targetY = i * Math.sin(Math.toRadians(pYaw))*Math.sin(Math.toRadians(pPitch)); 
      targetZ = i * Math.cos(Math.toRadians(pPitch)); 
      int bx = (int)Math.ceil(targetX+px); 
      int by = (int)Math.ceil((targetY*-1)+py); 
      int bz = (int)Math.ceil(targetZ+pz); 
      System.out.println(bx + "," + by + "," + bz + "," + getBlock(bx,by,bz).getID()); 
      if(getBlock(bx,by,bz).getID()!=0)return getBlock(bx,by,bz); 
     } 



     return Block.air; 
    } 

我在做什麼在這裏,用球面座標,並加入0.01〜檢查半徑,以檢查是否有塊比範圍空氣不同。雖然這似乎不起作用(它總是返回空氣,我需要在塊內檢測其他塊) 任何人都有解決方案,或者更好的方法來做到這一點?

PS。我讀過schabby的教程,並且據我瞭解,它不檢查Z軸,所以這不是我正在尋找的。

回答

0
double searchRadius = 0; 
... 
for(double i = 0; i<searchRadius; i+=0.01){ 
    ... 
} 
return Block.air; 

嘗試一個大於零的searchRadius

+0

我做了searchRadius 6,但它仍然只輸出0。 – TheMorfeus 2013-02-25 19:51:46