我目前正在用Java开发一个在线游戏的扩展。我目前正在处理隐形传送,以了解玩家是否传送了距离distance参数可以达到的特定距离。
问题是,我知道这在CPU上是一个超级密集的任务,即使我确实试图从主线程上卸载它。
有没有更有效的方法来处理这件事,或者我基本上必须重新组织整个事情?
this.square就是Math.pow();,this.sqrt就是Math.sqrt();,this.hypot如下:
default double hypot(double... numbers) {
double squaredSum = 0.0;
int length = numbers.length;
int count = 0;
while (count < length) {
double number = numbers[count];
squaredSum += Math.pow(number, 2.0);
++count;
}
return this.sqrt(squaredSum);
}这就是我检查玩家是否传送的方法
public boolean anyNear(CustomLocation location, double distance) {
if (!this.locations.isEmpty()) {
Iterator<TeleportSnapshot> iterator = this.locations.iterator();
if (iterator.hasNext()) {
TeleportSnapshot teleport = iterator.next();
double deltaX = location.getX() - teleport.x;
double deltaY = location.getY() - teleport.y;
double deltaZ = location.getZ() - teleport.z;
while (!(this.hypot(deltaX, deltaY, deltaZ) < this.sqrt(this.square(distance)))) {
if (iterator.hasNext()) {
teleport = iterator.next();
deltaX = location.getX() - teleport.x;
deltaY = location.getY() - teleport.y;
deltaZ = location.getZ() - teleport.z;
} else {
return false;
}
}
return true;
} else {
return false;
}
} else {
return false;
}
}发布于 2020-02-04 02:28:35
我同意大卫·齐默尔曼的观点,你可能不应该在计算中使用平方根。
而不是做
sqrt(dx^2 + dy^2 + dz^2) < sqrt(distance^2),您可以使用:
dx^2 + dy^2 + dz^2 < distance^2这对cpu更友好。不过,用于抵押权的辅助函数的名称将不再适合...
https://stackoverflow.com/questions/59993370
复制相似问题