2

このプログラムでは、立方体で囲まれた 3D 空間を球体が動き回ります。球体は、壁または別の球体にぶつかるまで移動します。壁にぶつかると破壊されます。それらが別の球体に当たると、小さい方の球体が破壊されます。

球体は、x、y、z 座標で半径 r と dx、dy、dz の動きで指定されます。

プログラムの一般的な要点は、球体オブジェクトのリストをループし、距離式を使用して、衝突を検出する半径よりも小さいかどうかを確認することです。

とにかく、実行時にVisual Studioで「リストイテレータはインクリメントできません。ただし、プログラムはLinux g ++コンパイラでエラーなしで実行されます。プログラムが動作するときに球の衝突を検出しようとすると、問題が発生するようです。セクションがコメントアウトされている場合。

// This is where our spheres are moved
s = sphereList.begin();                     //set iterator to beginning of list

while(s != sphereList.end())                //This loop moves the spheres
{
    bool firstRun = true;
    double x,y,z,r,xj,yj,zj,dist,radii,area1,area2;         //Temp x,y,z,r

    Point newCenter;        //Temp Center for movement calculatio n

    x = s->getX()+s->getdx();
    y = s->getY()+s->getdy();
    z = s->getZ()+s->getdz();
    r = s->getRadius();

    newCenter.setX(x);
    newCenter.setY(y);
    newCenter.setZ(z);

    s->setCenter(newCenter);    //replaces existing sphere with new coordinates

    //check if the sphere has reached the edge of the cube at 0 or 1000 units 
    if(x+r >= MAX_CUBE || x-r <= MIN_CUBE || y+r >= MAX_CUBE || y-r <= MIN_CUBE || z+r >= MAX_CUBE || z-r <= MIN_CUBE)
    {
        cout << "  " << s->getElement() << "         " << (int)time << "         " << "Boundary" << endl;
        s = sphereList.erase(s);
        time = 1 * TIME_VALUE;
    }

    std::list<Sphere>::iterator j = sphereList.begin();         //second iterator
        j++;
    while(j != sphereList.end())
    {
        xj = j->getX();
        yj = j->getY();
        zj = j->getZ();

        dist = sqrt(((x-xj)*(x-xj))+((y-yj)*(y-yj))+((z-zj)*(z-zj)));

        //cout << dist << endl;
        radii = s->getRadius() + j->getRadius();

        if(dist < radii)
        {

            if(s->getRadius() < j->getRadius())
            {
                cout << "  " << s->getElement() << "         " << (int)time << "         " << "Collision" << endl;
                s = sphereList.erase(s);
                time = 1 * TIME_VALUE;
                break;
            }
            else
            {
                cout << "  " << j->getElement() << "         " << (int)time << "         " << "Collision" << endl;
                j = sphereList.erase(j);
                time = 1 * TIME_VALUE;
                break;
            }
        }
    j++;
    }
    time++;


}
4

2 に答える 2

0

以下のループ内で、i数回インクリメントしていますが、本当にたくさんの要素がありますか?

while(i != doubleList.end())
{
于 2013-02-07T06:17:02.513 に答える
0

sとがリスト内の同じ位置にある場合j、 を消去jし、 を無効にしsます。

jの前にあるすべての要素と既に比較しているため、毎回最初から開始する必要はありませんs

交換すると思います

j = sphereList.begin();

j = s;

十分なはずです。

于 2013-02-07T08:52:54.760 に答える