我正在尝试为服务器编写一个插件,我想从 A 点到 B 点启动一个播放器。
我已经尝试过这个,但很多时候它不准确,并且会错过几个街区。
private Vector calculateVelocity(Location fromLoc, Location toLoc, int heightGain) {
Vector from = fromLoc.toVector();
Vector to = toLoc.toVector();
//Block locations
int endGain = to.getBlockY() - from.getBlockY();
double horizDist = fromLoc.distance(toLoc);
//Height gain
int gain = heightGain;
double maxGain = (gain > (endGain + gain) ? gain : (endGain + gain));
//Solve quadratic equation for velocity
double a = -horizDist * horizDist / (4 * maxGain);
double b = horizDist;
double c = -endGain;
double slope = -b / (2 * a) - Math.sqrt(b * b - 4 * a * c) / (2 * a);
//Vertical velocity
double veloY = Math.sqrt(maxGain * 0.115);
//Horizontal velocity
double vH = veloY / slope;
//Calculate horizontal direction
int distX = to.getBlockX() - from.getBlockX();
int distZ = to.getBlockZ() - from.getBlockZ();
double mag = Math.sqrt(distX * distX + distZ * distZ);
double dirX = distX / mag;
double dirZ = distZ / mag;
//Horizontal velocity components
double veloX = vH * dirX;
double veloZ = vH * dirZ;
//Actual velocity
Vector velocity = new Vector(veloX, veloY, veloZ);
return velocity;
}
关于我应该做什么才能使其 100% 准确,有什么建议吗?
您可以使用以下代码:
if (commandSender instanceof Player) {
Player player = (Player) commandSender;
Location from = player.getLocation();
Location to = new Location(player.getWorld(), player.getLocation().getX() + 4, player.getLocation().getY(), player.getLocation().getZ() + 4);
player.setVelocity(new Vector(from.getX() - to.getX(), from.getY() - to.getY(), from.getZ() - to.getZ()));
}
只需将
Location to
变量替换为您需要投掷玩家的坐标即可。