• home
  • forum
  • my
  • kt
  • download
  • Home / 2D Graphics / Flash / Games /

    Collision Detection Between Circles

    Author: 2008-10-08 08:23:50 From:

    Collision detection between circles is highly useful because it allows for more accurate collision detection with circles and other objects that require a collision detection other than ‘hitTest’. In this tutorial I will explain how to create collision detections with circles.

    Tutorial
    We must first find the distance between the circles. To find this we use:


    distx = circle1._x-circle2._x
    disty= circle1._y-circle2._y
    distance = Math.sqrt((distx*distx)+(disty*disty))

    The first line of code finds the distance between the _x values of the circles and the second finds the distance between the _y values. The third line finds the distance between the two circles (If you are wondering why: The Pythagorean Theorem allows us to find the third line, or the distance in the triangle created by the two distances by finding the root of (x*x)+(y*y). ) Now that we have the distance between the circles, we have to find the minimal distance between the circles before there is a collision.

    In this diagram you can see that the closest possible distance they can get before touching is the sum of the radii. Therefore, we know that the minimal distance can be found with:

    mindist = (circle1._width/2) + (circle2._width/2)

    So with these variables we know the actual test is:
    if(distance<mindist){
    //collision
    }

    Final Code

    distx = circle1._x-circle2._x
    disty= circle1._y-circle2._y
    distance = Math.sqrt((distx*distx)+(disty*disty))
    mindist = (circle1._width/2) + (circle2._width/2)
    if(distance<mindist){
    //collision
    }

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      3D (20)
      Math Physics (14)
      3rd Party (5)
      Navigation (60)
      Actionscripting (26)
      Optimization (16)
      Animation (32)
      Projector (9)
      Audio (46)
      Special Effects (112)
      Backend (25)
      Text Effects (65)
      Drawing (18)
      Tips and Techniques (41)
      Dynamic Content (25)
      Tricks (6)
      Games (82)
      Utilities (21)
      Getting Started (91)
      Video (24)
      Interactivity (43)
      Web Design (29)

    New

    Hot