Java 设置玩家的速度,使其以一个弧线从A点发射到B点
我正在尝试为服务器编写一个插件,我想从a点到B点启动一个播放器 我已经试过了,但是很多时候它是不准确的,并且错过了几个街区Java 设置玩家的速度,使其以一个弧线从A点发射到B点,java,vector,bukkit,Java,Vector,Bukkit,我正在尝试为服务器编写一个插件,我想从a点到B点启动一个播放器 我已经试过了,但是很多时候它是不准确的,并且错过了几个街区 private Vector calculateVelocity(Location fromLoc, Location toLoc, int heightGain) { Vector from = fromLoc.toVector(); Vector to = toLoc.toVector(); //Block locations in
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%准确,有什么建议吗