3 Replies Latest reply on Mar 3, 2007 8:05 AM by kglad

    hitTest but not for boundaries

    MajiDfn
      Hi all
      As you know hitTest for two Movieclips just test hit of both boundaries, so when you have two circled movieclips you have some collisions where there is no overlap in real.
      what do you suggest me to do when real overlap detection of two non-square objects is needed ?!

      regards
      MajiD
        • 1. Re: hitTest but not for boundaries
          tonyhigham
          If you just need to hittest circles, you can do so by checking the distance between the centers of the circles. For instance, assuming you have two circle mc's with center registration, named ball1 and ball2...

          //First, find the x and y distances between the centers
          var distX:Number = Math.abs(ball1._x - ball2._x)
          var distY:Number = Math.abs(ball1._y - ball2._y)

          //Use the x and y distances to find the acual distance
          var distT:Number = Math.sqrt(Math.pow(distX,2)+Math.pow(distY,2))

          //Compare the total distance to 2*the radius
          if(distT <= ball1.width){
          trace("CONTACT")
          } else {
          trace("NO CONTACT")
          }

          This can be used with multiple mc's with a simple 'for' loop. Keep in mind that you may need to adjust the conditional statement to compensate for errors due to the fact that your mc's move by more than 1 pixel per frame.
          • 2. Re: hitTest but not for boundaries
            MajiDfn Level 1
            Dear tonyhigham
            thanks for your quick response.
            It's very useful if all the movieclips are exactly circle.
            But what about the situation which you have movieclips with non-geometric shaped ... like a car or something similar.
            any idea would help me alot
            regards
            MajiD