好大一坨翔哟
本帖最后由 好大一坨翔哟 于 2023-1-26 22:08 编辑

解决方法:
List<Location> getLocationsAroundPlayer(Player player, double radius) {
    World world = player.getWorld();
    Location playerLocation = player.getLocation();
    double x = playerLocation.getX();
    double y = playerLocation.getY();
    double z = playerLocation.getZ();

    List<Location> locations = new ArrayList<Location>();
    for (double nowX = x - radius; nowX <= x + radius; nowX++) {
        for (double nowY = y - radius; nowY <= y + radius; nowY++) {
            for (double nowZ = z - radius; nowZ <= z + radius; nowZ++) {
                Location location = new Location(world, nowX, nowY, nowZ);
                double distance = playerLocation.distance(location);
                if (distance <= radius) {
                    locations.add(location);
                }
            }
        }
    }
    return locations;
}



William_Shi
“玩家周围的所有坐标”包含了无限个坐标,比如 (0, 0, 0.1) 、(0, 0, 0.2) 等等,没有办法“得到”

好大一坨翔哟
William_Shi 发表于 2023-1-26 21:13
“玩家周围的所有坐标”包含了无限个坐标,比如 (0, 0, 0.1) 、(0, 0, 0.2) 等等,没有办法“得到” ...

对的 所以我这个问题其实想要问的是如何获得玩家周围圆形所有的方块的中心点

凌语丶
r为定值吗 如果r为定值的话打个偏移量表(

BlessAzek
本帖最后由 BlessAzek 于 2023-1-26 21:34 编辑

List<Location> getLocationsAroundPlayer(Player player, double radius) {
    World world = player.getWorld();
    Location playerLocation = player.getLocation();
    double x = playerLocation.getX();
    double y = playerLocation.getY();
    double z = playerLocation.getZ();

    List<Location> locations = new ArrayList<Location>();
    for (double nowX = x - radius; nowX <= x + radius; nowX++) {
        for (double nowY = y - radius; nowY <= y + radius; nowY++) {
            for (double nowZ = z - radius; nowZ <= z + radius; nowZ++) {
                Location location = new Location(world, nowX, nowY, nowZ);
                double distance = playerLocation.distance(location);
                if (distance <= radius) {
                    locations.add(location);
                }
            }
        }
    }
    return locations;
}

还算是简单吧, 就是遍历的问题(
浅写了一下,
(代码未经验证,但应该没什么问题)
返回的是周围所有的location

注意,这里的是一个三维球体,如果不想要z轴的话直接去掉z轴这块遍历即可

好大一坨翔哟
BlessAzek 发表于 2023-1-26 21:32
List getLocationsAroundPlayer(Player player, double radius) {
    World world = player.getWorld();
  ...

好的我去试试

好大一坨翔哟
BlessAzek 发表于 2023-1-26 21:32
List getLocationsAroundPlayer(Player player, double radius) {
    World world = player.getWorld();
  ...

emmmm这是一个球形 有木有圆型的...

好大一坨翔哟
凌语丶 发表于 2023-1-26 21:25
r为定值吗 如果r为定值的话打个偏移量表(

emmm 并不是哦

好大一坨翔哟
BlessAzek 发表于 2023-1-26 21:32
List getLocationsAroundPlayer(Player player, double radius) {
    World world = player.getWorld();
  ...

大佬可以和我说下这段遍历是啥意思不

BlessAzek
本帖最后由 BlessAzek 于 2023-1-26 22:31 编辑
好大一坨翔哟 发表于 2023-1-26 22:13
大佬可以和我说下这段遍历是啥意思不

就是遍历啊(
这三个for循环分别遍历对应的坐标轴
最内层的遍历z轴
以当前位置的轴向上取 r,遍历到向下取 r
x,z轴同理

严格来说它的遍历形状其实是个柱体(再细分点就是正方体,因为长宽高都是半径r)
distance 定义的是当前的location距离中心坐标的距离,如果在规定半径内就添加到返回结果中

BlessAzek
好大一坨翔哟 发表于 2023-1-26 21:59
emmmm这是一个球形 有木有圆型的...

这个讲了,如果不想要z轴就去掉最内层的z轴遍历

直接用玩家当前位置的z轴就行

William_Shi

首先,上下方向的那根轴是y轴,玩家前后左右移动的时候,其实是在 xOz 平面上移动。要去掉的是 y 轴的遍历。其次,那个偏移量表也可以预先准备几个加快运行时的效率。

602723113
既然要圆形,为什么不问问神奇的三角函数呢,题主的解决办法实际上是一个长方体空间内生成而不是圆形这个要明确


  1. // 圆的原点
  2. Location origin;
  3. // 计算出结果的点列表
  4. List<Location> points = new ArrayList<>();
  5. // 边长
  6. double radius = 5;
  7. for(double i = 0; i < 360;i += 1) {
  8.         double rad = Math.toRadians(i);
  9.         double x = radius * Math.cos(rad);
  10.         double z = radius * Math.sin(rad);

  11.         points.add(origin.clone.add(x, 0, z));
  12. }

  13. // points 即为所求的的点
复制代码


请注意, 这里的代码没有涉及 Y 轴, 因此请自行生成时判断

这个算法将会获取圆的 360° 的每一个点, 若你不需要这么多的点, 那么你可以在 步进 时 i += 360/8 即可表示就取 8 个样本点