判断两个任意形状的物体间的碰撞

 
  1.  package org.geom{ 
  2. import flash.display.BitmapData; 
  3. import flash.display.BlendMode; 
  4. import flash.display.DisplayObject; 
  5. import flash.display.Sprite; 
  6. import flash.geom.ColorTransform; 
  7. import flash.geom.Matrix; 
  8. import flash.geom.Point; 
  9. import flash.geom.Rectangle; 
  10. public class BitmapHitTest { 
  11. //判断两个任意形状的物体是否有实际象素接触   
  12. public static function complexHitTestObject( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Boolean { 
  13. return complexIntersectionRectangle(target1,target2,accurracy).width != 0; } 
  14. //返回两个任意形状的物体轮廓接触的矩形区域 
  15. public static function intersectionRectangle( target1:DisplayObject, target2:DisplayObject ):Rectangle { 
  16. // If either of the items don't have a reference to stage, then they are not in a display list 
  17. // or if a simple hitTestObject is false, they cannot be intersecting. 
  18. if ( !target1.root || !target2.root || !target1.hitTestObject( target2 ) ) { return new Rectangle ; } 
  19. // Get the bounds of each DisplayObject. 
  20. var bounds1:Rectangle = target1.getBounds( target1.root );
  21.  var bounds2:Rectangle = target2.getBounds( target2.root );
  22.  // Determine test area boundaries. 
  23. var intersection:Rectangle = new Rectangle();
  24.  intersection.x = Math.max( bounds1.x, bounds2.x );
  25.  intersection.y = Math.max( bounds1.y, bounds2.y ); 
  26. intersection.width = Math.min( ( bounds1.x + bounds1.width ) - intersection.x, ( bounds2.x + bounds2.width ) - intersection.x );
  27.  intersection.height = Math.min( ( bounds1.y + bounds1.height ) - intersection.y, ( bounds2.y + bounds2.height ) - intersection.y ); 
  28. return intersection; } 
  29. //返回两个任意形状的物体实际象素接触的矩形区域 
  30. public static function complexIntersectionRectangle( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Rectangle { 
  31. if ( accurracy <= 0 ) { 
  32. throw new Error("ArgumentError: Error #5001: Invalid value for accurracy",5001); } 
  33. // If a simple hitTestObject is false, they cannot be intersecting. 
  34. if ( !target1.hitTestObject( target2 ) ) {
  35.  return new Rectangle ; } 
  36. var hitRectangle:Rectangle = intersectionRectangle( target1, target2 ); 
  37. // If their boundaries are no interesecting, they cannot be intersecting.
  38.  if ( hitRectangle.width * accurracy <1 || hitRectangle.height * accurracy <1 ) { return new Rectangle ; }
  39.  var bitmapData:BitmapData = new BitmapData( hitRectangle.width * accurracy, hitRectangle.height * accurracy, false, 0x000000 );
  40.  // Draw the first target. 
  41. bitmapData.draw( target1, BitmapHitTest.getDrawMatrix( target1, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) ); 
  42. // Overlay the second target. 
  43. bitmapData.draw( target2, BitmapHitTest.getDrawMatrix( target2, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, 255, 255, 255 ), BlendMode.DIFFERENCE ); 
  44. // Find the intersection. 
  45. var intersection:Rectangle = bitmapData.getColorBoundsRect( 0xFFFFFFFF,0xFF00FFFF ); 
  46. bitmapData.dispose(); 
  47. // Alter width and positions to compensate for accurracy 
  48. if ( accurracy != 1 ) { 
  49. intersection.x /= accurracy; 
  50. intersection.y /= accurracy; 
  51. intersection.width /= accurracy; 
  52. intersection.height /= accurracy; 
  53. intersection.x += hitRectangle.x; 
  54. intersection.y += hitRectangle.y; 
  55. return intersection; } 
  56. //矩阵 
  57. protected static function getDrawMatrix( target:DisplayObject, hitRectangle:Rectangle, accurracy:Number ):Matrix { 
  58. var localToGlobal:Point;
  59.  var matrix:Matrix; 
  60. var rootConcatenatedMatrix:Matrix = target.root.transform.concatenatedMatrix; 
  61. localToGlobal = target.localToGlobal( new Point( ) ); 
  62. matrix = target.transform.concatenatedMatrix; 
  63. matrix.tx = localToGlobal.x - hitRectangle.x; 
  64. matrix.ty = localToGlobal.y - hitRectangle.y; 
  65. matrix.a = matrix.a / rootConcatenatedMatrix.a; 
  66. matrix.d = matrix.d / rootConcatenatedMatrix.d; 
  67. if ( accurracy != 1 ) { matrix.scale( accurracy, accurracy ); }
  68.  return matrix; 
  69. } } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值