• 周年纪念勋章活动已圆满结束,如有已购买但仍未申请的用户,可以通过对应勋章的下载链接申请~

Java 数 学 必 修 二

【适用于 Bukkit】

写了个轮子,用来获取跟随玩家视角的位置点
代码:
    public static Location rotateLocation(Location loc, double length, double angle) {
        return rotateLocation(loc, 0.0D, 0.0D, 0.0D, length, angle);
    }
    public static Location rotateLocation(Location loc, double xOffset, double yOffset, double zOffset, double length, double angle) {
        World world = loc.getWorld();
        double x = loc.getX() + xOffset;
        double y = loc.getY() + yOffset;
        double z = loc.getZ() + zOffset;
        float pitch = loc.getPitch();
        float yaw = loc.getYaw();
        System.out.println(loc.getYaw());
        double a = angle - (double) loc.getYaw();
        while(a < 0) {
            a += 360.0D;
        }
        System.out.println(a);
        Double newAngle = (a) % 360.0D;
        double radians = Math.toRadians(newAngle % 90);
        // 特殊角
        if(newAngle.equals(0.0D)) return new Location(world, x, y, z + length, yaw, pitch);
        if(newAngle.equals(90.0D)) return new Location(world, x + length, y, z, yaw, pitch);
        if(newAngle.equals(180.0D)) return new Location(world, x, y, z - length, yaw, pitch);
        if(newAngle.equals(270.0D)) return new Location(world, x - length, y, z, yaw, pitch);
        double sinL = length * Math.sin(radians);
        double cosL = length * Math.cos(radians);
        if(newAngle > 0.0D && newAngle < 90.0D) return new Location(world, x + sinL, y, z + cosL, yaw, pitch);
        if(newAngle > 90.0D && newAngle < 180.0D) return new Location(world, x + cosL, y, z - sinL, yaw, pitch);
        if(newAngle > 180.0D && newAngle < 270.0D) return new Location(world, x - sinL, y, z - cosL, yaw, pitch);
        if(newAngle > 270.0D && newAngle < 360.0D) return new Location(world, x - cosL, y, z + sinL, yaw, pitch);
        return loc;
    }
原理不讲了,就举个例子
代码:
rotateLocation(player.getLocation(), 0.0D, 1.5D, 0.0.D, 1.0D, 90.0D);
获取距离玩家头部左侧1格的位置
 

在线会员

  • eternal36900
  • 怡儿
  • 北纬一星
  • xinge54088
  • Maplef_snow
  • gdjd1412
  • 2801326928
  • ewsk
  • 黄泉
  • 芜湖胡子
  • 王聪聪
  • Sh1roCu
  • 思羽
  • jun1mo1xiao
  • waiwai_awa
  • pwnbb
  • 问就是玩不懂
  • miyamiya
  • 莫道云行
  • winter0203
  • polang
...和 36 更多。
后退
顶部 底部