分享一个超高效的不规则物体碰撞检测的类~~

 之前,在坛里看到过有朋友贴过不规则物体 碰撞检测的类,这里,我再贴个一位牛老外写的一个类,经自己测试,用一个1500*1500的矢量图和一个10*10的不短移动的小球进行碰状检测,CPU占用仅4%左右,超级高效!!!
这里是 代码
Actionscript:
  1. package ws.tink.display
  2. {
  3.         
  4.         import flash.display.BitmapData;
  5.         import flash.display.BlendMode;
  6.         import flash.display.DisplayObject;
  7.         import flash.display.Sprite;
  8.         
  9.         import flash.geom.ColorTransform;
  10.         import flash.geom.Matrix;
  11.         import flash.geom.Point;
  12.         import flash.geom.Rectangle;
  13.         
  14.         public class HitTest
  15.         {

  16.                 public static function complexHitTestObject( target1:DisplayObject, target2:DisplayObject,  accurracy:Number = 1 ):Boolean
  17.                 {
  18.                         return complexIntersectionRectangle( target1, target2, accurracy ).width != 0;
  19.                 }
  20.                
  21.                 public static function intersectionRectangle( target1:DisplayObject, target2:DisplayObject ):Rectangle
  22.                 {
  23.                         // If either of the items don't have a reference to stage, then they are not in a display list
  24.                         // or if a simple hitTestObject is false, they cannot be intersecting.
  25.                         if( !target1.root || !target2.root || !target1.hitTestObject( target2 ) ) return new Rectangle();
  26.                         
  27.                         // Get the bounds of each DisplayObject.
  28.                         var bounds1:Rectangle = target1.getBounds( target1.root );
  29.                         var bounds2:Rectangle = target2.getBounds( target2.root );
  30.                         
  31.                         // Determine test area boundaries.
  32.                         var intersection:Rectangle = new Rectangle();
  33.                         intersection.x   = Math.max( bounds1.x, bounds2.x );
  34.                         intersection.y    = Math.max( bounds1.y, bounds2.y );
  35.                         intersection.width      = Math.min( ( bounds1.x + bounds1.width ) - intersection.x, ( bounds2.x + bounds2.width ) - intersection.x );
  36.                         intersection.height = Math.min( ( bounds1.y + bounds1.height ) - intersection.y, ( bounds2.y + bounds2.height ) - intersection.y );
  37.                
  38.                         return intersection;
  39.                 }
  40.                
  41.                 public static function complexIntersectionRectangle( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Rectangle
  42.                 {                     
  43.                         if( accurracy <= 0 ) throw new Error( "ArgumentError: Error #5001: Invalid value for accurracy", 5001 );
  44.                         
  45.                         // If a simple hitTestObject is false, they cannot be intersecting.
  46.                         if( !target1.hitTestObject( target2 ) ) return new Rectangle();
  47.                         
  48.                         var hitRectangle:Rectangle = intersectionRectangle( target1, target2 );
  49.                         // If their boundaries are no interesecting, they cannot be intersecting.
  50.                         if( hitRectangle.width * accurracy <1 || hitRectangle.height * accurracy <1 ) return new Rectangle();
  51.                         
  52.                         var bitmapData:BitmapData = new BitmapData( hitRectangle.width * accurracy, hitRectangle.height * accurracy, false, 0x000000 );

  53.                         // Draw the first target.
  54.                         bitmapData.draw( target1, HitTest.getDrawMatrix( target1, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) );
  55.                         // Overlay the second target.
  56.                         bitmapData.draw( target2, HitTest.getDrawMatrix( target2, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, 255, 255, 255 ), BlendMode.DIFFERENCE );
  57.                         
  58.                         // Find the intersection.
  59.                         var intersection:Rectangle = bitmapData.getColorBoundsRect( 0xFFFFFFFF,0xFF00FFFF );
  60.                         
  61.                         bitmapData.dispose();
  62.                         
  63.                         // Alter width and positions to compensate for accurracy
  64.                         if( accurracy != 1 )
  65.                         {
  66.                                 intersection.x /= accurracy;
  67.                                 intersection.y /= accurracy;
  68.                                 intersection.width /= accurracy;
  69.                                 intersection.height /= accurracy;
  70.                         }
  71.                         
  72.                         intersection.x += hitRectangle.x;
  73.                         intersection.y += hitRectangle.y;
  74.                         
  75.                         return intersection;
  76.                 }
  77.                
  78.                
  79.                 protected static function getDrawMatrix( target:DisplayObject, hitRectangle:Rectangle, accurracy:Number ):Matrix
  80.                 {
  81.                         var localToGlobal:Point;;
  82.                         var matrix:Matrix;
  83.                         
  84.                         var rootConcatenatedMatrix:Matrix = target.root.transform.concatenatedMatrix;
  85.                         
  86.                         localToGlobal = target.localToGlobal( new Point( ) );
  87.                         matrix = target.transform.concatenatedMatrix;
  88.                         matrix.tx = localToGlobal.x - hitRectangle.x;
  89.                         matrix.ty = localToGlobal.y - hitRectangle.y;
  90.                         
  91.                         matrix.a = matrix.a / rootConcatenatedMatrix.a;
  92.                         matrix.d = matrix.d / rootConcatenatedMatrix.d;
  93.                         if( accurracy != 1 ) matrix.scale( accurracy, accurracy );

  94.                         return matrix;
  95.                 }

  96.         }

  97. }
复制代码


封装了3个静态方法,直接用传参调用即可!

算法值得学习。
这里是原文地址: http://www.tink.ws/blog/as-30-hittest

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值