ball collision detection

限于喜欢 提交于 2019-12-12 18:07:38

问题


im trying to detect what colour ball a white ball initially makes contact with.

the coordinates and colors of all balls are known and the video feed of the balls will be top-view so there is only coordinates in x-y

my code for the detected balls is as follows

*//draw all detected circles
for (int i = 0; i < circles->total; i++)
{
     // round the floats to an int
     float* p = (float*)cvGetSeqElem(circles, i);
     cv::Point center(cvRound(p[0]), cvRound(p[1]));
     int radius = cvRound(p[2]);
     //uchar* ptr;
     //ptr = cvPtr2D(img, center.y, center.x, NULL);
     //printf("B: %d G: %d R: %d\n", ptr[0],ptr[1],ptr[2]);
     CvScalar s;
    s = cvGet2D(img,center.y, center.x);//colour of circle
    printf("B: %f G: %f R: %f\n",s.val[0],s.val[1],s.val[2]);
     // draw the circle center
     cvCircle(img, center, 3, CV_RGB(0,255,0), -1, 8, 0 );
     // draw the circle outline
     cvCircle(img, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );
     //display coordinates
     printf("x: %d y: %d r: %d\n",center.x,center.y, radius);
}* 

回答1:


Since you know the coordinates for the centers of the balls you can just calculate the euclidean distance from the white ball center each ball center. once the distance between two ball centers is the sum of their radiuses then you have your collision. Hope that helps.



来源:https://stackoverflow.com/questions/8767205/ball-collision-detection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!