Circular collision rebound not working properly
<p>I'm writing a little physics simulation in C++ that basically moves circles across the screen and when two circles collide, they should ricochet in the same manner as billiard balls would. When the circles do collide with each other, most of the time they will practically slow down infinitely/they appear to stick to each other and become static. Sometimes only one ball will rebound in the collision and the other will retain it's trajectory. This is just a simple 2D simulation.</p>

<p>So here's what I have for the detection/ricochet logic:</p>

<pre><code>bool Ball::BallCollision(Ball &amp;b2)
{
    if (sqrt(pow(b2.x - x, 2) + pow(b2.y - y, 2)) &lt;= b2.radius + radius) // Test for collision
    {
        normal[0] = (x - (x + b2.x) / 2) / radius; // Finds normal vector from point of collision to radius
        normal[1] = (y - (y + b2.y) / 2) / radius;
        xvel = xvel - 2 * (xvel * normal[0]) * normal[0]; // Sets the velocity vector to the reflection vector
        yvel = yvel - 2 * (yvel * normal[1]) * normal[1];

        ////x = xprev; // These just move the circle back a 'frame' so the collision
        ////y = yprev; // detection doesn't detect collision more than once.
                   // Not sure if working?
     }
}
</code></pre>

<p>I can't figure out what is wrong with my function. Thanks for any help in advance!</p>

<p><strong>Edit</strong>:
Every variable is declared as a float</p>

<p>The functions:</p>

<pre><code>void Ball::Move()
{
    xprev = x;
    yprev = y;
    x += xvel;
    y += yvel;
}

void Ball::DrawCircle()
{
    glColor3ub(100, 230, 150);
    glBegin(GL_POLYGON);
    for (int i = 0; i &lt; 10; i++)
    {
        angle = i * (2*3.1415/10);
        newx = x + r*cos(angle);
        newy = y + r*sin(angle);
        glVertex2f(newx, newy);
    }
    glEnd();
}
</code></pre>

<p>The loop:</p>

<pre><code>    run_prev.clear(); // A vector, cleared every loop, that holds the Ball objects that collided

    for (int i = 0; i &lt; num_balls; i++)
    {
        b[i].Move();
    }

    for (int i = 0; i &lt; num_balls; i++)
    {
        b[i].WallCollision(); // Just wall collision detecting, that is working just fine
    }

    //The loop that checks for collisions... Am I executing this properly?
    for (int i = 0; i &lt; num_balls; i++)
    {
        for (int j = 0; j &lt; num_balls; j++)
        {
            if (i == j) continue;
            if (b[i].BallCollision(b[j]) == true)
            {
                run_prev.push_back(b[i]);
            }
        }
    }

    for (int i = 0; i &lt; num_balls; i++)
    {
        b[i].DrawCircle();
    }

    //xprev and yprev are the x and y values of the frame before for each circle
    for (int i = 0; i &lt; run_prev.size(); i++)
    {
        run_prev[i].x = run_prev[i].xprev;
        run_prev[i].y = run_prev[i].yprev;
    }
</code></pre>
