Error with Program to detect colliding spheres
<p>In this program spheres will move around a 3d space that is enclosed by a cube. The spheres will move until they hit a wall or another sphere. If they hit the wall they are destroyed. If they hit another sphere, the smaller sphere will be destroyed.</p>

<p>The sphere's are given in x,y,z coordinates with a radius r and a dx, dy, dz for the movement.</p>

<p>The general gist of the program is to loop through a list of sphere objects and use the distance formula to see if it is less than the radius to detect a collision.</p>

<p>Anyways, I keep getting an error in Visual Studio during runtime saying "list iterator is not incrementable. However, the program does run without errors on the Linux g++ compiler. It looks like the problem occurs when attempting to detect sphere collisions as the program works when the section is commented out.</p>

<pre><code>// 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-&gt;getX()+s-&gt;getdx();
    y = s-&gt;getY()+s-&gt;getdy();
    z = s-&gt;getZ()+s-&gt;getdz();
    r = s-&gt;getRadius();

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

    s-&gt;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 &gt;= MAX_CUBE || x-r &lt;= MIN_CUBE || y+r &gt;= MAX_CUBE || y-r &lt;= MIN_CUBE || z+r &gt;= MAX_CUBE || z-r &lt;= MIN_CUBE)
    {
        cout &lt;&lt; "  " &lt;&lt; s-&gt;getElement() &lt;&lt; "         " &lt;&lt; (int)time &lt;&lt; "         " &lt;&lt; "Boundary" &lt;&lt; endl;
        s = sphereList.erase(s);
        time = 1 * TIME_VALUE;
    }

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

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

        //cout &lt;&lt; dist &lt;&lt; endl;
        radii = s-&gt;getRadius() + j-&gt;getRadius();

        if(dist &lt; radii)
        {

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


}
</code></pre>
