このプログラムでは、立方体で囲まれた 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++;
}