Recap
Solution
MinDistance = ball1->radius + ball2->radius;
dxx = ball1->xPos – ball2->xPos;
dyy = ball1->yPos – ball2->yPos;
//calculate new distance between marbles
distance = sqrt(dxx*dxx + dyy*dyy);
printf("COLLISION: New Distance:%d \r\n", (int)distance);
//this part makes sure that the new positions of the marbles are not overlapping
//sometimes the new position itself overlaps and that leads to weird behaviour
//check if the marbles are overlapping
if ((int)distance <= MinDistance)
{
//adjust their positions
do
{
if (ball1->xPos > ball2->xPos)
{
ball1->xPos += 1;
ball2->xPos -= 1;
}
else
{
ball1->xPos -= 1;
ball2->xPos += 1;
}
if (ball1->yPos > ball2->yPos)
{
ball1->yPos += 1;
ball2->yPos -= 1;
}
else
{
ball1->yPos -= 1;
ball2->yPos += 1;
}
dxx = ball1->xPos – ball2->xPos;
dyy = ball1->yPos – ball2->yPos;
distance = sqrt(dxx*dxx + dyy*dyy);
}while((int)distance <= MinDistance);
}