assuming you are at a place where the ball is just contacting the brick;

You will have to create a structure that holds a point located on each edge, and a vector for each edge that is the edge's normal.

Give that structure a method that iterates through each point, creates a vector that is the difference between the ball's center &amp; the point, dot it to the corresponding normal, and only one of the 4 results will be a positive number, that's the edge that has intersected.

pseudo code:

struct brick

Vector min, max, L, R, T, B; //letters repre points on edges
Vector normalLeft, normalRight, normalTop, normalBottom;

//ctor
Brick(min, max)
{
   //generate values for all the fields above
}

vector normalOfEdgeHit(Vector ballCtr)
{
   foreach(point i =  L, R, T, B)
   {
     if(Dot(ballCtr - i, normal[i]) &gt; 0)
     {
       return normal[i];
     }

   }

}


in the following diagram, it would return normalB. notice how all the other normals point away from the ball (thus would return values less than 0).
The points can be located anywhere on the edge, they do not need to be in the center.

It is possible to have two normals return positive when close to the corner. In that case, you must determine distance from point to line (as if the brick's edge were an infinitely long line) for both edges. Whichever is a greater distance is the edge that has intersected.



How can I detect collision between a circle and a rectangle?
I have the following code: 

void NewCore::spin(){
    ros::Rate rate(10);

    int first =1;
    while(ros::ok()){

        if (first){         
            askMath();first=0;}
        ros::spinOnce();
        rate.sleep();
    }
}

int main(int argc, char **argv){

    ros::init(argc, argv, "newCore");
    NewCore nc;
    nc.init();

    nc.spin();
}

void NewCore::init(){

    mngrSub = handle.subscribe&lt;std_msgs::String&gt;("/tawi/core/launch", 10, &amp;NewCore::launchCallback, this);
    mngrPub = handle.advertise&lt;std_msgs::String&gt;("/tawi/core/launch", 100);

    mathSub = handle.subscribe&lt;std_msgs::String&gt;("/display", 10, &amp;NewCore::launchCallback, this);
    serSub = handle.subscribe&lt;std_msgs::String&gt;("/tawi/arduino/serial", 100,&amp;NewCore::serialCallback,this);
    mathPub = handle.advertise&lt;std_msgs::String&gt;("/questions", 100);

    ballPub = handle.advertise&lt;std_msgs::Int16&gt;("/tawi/core/ballcount", 100);
    nmbrPub = handle.advertise&lt;std_msgs::Int16&gt;("/tawi/core/number", 100);
}

void NewCore::askMath(){
    ROS_INFO("addition1d&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;");
    std_msgs::String question;
    question.data = "1digitAddition";
    mathPub.publish(question);
}


(code that isn't interesting has been removed)

Running this causes the following output to appear: 

$ rosrun glados newCore 
[ INFO] [1421236273.617723131]: addition1d&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;


However if I have the following code on during launch:

$ rostopic echo /questions 


then it does not show me an initial message being sent.

Changing

        if (first){         
            askMath();first=0;}


into 

            askMath();first=0;


does appear to work but it then sends a message every cycle rather then just the one at the start. 

Does anybody know what is wrong here? 
