Sharing the pixel-level Collision Detection Algorithm and detailed explanation of an irregular object written by a foreign Daniel

Source: Internet
Author: User

Recently, a small game about shooting requires pixel-level collision detection. The hittestobject provided by as3 obviously cannot meet the needs. I searched the internet and dug two good articles at 9ria:

Sharing an ultra-efficient collision detection class for Irregular Objects ~~

[LII] ultra-efficient Collision Detection of Irregular Objects <efficiency optimization>

The first article introduces an irregular object pixel-level collision detection algorithm written by a foreign Daniel. The principle is to use bitmap to draw two objects in an opaque area and calculate the intersection areas of the two objects in a hybrid mode.

In the second article, the efficiency is optimized based on the algorithm. The principle is to determine whether the collision between two objects occurs after the hittestobject collision, and then narrow down the collision rectangle area to 20*20 for judgment, the reduced detection result may be different from the actual result. Therefore, you need to change the zoom-In rate for multiple checks. However, the efficiency has been improved by 50%-compared with the original algorithm, especially for large-size pixel detection ~ For small objects of 500% and 20*20, the efficiency is the same.

In actual tests, the optimized algorithm, as reflected by netizens, is normal when the detection object is not rotated. When the detection object is rotated, the detection result is abnormal, the effect is similar to the built-in hittestobject, which cannot be detected in pixels. According to the needs of the game, the size of the detected object is not large. It must be within 80*80 and must be able to detect normally when the object is rotated. Therefore, the original algorithm is used. The following is a detailed description of the algorithm to better understand the algorithm principle:

 

1 package 2 {3 Import flash. display. bitmapdata; 4 Import flash. display. blendmode; 5 import flash. display. displayobject; 6 Import flash. display. sprite; 7 8 Import flash. geom. colortransform; 9 Import flash. geom. matrix; 10 Import flash. geom. point; 11 import flash. geom. rectangle; 12 13/** 14 * efficient Collision Detection for Irregular Objects 15 */16 public class hittest 17 {18 public function hittest () 19 {20} 21 22/** determines whether the two objects touch Hit (adjustable precision) */23 public static function complexhittestobject (target1: displayobject, TARGET2: displayobject, accuracy: Number = 1): Boolean 24 {25 return complexintersectionrectangle (target1, TARGET2, accuracy ). width! = 0; 26} 27 28/** obtain the rectangular area of the collision intersection */29 public static function intersectionrectangle (target1: displayobject, TARGET2: displayobject ): rectangle 30 {31 // if any of the objects is not added to the display list, or if the result of the two hittestobjects is false, the two objects do not have a collision 32 If (! Target1.root |! Target2.root |! Target1.hittestobject (TARGET2) return New rectangle (); 33 34 // obtain the rectangular area of the two objects, respectively. 35 var bounds1: rectangle = target1.getbounds (target1.root); 36 var bounds2: rectangle = target2.getbounds (target2.root); 37 38 // obtain the rectangular area of the intersection of the two objects 39 var intersection: rectangle = new rectangle (); 40 intersection. X = math. max (bounds1.x, bounds2.x); 41 intersection. y = math. max (bounds1.y, bounds2.y); 42 intersecti On. width = math. min (bounds1.x + bounds1.width)-intersection. x, (bounds2.x + bounds2.width)-intersection. x); 43 intersection. height = math. min (bounds1.y + bounds1.height)-intersection. y, (bounds2.y + bounds2.height)-intersection. y); 44 45 return intersection; 46} 47 48/** obtain the collision intersection rectangle area (adjustable precision) */49 public static function complexintersectionrectangle (target1: displayobject, targ Et2: displayobject, accuracy: Number = 1): rectangle 50 {51 // If (accuracy <= 0), an error is thrown if (accuracy <= 0) throw new error ("argumenterror: Error #5001: invalid value for accurracy", 5001); 53 54 // if the result of the two hittestobject is false, the two objects do not have a collision 55 if (! Target1.hittestobject (TARGET2) return New rectangle (); 56 57 var hitrectangle: rectangle = intersectionrectangle (target1, TARGET2); 58 // determine whether the length and width of the overlapping area exceed the collision critical value, if the number is not exceeded, the two objects are considered to have not collided. The default critical value is 1. You can adjust the accuracy of 59 If (hitrectangle according to accuracy. width * accuracy <1 | hitrectangle. height * accuracy <1) return New rectangle (); 60 61 62 // ---------------------------------- core algorithm limit 63 // create a temporary bitmapdata object for draw 64 var bitmapdata: bitmapdata = new bitmapdata (hitrectangle. width * accuracy, hitrectangle. height * accuracy, false, 0x000000); 65 66 // draw the opacity of target1 Specify the color 67 bitmapdata. draw (target1, hittest. getdrawmatrix (target1, hitrectangle, accuracy), new colortransform (1, 1, 1, 1,255,-255,-255,255 )); 68 // draw the opacity of TARGET2 as the specified color and set the mixed mode to difference mode 69 bitmapdata. draw (TARGET2, hittest. getdrawmatrix (TARGET2, hitrectangle, accuracy), new colortransform (1, 1, 1, 1,255,255,255,255), blendmode. difference); 70 71 // If the opacity of target1 and TARGET2 intersect The 32-bit color information of the intersection area must be 0xff00ffff, that is, the two objects in the pixel collision Area 72 var intersection: rectangle = bitmapdata. getcolorboundsrect (0 xffffffff, 0xff00ffff); 73 74 bitmapdata. dispose (); 75 // -------------------------------- limit 76 77 // alter width and positions to compensate for accurracy 78 // The front is multiplied by accuracy to zoom two objects, then the overlapping mode is used to calculate the intersection area. Therefore, we need to divide the intersection area by the accuracy and restore the original intersection area with the size of 79 if (accuracy! = 1) 80 {81 intersection. x/= accuracy; 82 intersection. y/= accuracy; 83 intersection. width/= accuracy; 84 intersection. height/= accuracy; 85} 86 87 intersection. X + = hitrectangle. x; 88 intersection. Y + = hitrectangle. y; 89 90 return intersection; 91} 92 93 94 protected static function getdrawmatrix (target: displayobject, hitrectangle: rectangle, accurracy: Number): matrix 95 {96 var localtogl Obal: point; 97 var matrix: matrix; 98 99 var rootconcatenatedmatrix: matrix = target. root. transform. concatenatedmatrix; 100 101 localtoglobal = target. localtoglobal (new point (); 102 matrix = target. transform. concatenatedmatrix; 103 matrix. tx = localtoglobal. x-hitrectangle. *; 104 matrix. ty = localtoglobal. y-hitrectangle. y; 105 106 matrix. A = matrix. a/rootconcatenatedmatrix. a; 107 matrix. D = matri X. d/rootconcatenatedmatrix; 108 If (accurracy! = 1) matrix. Scale (accurracy, accurracy); 109 110 return matrix; 111} 112 113} 114}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.