获取双倍速度矢量以正确计算而无需四舍五入

Getting double velocity vectors to calculate correctly without rounding

我目前正在编写一个简单的游戏,其中有许多城镇有一定数量的士兵,每秒增加。一个玩家拥有的城镇可以攻击另一个玩家拥有的城镇,导致原来城镇的士兵数量分成两半,另一半去攻击另一个城镇。

问题是我将进攻的士兵表示为朝向另一个城镇的形状,并且我使用碰撞检测来确定 Attack 何时到达。因此,重要的是 Attack 用来到达城镇的矢量是正确的,这样它才能真正到达城镇。

每个城镇都有一个名为 attackPointPoint 字段,我将其用作攻击的起点以及他们前往的目的地。 attackPoint位于碰撞场的中间。

这是我的代码,它初始化 Attack 并创建运动向量:

public Attack(int troops, Team owner, Town source, Town destination) {
    this.troops = troops;             //troops included in the attack
    this.owner = owner;               //player that who sent the attack
    this.destination = destination;   //town the attack is going towards

    Point srcPoint = source.getAttackPoint(); //attackPoint of the source
    Point destPoint = destination.getAttackPoint(); //attackPoint of the destination town

    x = srcPoint.x;           //int --- the attack originates at the src's attack point
    y = srcPoint.y;           //int

    double hypotenuse = Math.sqrt(Math.pow(destPoint.y - y, 2) + Math.pow(destPoint.x - x, 2));
    double adjacent = destPoint.x - x;
    double opposite = destPoint.y - y;
    xVelocity = 3.0 * (adjacent / hypotenuse);     //field, double
    yVelocity = 3.0 * (opposite / hypotenuse);
}

然后另一种方法实际上移动了 Attack -

public void move() {
   x += xVelocity;
   y += yVelocity;
}

我可以通过测试确认 getAttackPoint() 返回的 attackPoints 都是正确的,并且我进行碰撞检测的方式工作正常。这里的问题是,似乎我的速度变量或其他一些变量正在四舍五入,因此 Attack 只能采用一些预定义的路径。例如:

//trying to attack X2     //trying to attack X1
X...............                     X------------------
.\..............                     .........X1.....
..\.............                     ................
...\............                     ................
....\...........                     ................
.....X1..X2.....                     ................

"predefined" 路径似乎每隔 30 度左右,我可以直接向上、向下、向左和向右发送 Attacks,这就是为什么我认为有些舍入误差是当我计算向量并进行三角函数时会继续。 当我有两个靠得很近的城镇时,无论我试图攻击哪一个,两个 Attack 都遵循相同的路径(图 1)。 还有,当我有一个接近源镇180度east/west的镇时,我发送的Attack会直接east/west(图2)

如果有人能发现问题或给我建议如何让物体从一个点或另一个点沿直接路径行进,将不胜感激!

虽然我确实查过你计算二维加速度的方法,但似乎很奇怪。

您需要由两点(源和目标)定义的直线的斜率。

double slope = (destination.y - source.y)/(destination.x - source.x);
xVelocity = 1; // do the normalisation as you wish
yVelocity = xVelocity * slope;

一般来说,我建议您使用向量而不是 x 和 y 变量。您可以概括大多数操作并使您的代码更具可读性和重复性(在这种情况下,连接源和目标的段将是目标 - 源给出的向量)。

因此,在对此进行更多研究并尝试了几种不同的解决方案之后,我非常确信存在一些舍入误差 - 也许将 Pointint 字段转换为 doubles,但我不确定。

我为让它工作所做的工作是将所有计算移至 move() 方法,以便在每次滴答时重新计算该行。这对优化不太有利,但我没有很多对象,这有助于 Attacks 沿着动态线路向其他城镇移动。最初我无法弄清楚是什么导致了问题,但确保计算能够通过重新计算进行自我检查解决了问题。