【适用于 Bukkit】
写了个轮子,用来获取跟随玩家视角的位置点
原理不讲了,就举个例子
获取距离玩家头部左侧1格的位置
写了个轮子,用来获取跟随玩家视角的位置点
代码:
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);