问题是,当 Actor/实体向点击位置移动时,由于距离大约 1 格,角色开始晃动,因为它向上移动 7 个像素,然后向下移动 7 个像素,一遍又一遍地没有到达目的地。
我尝试了几种不同的方法,但似乎都以相同的方式结束,有可能它只需要上升几个像素,但由于速度为 7,它移动得太远,然后又向后移动。
if (Objects.nonNull(next)) {
if (this.getDistanceVector(next) <= 64) {
return;
}
double destX = next.y*64 - this.getX();
double destY = next.x*64 - this.getY();
double dist = Math.sqrt(destX * destX + destY * destY);
destX = destX / dist;
destY = destY / dist;
double travelX = (destX * speed);
double travelY = (destY * speed);
this.moveBy((float) travelX, (float)travelY);
} else {
moveTo = null;
}
它应该走到该位置并停止,它当前走得很近,然后根据单击的位置在 x 轴或 y 轴上摇动。
最佳答案
通过检查速度是否大于距离,我能够实现平滑过渡,没有任何抖动。
if (speed > dist) {
destX = target.y - this.getX();
destY = target.x - this.getY();
destX = destX / dist;
destY = destY / dist;
double travelX = (destX * speed);
double travelY = (destY * speed);
this.setX((int)travelX);
this.setY((int)travelY);
} else {
destX = destX / dist;
destY = destY / dist;
double travelX = (destX * speed);
double travelY = (destY * speed);
this.moveBy((float) travelX, (float)travelY);
}
关于java - Actor 在靠近目的地时晃动 Vector2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55345469/