Collision Detection AS3 Util

Collision Detection Utility class for AS3,

Usage: var collide:Rectangle = checkForCollision(obj1, obj2);

package {
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
import flash.geom.Rectangle;

public class CollisionDetection{

public static function checkForCollision(firstObj: DisplayObject,secondObj: DisplayObject):Rectangle
{
var bounds1:Object = firstObj.getBounds(firstObj.root);
var bounds2:Object = secondObj.getBounds(secondObj.root);

if (((bounds1.right < bounds2.left) || (bounds2.right < bounds1.left)) || ((bounds1.bottom < bounds2.top) || (bounds2.bottom < bounds1.top)) ) {
return null;
}

var bounds:Object = {};
bounds.left = Math.max(bounds1.left,bounds2.left);
bounds.right= Math.min(bounds1.right,bounds2.right);
bounds.top = Math.max(bounds1.top,bounds2.top);
bounds.bottom = Math.min(bounds1.bottom,bounds2.bottom);

var w:Number = bounds.right-bounds.left;
var h:Number = bounds.bottom-bounds.top;

if(w < 1 || h < 1)
{
return null;
}

var bitmapData:BitmapData = new BitmapData(w,h,false);

var matrix:Matrix = firstObj.transform.concatenatedMatrix;
matrix.tx -= bounds.left;
matrix.ty -= bounds.top;
bitmapData.draw(firstObj,matrix,new ColorTransform(1,1,1,1,255,-255,-255,255));

matrix = secondObj.transform.concatenatedMatrix;
matrix.tx -= bounds.left;
matrix.ty -= bounds.top;
bitmapData.draw(secondObj,matrix,new ColorTransform(1,1,1,1,255,255,255,255),BlendMode.DIFFERENCE);

var intersection:Rectangle = bitmapData.getColorBoundsRect(0xFFFFFFFF,0xFF00FFFF);

/*try{
bitmapData.dispose();
}catch(e:Error){}*/

if (intersection.width == 0) { return null; }

intersection.x += bounds.left;
intersection.y += bounds.top;

return intersection;
}
}
}

About these ads
  1. This doesn’t work as the as2 version written by gskinner, it doesn’t work at all

    • ruthklempel
    • February 9th, 2009

    Hi, What about checking the intersection with the rectangle methods?

    var intersects:Boolean = obj1.getRect(this).intersects(obj2.getRect(this));

    Regards,

    Ruth

  2. Thank you, it works great!!!

    Ruth: You can’t use the intersection with the rectangle methods always. If obj1 is a PNG Image (a circle, for example) and obj2 is other PNG Image (a rectangle), the method return “true” even when the (imaginary) corners of the circle doesn’t touch the rectangle.

    The area of the yellow rectangle is contained into the area of the circle, but there is not collision.

    Max

    • dataffect
    • February 17th, 2010

    I combined this with facial recognition to determine when the face is outside the specified boundaries.

    Thanks it works great.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 442 other followers

%d bloggers like this: