Checking for HitTest/Collision Detection of DisplayObjects


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*
  2. Copyright (c) 2008 Tink Ltd - http://www.tink.ws
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  6. the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  7. to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8.  
  9. The above copyright notice and this permission notice shall be included in all copies or substantial portions
  10. of the Software.
  11.  
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  13. THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  15. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. SOFTWARE.
  17.  */
  18.  
  19. package ws.tink.display {
  20.  
  21. import flash.display.BitmapData;
  22. import flash.display.BlendMode;
  23. import flash.display.DisplayObject;
  24. import flash.geom.ColorTransform;
  25. import flash.geom.Matrix;
  26. import flash.geom.Point;
  27. import flash.geom.Rectangle;
  28.  
  29.  
  30. public class HitTest {
  31.  
  32. public static function complexHitTestObject( target1 : DisplayObject, target2 : DisplayObject, accuracy : Number = 1 ):Boolean {
  33. return complexIntersectionRectangle( target1, target2, accuracy ).width != 0;
  34. }
  35.  
  36. public static function intersectionRectangle( target1 : DisplayObject, target2 : DisplayObject ):Rectangle {
  37. // If either of the items don't have a reference to stage, then they are not in a display list
  38. // or if a simple hitTestObject is false, they cannot be intersecting.
  39. if( !target1.root || !target2.root || !target1.hitTestObject( target2 ) )
  40. 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, accuracy : Number = 1 ):Rectangle {
  57. if( accuracy <= 0 )
  58. throw new Error( "ArgumentError: Error #5001: Invalid value for accuracy", 5001 );
  59.  
  60. // If a simple hitTestObject is false, they cannot be intersecting.
  61. if( !target1.hitTestObject( target2 ) )
  62. return new Rectangle();
  63.  
  64. var hitRectangle : Rectangle = intersectionRectangle( target1, target2 );
  65. // If their boundaries are no interesecting, they cannot be intersecting.
  66. if( hitRectangle.width * accuracy < 1 || hitRectangle.height * accuracy < 1 )
  67. return new Rectangle();
  68.  
  69. var bitmapData : BitmapData = new BitmapData( hitRectangle.width * accuracy, hitRectangle.height * accuracy, false, 0x000000 );
  70.  
  71. // Draw the first target.
  72. bitmapData.draw( target1, HitTest.getDrawMatrix( target1, hitRectangle, accuracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) );
  73. // Overlay the second target.
  74. bitmapData.draw( target2, HitTest.getDrawMatrix( target2, hitRectangle, accuracy ), new ColorTransform( 1, 1, 1, 1, 255, 255, 255, 255 ), BlendMode.DIFFERENCE );
  75.  
  76. // Find the intersection.
  77. var intersection : Rectangle = bitmapData.getColorBoundsRect( 0xFFFFFFFF, 0xFF00FFFF );
  78.  
  79. bitmapData.dispose();
  80.  
  81. // Alter width and positions to compensate for accuracy
  82. if( accuracy != 1 ) {
  83. intersection.x /= accuracy;
  84. intersection.y /= accuracy;
  85. intersection.width /= accuracy;
  86. intersection.height /= accuracy;
  87. }
  88.  
  89. intersection.x += hitRectangle.x;
  90. intersection.y += hitRectangle.y;
  91.  
  92. return intersection;
  93. }
  94.  
  95.  
  96. protected static function getDrawMatrix( target : DisplayObject, hitRectangle : Rectangle, accuracy : Number ):Matrix {
  97. var localToGlobal : Point;
  98. var matrix : Matrix;
  99.  
  100. var rootConcatenatedMatrix : Matrix = target.root.transform.concatenatedMatrix;
  101.  
  102.  
  103.  
  104. localToGlobal = target.localToGlobal( new Point() );
  105. matrix = target.transform.concatenatedMatrix;
  106. matrix.tx = localToGlobal.x - hitRectangle.x;
  107. matrix.ty = localToGlobal.y - hitRectangle.y;
  108.  
  109. matrix.a = matrix.a / rootConcatenatedMatrix.a;
  110. matrix.d = matrix.d / rootConcatenatedMatrix.d;
  111. if( accuracy != 1 )
  112. matrix.scale( accuracy, accuracy );
  113.  
  114. return matrix;
  115. }
  116.  
  117. }
  118.  
  119. }

URL: http://www.tink.ws/blog/as-30-hittest/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.