2d Elastic Collision with Circles

  c++, collision, sfml

I’ve seen there is a lot of posts about this already but I can’t find one that relates to what I want to do,
I used the formula from here:
https://www.vobarian.com/collisions/2dcollisions2.pdf
As well as this one:
https://www.plasmaphysics.org.uk/programs/coll2d_cpp.htm
I think they area basically the same thing, now my problem is one of my circles is always static, and what I want is when the other circle hits it straight on, I want it to bounce back with the same speed, but these formulas have the circle stop still, presumably as it would pass it’s energy to the other circle which would then move away.
I tried doing things like bounce = vel.x pre collision – vel.y post collision and add or subtract that to vel.x post collision and it kinda works but not really, the angles are wrong and depending on which direction the ball is coming from it may bounce up instead of down, left instead of right,
would probably require a lot of if/else statements to get to work at all.
Can someone suggest something?
here’s the code for the function :

void Collision2(sf::CircleShape* b1, sf::CircleShape* b2, sf::Vector2f vel1,sf::Vector2f& vel2) {
        //vel1 is 0,0  but i might want to use it later
        //mass
        float m1 = 10;
        float m2 = 10;
        //normal vector
        sf::Vector2f nVec((b2->getPosition().x - b1->getPosition().x), (b2->getPosition().y - b1->getPosition().y));
        //unit vector
        sf::Vector2f uNVec(nVec / sqrt((nVec.x * nVec.x) + (nVec.y * nVec.y)));
        //unit tangent vec
        sf::Vector2f uTVec(-uNVec.y, uNVec.x);

        float v1n = (uNVec.x * vel1.x) + (uNVec.y * vel1.y);
        float v2n = (uNVec.x * vel2.x) + (uNVec.y * vel2.y);
        float v1t = uTVec.x * vel1.x + uTVec.y * vel2.y;
        float v2t = (uTVec.x * vel2.x) + (uTVec.y * vel2.y);


        //v1t and v1n after collision
        float v1tN = v1t;
        float v2tN = v2t;
        float v1nN = (v1n * (m1 - m2) + (2 * m2) * v2n) / (m1 + m2);
        float v2nN = (v2n * (m2 - m1) + (2 * m1) * v1n) / (m1 + m2);

        //new velocities
        sf::Vector2f vel1N(v1nN*uNVec);
        sf::Vector2f vel1tN(v1tN * uTVec);
        sf::Vector2f vel2N(v2nN * uNVec);
        sf::Vector2f vel2tN(v2tN * uTVec);
        vel1 = (vel1N + vel1tN);
        vel2 = (vel2N + vel2tN);

    }

Source: Windows Questions C++

LEAVE A COMMENT