超高效的不规则物体碰撞检测

之前,在坛里看到过有朋友贴过不规则物体碰撞检测的类,这里,我再贴个一位牛老外写的一个类,经自己测试,用一个1500*1500的矢量图和一个 10*10的不短移动的小球进行碰状检测,CPU占用仅4%左右,超级高效!!!
这里是代码
Actionscript:

Java代码  收藏代码
  1. package ws.tink.display  
  2.   
  3. {  
  4.   
  5.           
  6.   
  7.         import flash.display.BitmapData;  
  8.   
  9.         import flash.display.BlendMode;  
  10.   
  11.         import flash.display.DisplayObject;  
  12.   
  13.         import flash.display.Sprite;  
  14.   
  15.           
  16.   
  17.         import flash.geom.ColorTransform;  
  18.   
  19.         import flash.geom.Matrix;  
  20.   
  21.         import flash.geom.Point;  
  22.   
  23.         import flash.geom.Rectangle;  
  24.   
  25.           
  26.   
  27.         public class HitTest  
  28.   
  29.         {  
  30.   
  31.   
  32.   
  33.                 public static function complexHitTestObject( target1:DisplayObject, target2:DisplayObject,  accurracy:Number = 1 ):Boolean  
  34.   
  35.                 {  
  36.   
  37.                         return complexIntersectionRectangle( target1, target2, accurracy ).width != 0;  
  38.   
  39.                 }  
  40.   
  41.                   
  42.   
  43.                 public static function intersectionRectangle( target1:DisplayObject, target2:DisplayObject ):Rectangle  
  44.   
  45.                 {  
  46.   
  47.                         // If either of the items don't have a reference to stage, then they are not in a display list  
  48.   
  49.                         // or if a simple hitTestObject is false, they cannot be intersecting.  
  50.   
  51.                         if( !target1.root || !target2.root || !target1.hitTestObject( target2 ) ) return new Rectangle();  
  52.   
  53.                           
  54.   
  55.                         // Get the bounds of each DisplayObject.  
  56.   
  57.                         var bounds1:Rectangle = target1.getBounds( target1.root );  
  58.   
  59.                         var bounds2:Rectangle = target2.getBounds( target2.root );  
  60.   
  61.                           
  62.   
  63.                         // Determine test area boundaries.  
  64.   
  65.                         var intersection:Rectangle = new Rectangle();  
  66.   
  67.                         intersection.x   = Math.max( bounds1.x, bounds2.x );  
  68.   
  69.                         intersection.y    = Math.max( bounds1.y, bounds2.y );  
  70.   
  71.                         intersection.width      = Math.min( ( bounds1.x + bounds1.width ) - intersection.x, ( bounds2.x + bounds2.width ) - intersection.x );  
  72.   
  73.                         intersection.height = Math.min( ( bounds1.y + bounds1.height ) - intersection.y, ( bounds2.y + bounds2.height ) - intersection.y );  
  74.   
  75.                   
  76.   
  77.                         return intersection;  
  78.   
  79.                 }  
  80.   
  81.                   
  82.   
  83.                 public static function complexIntersectionRectangle( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Rectangle  
  84.   
  85.                 {                       
  86.   
  87.                         if( accurracy <= 0 ) throw new Error( "ArgumentError: Error #5001: Invalid value for accurracy"5001 );  
  88.   
  89.                           
  90.   
  91.                         // If a simple hitTestObject is false, they cannot be intersecting.  
  92.   
  93.                         if( !target1.hitTestObject( target2 ) ) return new Rectangle();  
  94.   
  95.                           
  96.   
  97.                         var hitRectangle:Rectangle = intersectionRectangle( target1, target2 );  
  98.   
  99.                         // If their boundaries are no interesecting, they cannot be intersecting.  
  100.   
  101.                         if( hitRectangle.width * accurracy <1 || hitRectangle.height * accurracy <1 ) return new Rectangle();  
  102.   
  103.                           
  104.   
  105.                         var bitmapData:BitmapData = new BitmapData( hitRectangle.width * accurracy, hitRectangle.height * accurracy, false0x000000 );   
  106.   
  107.   
  108.   
  109.                         // Draw the first target.  
  110.   
  111.                         bitmapData.draw( target1, HitTest.getDrawMatrix( target1, hitRectangle, accurracy ), new ColorTransform( 1111255, -255, -255255 ) );  
  112.   
  113.                         // Overlay the second target.  
  114.   
  115.                         bitmapData.draw( target2, HitTest.getDrawMatrix( target2, hitRectangle, accurracy ), new ColorTransform( 1111255255255255 ), BlendMode.DIFFERENCE );  
  116.   
  117.                           
  118.   
  119.                         // Find the intersection.  
  120.   
  121.                         var intersection:Rectangle = bitmapData.getColorBoundsRect( 0xFFFFFFFF,0xFF00FFFF );  
  122.   
  123.                           
  124.   
  125.                         bitmapData.dispose();  
  126.   
  127.                           
  128.   
  129.                         // Alter width and positions to compensate for accurracy  
  130.   
  131.                         if( accurracy != 1 )  
  132.   
  133.                         {  
  134.   
  135.                                 intersection.x /= accurracy;  
  136.   
  137.                                 intersection.y /= accurracy;  
  138.   
  139.                                 intersection.width /= accurracy;  
  140.   
  141.                                 intersection.height /= accurracy;  
  142.   
  143.                         }  
  144.   
  145.                           
  146.   
  147.                         intersection.x += hitRectangle.x;  
  148.   
  149.                         intersection.y += hitRectangle.y;  
  150.   
  151.                           
  152.   
  153.                         return intersection;  
  154.   
  155.                 }  
  156.   
  157.                   
  158.   
  159.                   
  160.   
  161.                 protected static function getDrawMatrix( target:DisplayObject, hitRectangle:Rectangle, accurracy:Number ):Matrix  
  162.   
  163.                 {  
  164.   
  165.                         var localToGlobal:Point;;  
  166.   
  167.                         var matrix:Matrix;  
  168.   
  169.                           
  170.   
  171.                         var rootConcatenatedMatrix:Matrix = target.root.transform.concatenatedMatrix;  
  172.   
  173.                           
  174.   
  175.                         localToGlobal = target.localToGlobal( new Point( ) );  
  176.   
  177.                         matrix = target.transform.concatenatedMatrix;  
  178.   
  179.                         matrix.tx = localToGlobal.x - hitRectangle.x;  
  180.   
  181.                         matrix.ty = localToGlobal.y - hitRectangle.y;  
  182.   
  183.                           
  184.   
  185.                         matrix.a = matrix.a / rootConcatenatedMatrix.a;  
  186.   
  187.                         matrix.d = matrix.d / rootConcatenatedMatrix.d;  
  188.   
  189.                         if( accurracy != 1 ) matrix.scale( accurracy, accurracy );  
  190.   
  191.   
  192.   
  193.                         return matrix;  
  194.   
  195.                 }  
  196.   
  197.   
  198.   
  199.         }  
  200.   
  201.   
  202.   
  203. }   
原文地址:https://www.cnblogs.com/keng333/p/2439306.html