Stack Exchange
Collision between two circles not behaving correctly
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
#include <Windows.h>
#include <iostream>
#include <math.h>
#include <cmath>
#include <thread>
#include <random>
double DISTANCE( sf::Vector2f l, sf::Vector2f k);
bool Check_Collision(sf::CircleShape x, sf::CircleShape y);
double angle_opp(sf::Vector2f l, sf::Vector2f k);
double angle(sf::Vector2f l, sf::Vector2f k);
double angle_adj(sf::Vector2f l, sf::Vector2f k);


int main() {
    int i = 1;
    int x1 = 400;
    int y1 = 200;
    int x2 = 200;
    int y2 = 200;
    int rad = 20;
    sf::Clock clock;
   sf::RenderWindow window(sf::VideoMode(1000,1000),"Sheka Snake");
   sf::CircleShape circle1;

   sf::CircleShape boost;

   
  
   circle1.setPosition(x1,y1);
   circle1.setRadius(rad);
 
   boost.setPosition(300, 500);
   boost.setRadius(10);
   boost.setFillColor(sf::Color::Red);
   window.clear();
   window.draw(circle1);
  
   window.display();
   window.setFramerateLimit(60);
   window.setVerticalSyncEnabled(false);
   
  
  
   sf::Vector2f f1;
   sf::Vector2f f2;
   
   bool collid = false;
 
   
   while (window.isOpen())

   {
      
       sf::Event event;
       while (window.pollEvent(event))
       {


          
           if (event.type == sf::Event::Closed)
               window.close();

       }

       float fps = clock.restart().asMilliseconds();
       float speed = 0.3f * fps;

       
       if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
       {

           circle1.move(-speed, 0);



       }
       if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
       {

           circle1.move(0, -speed);



       }
       if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
       {

           circle1.move(0, speed);


       }
       if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
       {

           circle1.move(speed, 0);


       }
      


       if (Check_Collision(circle1, boost) == true) {
           std::random_device dev;
           std::mt19937 rng(dev());

           std::uniform_int_distribution<std::mt19937::result_type> dist6(0, 1000);// distribution in range [1, 6]

           std::cout << dist6(rng)<<std::endl;
           
           boost.setPosition(dist6(rng), dist6(rng));
           rad = rad + 30;
           circle1.setRadius(rad);



       }
       sf::Vector2f l;
       sf::Vector2f k;
       l = circle1.getPosition();
       k = boost.getPosition();
       
  
       window.clear();
       window.draw(circle1);
       window.draw(boost);
       
      
       window.display();
       

       
   }
      

       
   }





double DISTANCE(sf::Vector2f l, sf::Vector2f k) {
    double dy = (l.y - k.y);
    double dx = (l.x - k.x);
    double d = sqrt(pow(dy, 2) + pow(dx, 2));
   
    return d;



}

double angle_opp(sf::Vector2f l, sf::Vector2f k) {
    return (k.y - l.y);
   
    


}
double angle_adj(sf::Vector2f l, sf::Vector2f k) {

    return (k.x - l.x);


}

double angle(sf::Vector2f l, sf::Vector2f k) {

    return atan((k.y - l.y) / (k.x - l.x));


}


bool Check_Collision(sf::CircleShape x,sf::CircleShape y) {
    using namespace sf;
    Vector2f v1; Vector2f v2;
    v1 = x.getPosition();
    v2 = y.getPosition();
    std::cout << "x:" << v2.x << "y:" << v2.y << std::endl;
    
    double dis =DISTANCE(v1,v2);
    double a;
   
    if (dis <= x.getRadius() + y.getRadius()) {
           
            
            
            return true;
            

        }
    }
    






Hello I am a beginner trying to learn SFML and I tried making a ball collide with another ball externally but the collision doesn't work correctly, sometimes the ball collides before it touches the other ball and sometimes it collides after going inside one another. The distance after the first collision doesn't seem to be calculated correctly

Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example.

Isn't your compiler screaming at you that Check_Collision does not always return something?

From the code you provided i think the problem which you're trying to fix is created due to the fact that you're calculating all these distances and comparing them and stuff but dont take into concideration the starting (origin) offset. When we usuall compare or calculate the distances of these objects the start of the objects for us is always at the center. whereas in sfml the origin is always in the top left corner of the shape. for any nonrectangular shape, its the top left corner of the rectangle (invisible) drawn just around the edges of the shape.
But theres an easier solution. SFML already has a built in basic collision:

int main()
{
sf::CircleShape circ1;
sf::CircleShape circ2;
cout << circ1.getGlobalBounds().intersects(circ2) << endl;

The above function takes the area of the circ1 and checks if any other sf::Drawable is in contact with this shape. Hope this helped
