2d Game Sprite的位置在到达某一点时在X和Y轴上变为-2147483648

时间:2014-12-04 17:17:50

标签: c++ vector artificial-intelligence sprite

所以我从一个非常基本的AI开始,我将在稍后进行扩展,但这个问题让我很头疼。如果玩家在底部,则敌人应该到屏幕的顶部中间,如果玩家在顶部,则到底部中间。它应该留在那里,同时向玩家射击直到被摧毁。敌人以一种生涩的方式避免相互碰撞但这并不是真正相关的。这是代码:

for (n = 0; n < upgrades.size(); n++)
    {
        for (inner = 0; inner < upgrades.size(); inner++)       
        {
            if (n != inner && upgrades[n]->HitTest(upgrades[inner])) 
            {
                upgradeCollide = true;
                if (upgrades[n]->GetX() < upgrades[inner]->GetX())  upgrades[n]->SetPosition(upgrades[n]->GetX() - 10, upgrades[n]->GetY());
                else if (upgrades[n]->GetX() > upgrades[inner]->GetX()) upgrades[n]->SetPosition(upgrades[n]->GetX() + 10, upgrades[n]->GetY());
                else if (upgrades[n]->GetY() < upgrades[inner]->GetY()) upgrades[n]->SetPosition(upgrades[n]->GetX(), upgrades[n]->GetY() - 10);
                else if (upgrades[n]->GetY() > upgrades[inner]->GetY()) upgrades[n]->SetPosition(upgrades[n]->GetX(), upgrades[n]->GetY() + 10);
                upgradeCollide = false;
            }
        }
        upgradeVX = 400 - upgrades[n]->GetX();
        if (player.GetY() >= 300) upgradeVY = 150 -  upgrades[n]->GetY();
        else if (player.GetY() < 300)  upgradeVY = 450 -  upgrades[n]->GetY();
        //Above 3 lines is the enemy seeking the position
        upgradeLength = sqrt(upgradeVX * upgradeVX + upgradeVY * upgradeVY);
        upgradeVX = upgradeVX / upgradeLength * 100;
        upgradeVY = upgradeVY / upgradeLength * 100;
        //Above is normalization of the velocity vector (VX, VY)
        enemy_rotation = atan2(upgrades[n]->GetY() - player.GetY(), upgrades[n]->GetX() - player.GetX()) * (180/3.14159);
        upgrades[n]->SetRotation(enemy_rotation-90);
        if (!upgradeCollide) upgrades[n]->SetVelocity(upgradeVX, upgradeVY);
}

所以它应该根据玩家的位置寻找两个不同的点。奇怪的是,如果我不对速度矢量进行标准化,它的工作原理就好了,但当然必须将其标准化,否则会看起来很奇怪而且不公平。

当敌人到达目标并发生碰撞时,请记住,因为另一个敌人已经在那里,它只会向左或向右推动它并留出足够的空间(只有当我没有正常化时才这样做,否则他们会旅行百万像素 - 正常化)

所以有人知道为什么他们在击中两个目标中的一个时会出现疯狂的像素数量?我只是希望他们停在目标位置并射击球员。

0 个答案:

没有答案