Collision Detection of two circles - Simple Java Code

Java : Drawing of two circles, mouse motion event handler on one circle, and detect collision with other.

Determining whether or not two circles intersect or overlap or collide is done by comparing the distance between the two circles to the sum of radius of the two circles.


Full working code for collision detection only download here: 


Steps
1)Find the distance between the centers of the two circles(a and b) using the distance formula
 float dxSq = (a.x - b.x) * (a.x - b.x);
 float dySq = (a.y - b.y) * (a.y - b.y);
 int d = (int) Math.sqrt(dxSq + dySq);

2)Then the distance is compared with the radii .
    int r1Pr2 = (int) (a.radius + b.radius);
    if (d < r1Pr2) {
        System.out.println("Collided");
    } else if (d == r1Pr2) {
        System.out.println("Just touching");
    } 



Collision detection and response - bounce examples

3 comments :

  1. dude, how can i capture real time video from two web cam in to my JFrame so that i can process it later by buffer image. any suggestion will be welcome.

    ReplyDelete
    Replies
    1. See this post for capturing real time video from a web cam. http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/javacv-capture-save-flip-show-live.html
      This code uses single thread "GrabberShow" to capture image from webcam 1 -
      FrameGrabber grabber = new VideoInputFrameGrabber(1);

      You can create another similar thread "GrabberShow2" and capture image from another camera.

      You must not forget that the integer sent to VideoInputFrameGrabber represents webcam count, which starts from 0.

      Hope this helps.

      Delete
    2. ya... thankss... i got it..
      now i need solution for another question.. how can i count framerate of specific camera in it..? it must be in pure javaCV... pls......

      Delete

Your Comment and Question will help to make this blog better...