1 Replies - 2963 Views - Last Post: 10 May 2014 - 11:24 PM

#1 copenhagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 16-March 08

collision detection actionscript 3

Posted 12 April 2014 - 09:51 AM

Hi

I have a problem with a precise collision detection test.

I have two movieclips on the stage with a registration point in the middle.
One called redClip and the other called blueClip.
The problem is that it only shows a collision between the two movieclips when their registration points touch eachother,but they should show a collision when their pixels were overlapping?
All help will be appreciated.
The code below is at frame 1 in my flash movie.

var redRect:Rectangle = redClip.getBounds(this);
var redClipBmpData = new BitmapData(redRect.width, redRect.height, true, 0);
redClipBmpData.draw(redClip);

var blueRect:Rectangle = blueClip.getBounds(this);
var blueClipBmpData = new BitmapData(blueRect.width, blueRect.height, true, 0);
blueClipBmpData.draw(blueClip);

addEventListener(Event.ENTER_FRAME, enterFrame);

function enterFrame(e:Event):void
{
    blueClip.x = mouseX;
    blueClip.y = mouseY;

    if(redClipBmpData.hitTest(new Point(redClip.x, redClip.y),
                                255,
                                blueClipBmpData,
                                new Point(blueClip.x, blueClip.y),
                                255

                          ))
    {
        trace("hit");
        redClip.filters = [new GlowFilter()];
    }
    else
    {
        redClip.filters = [];
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: collision detection actionscript 3

#2 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 592
  • Joined: 15-March 14

Re: collision detection actionscript 3

Posted 10 May 2014 - 11:24 PM

Very simple box overlap test can be adapted to your needs as long as the videos are not rotated.

// Returns true for overlap and false for no overlap
// X,Y top left of first box
// W,H width height of first box;
// X1,Y1 Top left of second box
// W1,H1 width and height of second box
function doBoxsOverlap = function(X, Y, W, H, X1, Y1, W1, H1){
    // test for overlap
    if((X > X1 + W1 || X + W < X1)||(Y > Y1 + H1 || Y + H < Y1)){
        return false; // no they do not overlap
    }
    return true;  // yes they do overlap
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1