• 欢迎加入MineBBS QQ讨论群:点击查看所有的官方讨论群
  • 我们将于近期对服务器进行迁移,服务可能中断至多2日。请各位安排好自己的访问计划,造成不便敬请谅解!
  • MineBBS入站考试已经上线!想要成为【正式会员】解锁更多功能吗?快来参与吧!【点我去看】

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格的位置
 

在线会员

  • goodletmc
  • imfcby
  • QBQ_lonani
  • shenbi_micro
  • CHLovo
  • xiaoziyi
  • tTs12131312
  • buzizi
  • gattor
  • 不知名的秦某
  • Berg
  • H_aaa
  • ㄏㄏ
  • Eila
  • xiaoyang4090
  • K3
  • nightlicapoo
  • 一个笨笨的腐竹
  • loggk slan
  • ermuyixing555
...和 17 更多。
后退
顶部 底部