c++ - vector 碰撞

标签 c++ vector project

我对 vector 很陌生,这是我第一次真正使用它们进行碰撞检查。这是我的项目,我对如何实现碰撞感到困惑。我目前的碰撞检查和响应代码似乎是……糟糕的设计。

这是我的代码:

for(auto it = ArrayofEntities.begin(); it != ArrayofEntities.end(); it++)
    {
        CEntity * go = (*it);

        for(auto i = ArrayofEntities.begin(); i != ArrayofEntities.end();)
        {
            //Collision for entities. Collision Event returns the iterator after an element is erased.
            CEntity * other = (*i);
            if (go != other)
            {
                if (!theCollision.CheckCollision(go, other, false, false, false, false)) //Checks if it has collided go with other
                {
                    i = go->CollisionEvent(*other, ArrayofEntities);    //Run collision code, setting i to the iterator which is returned.

                    //break;
                }
                else
                {
                    i++;
                }
            }
            else
            {
                i++;
            }
        }
}

CEntity 是所有实体的基类。

我的 CheckCollision 仅在碰撞时返回 true 或 false,而我的碰撞事件运行碰撞并返回一个迭代器(因为我可能不得不破坏 vector 中的东西)。

我的碰撞事件如下

vector<CEntity*>::iterator  bullet::CollisionEvent(CEntity &other, vector<CEntity*> & theArray)
{
case ZOMBIE:
    {
        other.hp -= power * 0.01;//Effect


        int Counter, index, bulletindex;
        auto it = theArray.begin();
        //Find the bullet and the other in the array.
        for (it = theArray.begin(), Counter = 0; it != theArray.end();it++, Counter++)
        {
            CEntity *go = NULL;
            go = (*it);
            if (go == &other)
            {
                index = Counter;
            }
            if(go->ID == BULLET && go->GetX() == GetX() && go->GetY() == GetY())
            {
                bulletindex = Counter;
            }
        }

        this->~bullet();//Delete the bullet
        theArray.erase(theArray.begin() + bulletindex);


        if(other.hp <= 0)
        {
            other.~CEntity();
            it = theArray.erase(theArray.begin() + index); //delete from array.


            return it;
        }

        it = theArray.begin() + index;
        return it;

    }

我基本上是像做数组一样做这件事的。只需检查它自己。在碰撞事件运行后的第一个 for 循环中,它给出的错误是“Vector Iterator not Incrementable”。

所以我的问题是:1) 我做错了什么? 2) 我的想法是像检查数组错误吗?

这是我的学校项目,所以我可以完全控制代码。

我宁愿快速修复而不是完全重写所有碰撞代码,但如果真的要解决这个问题,我会重写我的代码。

最佳答案

如果您查看 std::remove_if 的实现,你会看到他们用另一种方式解决了迭代器失效的问题。他们不是删除元素,而是将它们移动到数组的末尾。

这对您来说可能也是最简单的解决方案。保留一个指向最后一个“事件”实体之后的迭代器。它开始于 .end但是当子弹击中东西时,您将实体交换到范围的后面并递减最后一个事件的迭代器。

然后,当您完成对数组的循环后,您可以通过一次调用 .erase 进行清理。 .

是的,您应该使用 std::unique_ptr<CEntity>std::shared_ptr<CEntity>在集合中。这样,.erase不仅会删除指针,还会删除指向的对象。

关于c++ - vector 碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25563901/

相关文章:

c++ - 使用两个不同的管道c++以相同的进程读取和写入

c++ - 如何将 map 中的 copy_if 复制到 vector ?

c++ - 使用窗口 native API 创建 GUI

c++ - 当我洗牌时,指向 vector 中元素的指针会发生什么?

java - 文件在 eclipse luna 中不可见,我该如何获取?

引用变量的 C++ 初始化列表

python - 在 Python 中创建一组向量的笛卡尔积?

c++ - 将 OpenCv Mat 插入到 C++ std::vector

string - MSTest 项目无法获取本地化字符串?

java - 如何让用户输入字符串,然后对数组进行排序以根据输入的代码显示字符串