我正在制作一个Minecraft风格的游戏,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 :(得分:0)
double searchRadius = 0;
...
for(double i = 0; i<searchRadius; i+=0.01){
...
}
return Block.air;
尝试大于零的searchRadius
。