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.bottom < ) {
return null;

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

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

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 -=;
bitmapData.draw(firstObj,matrix,new ColorTransform(1,1,1,1,255,-255,-255,255));

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

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


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

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

return intersection;

  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));



  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.


    • 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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: