Okay, I tried to Implement the Collision-Resolving.
But there's a problem: When the balls collide, they stick at each other and don't bounce away.
Here's some Code:
int ballradius = 50;
int mass = ballradius;
float newVelY1(float yVel1, float yVel2)
{
float NEWyVel1 = (yVel1 * (mass - mass) + (2 * mass * yVel2)) / (mass + mass);
return NEWyVel1;
}
float newVelY2(float yVel1, float yVel2)
{
float NEWyVel2 = (yVel2 * (mass - mass) + (2 * mass * yVel1)) / (mass + mass);
return NEWyVel2;
}
float newVelX1(float xVel1, float xVel2)
{
float NEWxVel1 = (xVel1 * (mass - mass) + (2 * mass * xVel2)) / (mass + mass);
return NEWxVel1;
}
float newVelX2(float xVel1, float xVel2)
{
float NEWxVel2 = (xVel2 * (mass - mass) + (2 * mass * xVel1)) / (mass + mass);
return NEWxVel2;
}
// In the main-loop:
for (int i = 0; i < ballcount; i++)
{
for (int j = i + 1; j < ballcount; j++)
{
if (Collision(sf::Vector2f(Ball[i].getPosition().y, Ball[i].getPosition().x), sf::Vector2f(Ball[j].getPosition().y, Ball[j].getPosition().x)))
{
Ball[i].move(newVelX1(xSpeed[i], xSpeed[j]) * dt, newVelY1(ySpeed[i], ySpeed[j]) * dt);
Ball[j].move(newVelX2(xSpeed[i], xSpeed[j]) * dt, newVelY2(ySpeed[i], ySpeed[j])* dt);
xSpeed[i] = newVelX1(xSpeed[i], xSpeed[j]);
xSpeed[j] = newVelX2(xSpeed[i], xSpeed[j]);
ySpeed[i] = newVelY1(ySpeed[i], ySpeed[j]);
ySpeed[j] = newVelY2(ySpeed[i], ySpeed[j]);
}
}
}
Has anybody an idea what I did wrong?