Advertisement
Guest User

Untitled

a guest
May 3rd, 2010
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.Bitmap;
  4.     import flash.display.BitmapData;
  5.     import flash.display.BlendMode;
  6.     import flash.display.DisplayObject;
  7.     import flash.display.Sprite;
  8.     import org.flixel.FlxG;
  9.     import org.flixel.FlxPoint;
  10.     import org.flixel.FlxSprite;
  11.     import org.flixel.FlxU;
  12.    
  13.     import flash.geom.ColorTransform;
  14.     import flash.geom.Matrix;
  15.     import flash.geom.Point;
  16.     import flash.geom.Rectangle;
  17.  
  18.     public class FlxHitTest
  19.     {
  20.         public static function complexHitTestObject( target1:FlxSprite, target2:FlxSprite,  accurracy:Number = 1 ):Boolean
  21.         {
  22.             var bitmap1:Bitmap = flxspriteToBitmap(target1);
  23.             var bitmap2:Bitmap = flxspriteToBitmap(target2);
  24.            
  25.             FlxG.state.addChild(bitmap1);
  26.             FlxG.state.addChild(bitmap2);
  27.            
  28.             var result:Boolean = complexIntersectionRectangle( bitmap1, bitmap2, accurracy ).width != 0;
  29.            
  30.             FlxG.state.removeChild(bitmap1);
  31.             FlxG.state.removeChild(bitmap2);
  32.            
  33.             return result;
  34.         }
  35.  
  36.         public static function intersectionRectangle( target1:DisplayObject, target2:DisplayObject ):Rectangle
  37.         {
  38.             // If either of the items don't have a reference to stage, then they are not in a display list
  39.             // or if a simple hitTestObject is false, they cannot be intersecting.
  40.             if( !target1.root || !target2.root || !target1.hitTestObject( target2 ) ) return new Rectangle();
  41.  
  42.             // Get the bounds of each DisplayObject.
  43.             var bounds1:Rectangle = target1.getBounds( target1.root );
  44.             var bounds2:Rectangle = target2.getBounds( target2.root );
  45.  
  46.             // Determine test area boundaries.
  47.             var intersection:Rectangle = new Rectangle();
  48.             intersection.x      = Math.max( bounds1.x, bounds2.x );
  49.             intersection.y      = Math.max( bounds1.y, bounds2.y );
  50.             intersection.width  = Math.min( ( bounds1.x + bounds1.width ) - intersection.x, ( bounds2.x + bounds2.width ) - intersection.x );
  51.             intersection.height = Math.min( ( bounds1.y + bounds1.height ) - intersection.y, ( bounds2.y + bounds2.height ) - intersection.y );
  52.  
  53.             return intersection;
  54.         }
  55.  
  56.         public static function complexIntersectionRectangle( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Rectangle
  57.         {          
  58.  
  59.             // If a simple hitTestObject is false, they cannot be intersecting.
  60.             if( !target1.hitTestObject( target2 ) ) return new Rectangle();
  61.  
  62.             var hitRectangle:Rectangle = intersectionRectangle( target1, target2 );
  63.             // If their boundaries are no interesecting, they cannot be intersecting.
  64.             if( hitRectangle.width * accurracy < 1 || hitRectangle.height * accurracy < 1 ) return new Rectangle();
  65.  
  66.             var bitmapData:BitmapData = new BitmapData( hitRectangle.width * accurracy, hitRectangle.height * accurracy, false, 0x000000 );
  67.  
  68.             // Draw the first target.
  69.             bitmapData.draw( target1, FlxHitTest.getDrawMatrix( target1, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) );
  70.             // Overlay the second target.
  71.             bitmapData.draw( target2, FlxHitTest.getDrawMatrix( target2, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, 255, 255, 255 ), BlendMode.DIFFERENCE );
  72.  
  73.             // Find the intersection.
  74.             var intersection:Rectangle = bitmapData.getColorBoundsRect( 0xFFFFFFFF,0xFF00FFFF );
  75.  
  76.             bitmapData.dispose();
  77.  
  78.             // Alter width and positions to compensate for accurracy
  79.             if( accurracy != 1 )
  80.             {
  81.                 intersection.x /= accurracy;
  82.                 intersection.y /= accurracy;
  83.                 intersection.width /= accurracy;
  84.                 intersection.height /= accurracy;
  85.             }
  86.  
  87.             intersection.x += hitRectangle.x;
  88.             intersection.y += hitRectangle.y;
  89.  
  90.             return intersection;
  91.         }
  92.        
  93.         public static function flxspriteToBitmap(Graphic:FlxSprite):Bitmap
  94.         {
  95.             var bitmap:Bitmap = new Bitmap(Graphic._framePixels);
  96.            
  97.             var mtx:Matrix = new Matrix();
  98.             mtx.translate( -Graphic.origin.x, -Graphic.origin.y);
  99.             mtx.scale(Graphic.scale.x, Graphic.scale.y);
  100.             mtx.rotate(Math.PI * 2 * Graphic.angle / 360);
  101.             mtx.translate(Graphic.x - Graphic.offset.x + Graphic.origin.x, Graphic.y - Graphic.offset.y + Graphic.origin.y);
  102.            
  103.             bitmap.transform.matrix = mtx;
  104.            
  105.             return bitmap;
  106.         }
  107.        
  108.         protected static function getDrawMatrix( target:DisplayObject, hitRectangle:Rectangle, accurracy:Number ):Matrix
  109.         {
  110.             var localToGlobal:Point;;
  111.             var matrix:Matrix;
  112.  
  113.             var rootConcatenatedMatrix:Matrix = target.root.transform.concatenatedMatrix;
  114.  
  115.             localToGlobal = target.localToGlobal( new Point( ) );
  116.             matrix = target.transform.concatenatedMatrix;
  117.             matrix.tx = localToGlobal.x - hitRectangle.x;
  118.             matrix.ty = localToGlobal.y - hitRectangle.y;
  119.  
  120.             matrix.a = matrix.a / rootConcatenatedMatrix.a;
  121.             matrix.d = matrix.d / rootConcatenatedMatrix.d;
  122.             if( accurracy != 1 ) matrix.scale( accurracy, accurracy );
  123.  
  124.             return matrix;
  125.         }
  126.  
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement