突破游戏-碰撞检测 我在C++中为DirectXTK 8编写了一个突破性的游戏,我在一个向量里面的球和砖块之间有碰撞检测。Ball class负责与桨叶和砖块的碰撞检测。它有一个更新方法,该方法在其声明中引用了桨对象和砖对象。现在,与桨的碰撞工作正常,因为桨在主游戏类中声明,但我不知道如何引用存储在向量中的对象。顺便说一下,如果我从Ball::Update方法声明中删除“Brick&Brick”,则会按预期绘制5行砖

突破游戏-碰撞检测 我在C++中为DirectXTK 8编写了一个突破性的游戏,我在一个向量里面的球和砖块之间有碰撞检测。Ball class负责与桨叶和砖块的碰撞检测。它有一个更新方法,该方法在其声明中引用了桨对象和砖对象。现在,与桨的碰撞工作正常,因为桨在主游戏类中声明,但我不知道如何引用存储在向量中的对象。顺便说一下,如果我从Ball::Update方法声明中删除“Brick&Brick”,则会按预期绘制5行砖,c++,windows-8,C++,Windows 8,球更新类 void Ball::Update(float timeDelta, Rect windowBounds, Paddle& paddle, Brick& brick) { //Respond to collision with the Paddle if ( paddle.IntersectsWith(this->boundingBox)) { this->velocity.y = this->velocity.y * -1; } // R

球更新类

void Ball::Update(float timeDelta, Rect windowBounds, Paddle& paddle, Brick& brick)
{

//Respond to collision with the Paddle
if ( paddle.IntersectsWith(this->boundingBox))
{
    this->velocity.y = this->velocity.y * -1;
}

// Respond to collision with the brick
if (brick.IntersectsWith(this->boundingBox))
{
    this->velocity.y = this->velocity.y * -1;
    brick.Hit();
    this->score+= 10;
}
}
这是如何在游戏类中初始化Brick对象,bricksRow是向量

//Next, create this many Brick objects and store them in a std::vector
for (int j = 0; j < bricksNumberOfRows; j+=32)
{
    for ( int i = 0; i < numberOfBricks ; i++)
    {
        //Add a Brick object to the back of the vector
        this->bricksRow.push_back(Brick());
        //Get a local reference to this newly added Brick object
        Brick& brick = this->bricksRow.back();
        //Call the initialise method on the newly added Brick object
        brick.Initialize(L"Assets/Brick.png"
            , this->d3dDevice
            , this->d3dDeviceContext
            , Vector2((float)(i * this->brickWidth), bricksStartingPosition + j));
    }
}
星星在哪里,我应该指的是一块砖块&向量中的物体,但我不知道怎么做


感谢您提前回复。

您可能希望检测桨与向量中每个砖块对象的碰撞,因此可以像这样枚举和调用Update():

vector<Brick>::iterator ci;
for(ci = bricksRow.begin(); ci! = bricksRow.end(); ci++)
{
    this->ball.Update(timeDelta, this->windowBounds, this->paddle, *ci);
}
更新:迭代器应该是迭代器而不是常量迭代器,因此不是

vector<Brick>::const_iterator ci;
向量::常量迭代器ci; 应该是

vector<Brick>::iterator ci;
vector::迭代器ci;
更好的是,由于您没有在void Ball::Update(float timeDelta,Rect windowBounds,palle&palle,brick&brick)中修改对象brick,如果您可以将其更改为 无效球::更新(浮动时间增量、矩形窗口边界、划桨和划桨、常量砖和砖),然后您可以保留

vector<Brick>::const_iterator ci;
向量::常量迭代器ci;
对这种差异的解释可能需要一个单独的问题。上述内容可能会帮助您解决当前的问题。

您需要的是对存储砖块的向量的引用:
void Ball::Update(…,(std::vector)&Bricks)
这样我就不能从Ball类中的Brick类调用方法。我想检测球与向量中每个Brick对象的碰撞,我想这就是你在回答中的意思。无论如何,我在这一行中得到一个错误,其中*ci是this->ball.Update(timeDelta,this->windowBounds,this->patle,*ci);它表示错误:在类型为“Brick&”的绑定引用中丢弃了限定符,以使用迭代器ball初始化类型为“const Brick”。Update被调用了大量次,ball每秒更新多次。我相信每次迭代器遍历向量时它都会更新,而向量一直都是。谢谢你抽出时间,凯文。
vector<Brick>::iterator ci;
vector<Brick>::const_iterator ci;