Image collision detection method
By employing a bisection method to quickly scan the four boundaries of an image object to construct an AABB bounding box in image collision detection, the performance bottleneck and complexity issues of existing algorithms are resolved, achieving efficient image collision detection suitable for teaching tools.
Patent Information
- Application Number
- CN202511196104.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-26
- Publication Date
- 2025-12-19
AI Technical Summary
Existing image collision detection algorithms in Scratch suffer from performance bottlenecks, are complex to implement, and have an imbalance between accuracy and efficiency. In particular, the pursuit of precise collision detection in teaching scenarios leads to performance degradation.
A binary search method is used to quickly scan the four boundaries of an image object, construct an AABB bounding box, and determine collisions in the world coordinate system by the intersection of the bounding boxes, avoiding pixel-level detection and adapting to the needs of different teaching scenarios.
It achieves efficient image collision detection, simplifies code maintenance, is suitable for integration with teaching tools, avoids performance loss caused by overly precise calculations, and meets teaching needs.
Smart Images

Figure CN121169818A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computers, and in particular to an image collision detection method. Background Technology
[0002] The existing image collision detection algorithms in Scratch have the following problems:
[0003] 1. Performance bottleneck: Existing methods obtain contour points by point scanning and construct convex hulls, and then combine them with AABB bounding boxes for pixel-level judgment, which has high computational complexity and takes a long time.
[0004] 2. Complex implementation: Existing algorithms rely on Monotone Chain to generate convex hulls and combine them with pixel detection, which is logically complex and not adaptable to teaching scenarios.
[0005] 3. Imbalance between accuracy and efficiency: For teaching scenarios, existing methods overemphasize precise collision detection (such as pixel-level judgment), resulting in performance sacrifice, while coarse detection can meet the needs in actual teaching.
[0006] Invention Object
[0007] To address the aforementioned problems in the prior art, this invention provides an image collision detection method.
[0008] An image collision detection method includes the following steps:
[0009] Step 1, Establish the world coordinate system: Establish a standard Cartesian coordinate system with the center of the stage area as the origin;
[0010] Step 2, establish the image coordinate system: take the top left corner of the image as the origin, extend the width of the image to the right as the X-axis, and increase the value from left to right. Extend the height of the image downwards as the Y-axis, and increase the value from top to bottom. All pixels of the image correspond to a coordinate point in the image coordinate system.
[0011] Step 3, construct the AABB bounding box of the image coordinate system: find the top, bottom, left and right boundaries of the smallest rectangular region containing the object in the image coordinate system using the quad-scan method, i.e., the AABB bounding box;
[0012] Step 4: Transform the image coordinate system AABB bounding box to the world coordinate system;
[0013] Step 5: In the world coordinate system, use the transformed AABB bounding box to determine whether a collision has occurred by the intersection of the edges.
[0014] Furthermore, the quad-side scanning method in step 3 specifically involves dividing the scanning direction by the image midpoint (width / 2, height / 2) and scanning the image object using a bisection method to obtain the top, bottom, left, and right boundaries of the AABB bounding box.
[0015] Furthermore, the scanning of the midpoint row of the image object using the bisection method specifically includes:
[0016] Step 301, Scan the upper boundary.
[0017] Objective: Find the Y-coordinate of the first row containing non-transparent pixels.
[0018] Search range: Y-coordinate from the origin Y=0 to the midline Y=height / 2, where height represents the image height.
[0019] Search strategy: Using Y a =0 and Y b =height / 2 represents the two starting edges, and the midpoint row Y is scanned using a binary search method. mid =m=(Y a +Y b Is the value at ) / 2 empty? If so, use Y. b =Y mid -1 and Y a =0 as the starting edge, then scan using a binary search method. If it is blank, then use Y as the starting edge. a =Y mid +1 and Y b =height / 2 is the starting edge, and then a binary search is performed; the above binary search is repeated until Y is reached. a >Y b At that time, take the midpoint of the object in row Y. mid Given the top boundary of the AABB bounding box, return the value of m, which is the number of blank lines starting from the top;
[0020] Step 302, Scan the lower boundary.
[0021] Objective: Find the Y-coordinate of the last row containing non-transparent pixels.
[0022] Search range: Y-coordinate from Y = height / 2 to Y = height-1.
[0023] Search strategy, using Y a =height / 2 and Y b =height-1 represents the two starting edges, and the midpoint row Y is scanned using a binary search method. mid =n=(Y a +Y b Is the value at ) / 2 empty? If so, use Y.a =Y mid +1 and Y b = height-1 as the starting edge, then scan using a binary search method. If it is blank, then use Y as the starting edge. b =Y mid -1 and Y a =height / 2 is the starting edge, and then a binary search is performed; the above binary search is repeated until Y is reached. a >Y b At that time, take the midpoint of the object in row Y. mid Given the bottom boundary of the AABB bounding box, return the height-n value, which is the number of blank lines from the bottom.
[0024] Step 303, scan the left boundary.
[0025] Objective: Find the X-coordinate of the first column containing non-transparent pixels.
[0026] Search range: X coordinate from X=0 to X=width / 2,
[0027] Search strategy: Using X a =0 and X b =width / 2 represents the two starting edges on the left and right. A binary search method is used to scan the midpoint row X. mid =L=(X) a +X b Is the value at ) / 2 blank? If it contains an object, then use X. b =X mid -1 and X a Starting with the edge, a binary search is performed; if it's blank, then X is used as the starting edge. mid +1 and X b Starting with X, scan using a binary search method; repeat the binary search process until X is found. a >X b When, take the midpoint of the object as the X-axis. mid Given the left boundary of the AABB bounding box, return the value L, which is the number of blank columns starting from the left.
[0028] Step 304, Scan the right boundary.
[0029] Objective: Find the X-coordinate of the last column containing non-transparent pixels.
[0030] Search range: X coordinate from X = width / 2 to X = width-1,
[0031] Search strategy: Using X a =width / 2 and X b =width-1 represents the two starting edges on the left and right, and the midpoint row X is scanned using a binary search method. mid=R=(X) a +X b Is the value at ) / 2 blank? If it contains an object, then use X. a =X mid +1 and X b Starting with the edge, a binary search is performed. If the edge is blank, then X is used as the starting edge. b =X mid -1 and X a Starting with the right edge, scan using a bisection method; repeat the bisection scan until X is reached. a >X b When, take the midpoint of the object as the X-axis. mid Given the right boundary of the AABB bounding box, return the width-R value, which is the number of blank columns from the right.
[0032] Furthermore, all images in the world coordinate system have a position in the world coordinate system as the basis for image rendering.
[0033] Furthermore, step 4 specifically includes: calculating the AABB bounding box of the world coordinate system based on the image size, the four boundaries (top, bottom, left, and right) in the image coordinate system, the scaling ratio, and the center position of the object in the world coordinate system. The formula is: World coordinate boundary = world position of object ± (image half size - corresponding direction padding) × scaling ratio.
[0034] Furthermore, when calculating the AABB bounding box in the world coordinate system, if the object is rotated, an inverse rotation transformation is applied to calculate the rotated AABB bounding box.
[0035] Furthermore, step 5 specifically involves: in the world coordinate system, comparing whether the two AABB bounding boxes have an intersection; if so, scanning the X coordinate from the intersection edge with the largest Y coordinate to the right to determine if there is a pixel at the coordinate; if so, a collision occurs; if not, continuing to scan the other three intersection edges in sequence.
[0036] Furthermore, the scanning accuracy of the intersection edges can be dynamically adjusted to adapt to different teaching scenarios.
[0037] The beneficial effects of this invention are as follows: This invention abandons the traditional convex hull generation algorithm and uses the binary search method to quickly scan the four sides of the character and directly construct the AABB bounding box, avoiding complex pixel-level contour calculations, making the code easier to maintain, suitable for integration into teaching tools, meeting teaching needs through coarse detection, and avoiding performance loss caused by overly precise calculations. Attached Figure Description
[0038] Figure 1 This is a flowchart of the method of the present invention. Detailed Implementation
[0039] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0040] In this embodiment: Refer to Figure 1 As shown in the figure, an image collision detection method according to an embodiment of the present invention includes the following steps:
[0041] Step 1, Establish the world coordinate system: Establish a standard Cartesian coordinate system with the center of the stage area as the origin; all images in the world coordinate system have a position in the world coordinate system as the basis for image rendering.
[0042] Step 2, establish the image coordinate system: take the top left corner of the image as the origin, extend the width of the image to the right as the X-axis, and increase the value from left to right. Extend the height of the image downwards as the Y-axis, and increase the value from top to bottom. All pixels are described based on a point in the image's own coordinate system.
[0043] Step 3, Construct the AABB bounding box in the image coordinate system: Find the top, bottom, left, and right boundaries of the smallest rectangular region containing the object in the image coordinate system using the quad-scan method; specifically: divide the scan direction using the image midpoint (width / 2, height / 2), and perform a bisection scan of the image object along the midpoint row to obtain the top, bottom, left, and right boundaries of the AABB bounding box, including:
[0044] Step 301, Scan the upper boundary.
[0045] Objective: Find the Y-coordinate of the first row containing non-transparent pixels.
[0046] Search range: Y-coordinate from the origin Y=0 to the midline Y=height / 2, where height represents the image height.
[0047] Search strategy: Using Y a =0 and Y b =height / 2 represents the two starting edges, and the midpoint row Y is scanned using a binary search method. mid =m=(Y a +Y b Is the value at ) / 2 empty? If so, use Y. b =Y mid -1 and Y a =0 as the starting edge, then scan using a binary search method. If it is blank, then use Y as the starting edge.a =Y mid +1 and Y b =height / 2 is the starting edge, and then a binary search is performed; the above binary search is repeated until Y is reached. a >Y b At that time, take the midpoint of the object in row Y. mid Given the top boundary of the AABB bounding box, return the value of m, which is the number of blank lines starting from the top;
[0048] Step 302, Scan the lower boundary.
[0049] Objective: Find the Y-coordinate of the last row containing non-transparent pixels.
[0050] Search range: Y-coordinate from Y = height / 2 to Y = height-1.
[0051] Search strategy, using Y a =height / 2 and Y b =height-1 represents the two starting edges, and the midpoint row Y is scanned using a binary search method. mid =n=(Y a +Y b Is the value at ) / 2 empty? If so, use Y. a =Y mid +1 and Y b = height-1 as the starting edge, then scan using a binary search method. If it is blank, then use Y as the starting edge. b =Y mid -1 and Y a =height / 2 is the starting edge, and then a binary search is performed; the above binary search is repeated until Y is reached. a >Y b At that time, take the midpoint of the object in row Y. mid Given the bottom boundary of the AABB bounding box, return the height-n value, which is the number of blank lines from the bottom.
[0052] Step 303, scan the left boundary.
[0053] Objective: Find the X-coordinate of the first column containing non-transparent pixels.
[0054] Search range: X coordinate from X=0 to X=width / 2,
[0055] Search strategy: Using X a =0 and X b =width / 2 represents the two starting edges on the left and right. A binary search method is used to scan the midpoint row X. mid =L=(X) a +X bIs the value at ) / 2 blank? If it contains an object, then use X. b =X mid -1 and X a Starting with the edge, a binary search is performed; if it's blank, then X is used as the starting edge. mid +1 and X b Starting with X, scan using a binary search method; repeat the binary search process until X is found. a >X b When, take the midpoint of the object as the X-axis. mid Given the left boundary of the AABB bounding box, return the value L, which is the number of blank columns starting from the left.
[0056] Step 304, Scan the right boundary.
[0057] Objective: Find the X-coordinate of the last column containing non-transparent pixels.
[0058] Search range: X coordinate from X = width / 2 to X = width-1,
[0059] Search strategy: Using X a =width / 2 and X b =width-1 represents the two starting edges on the left and right, and the midpoint row X is scanned using a binary search method. mid =R=(X) a +X b Is the value at ) / 2 blank? If it contains an object, then use X. a =X mid +1 and X b Starting with the edge, a binary search is performed. If the edge is blank, then X is used as the starting edge. b =X mid -1 and X a Starting with the right edge, scan using a bisection method; repeat the bisection scan until X is reached. a >X b When, take the midpoint of the object as the X-axis. mid Given the right boundary of the AABB bounding box, return the width-R value, which is the number of blank columns from the right.
[0060] Step 4: Transform the AABB bounding box of the image coordinate system to the world coordinate system. Specifically: Calculate the AABB bounding box of the world coordinate system based on the image size, the four boundaries (top, bottom, left, and right) in the image coordinate system, the scaling ratio, and the center position of the object in the world coordinate system. The formula is: World coordinate boundary = Object world position ± (Image half-size - Corresponding direction padding) × Scaling ratio.
[0061] Left boundary: worldX - (halfwidth - leftPadding) * scaleX
[0062] Right boundary: worldX + (halfwidth - rightPadding) * scaleX
[0063] Upper bound: worldY + (halfHeight - topPadding) * scaleY
[0064] Lower bound: worldY - (halfHeight - bottomPadding) * scaleY
[0065] The complete calculation process of AABB in the world coordinate system is as follows:
[0066] Step 1: Obtain the padding information of the image coordinate system. padding = {topPadding: number of pixel rows in the transparent area at the top of the image, bottomPadding: number of pixel rows in the transparent area at the bottom of the image, leftPadding: number of pixel columns in the transparent area on the left side of the image, rightPadding: number of pixel columns in the transparent area on the right side of the image}. The padding information can be obtained by using the m, n, L, and R values returned in Step 2.
[0067] Step 2: Obtain the image size skinSize = const[width, height]. Then the distance from the image center to the edge is:
[0068] const halfwidth=Math.floor(width / 2),
[0069] const halfHeight=Math.floor(height / 2),
[0070] The distance from the image center to the actual content boundary is:
[0071] leftoLeft = halfwidth - leftpadding / / Center to left content boundary
[0072] leftoRight = halfwidth - rightpadding / / Center to right content boundary
[0073] leftoBottom = halfHeight-bottompadding / / Center to bottom content boundary
[0074] leftoTop = halfHeight - toppadding / / Center to top content boundary
[0075] Note: halfwidth-leftpadding represents the distance from the center of the image to the left edge of the content area. If leftpadding=10 and halfwidth=50, then toLeft=40, which means that the actual content edge is 40 pixels to the left of the image center.
[0076] Step 3: Apply scaling transformation
[0077] if (scaleX !== 100) {
[0078] toLeft=Math.floor(toLeft*scaleX*0.01)
[0079] toRight=Math.floor(toRight*scaleX*0.01)
[0080] }
[0081] if (scaleY !== 100)i
[0082] toTop=Math.floor(toTop*scaleY*0.01)
[0083] toBottom=Math.floor(toBottom*scaleY*0.01)}
[0084] }
[0085] Step 4: Obtain AABB in the world coordinate system
[0086] / / position is the center position of the object in the world coordinate system.
[0087] let left = position[0] - toLeft / / Left boundary of world coordinates
[0088] let right = position[0] + toRight / / Right boundary of world coordinates
[0089] letbottom = position[1] - toBottom / / Lower boundary of world coordinates
[0090] let top = position[1] + toTop / / Upper boundary of world coordinates
[0091] / / Handling negative scaling (flipping)
[0092] if(right <left){
[0093] [left, right]=[right, left]}
[0094] }
[0095] When calculating the AABB bounding box in the world coordinate system, if the object is rotated, an inverse rotation transformation is applied. After the transformation, the boundaries are checked to ensure that the transformed coordinates are within the image range. Through the above steps, the following are achieved: 1. Transparent area exclusion: AABB automatically excludes transparent boundaries through padding calculation; 2. Precise boundaries: Compared to simple rectangular boundaries, it provides pixel-level accurate collision boundaries; 3. Performance balance: Pre-calculated padding avoids the overhead of real-time pixel detection.
[0096] Step 5: In the world coordinate system, use the transformed AABB bounding boxes to determine if a collision has occurred by checking the intersection of edges. Specifically: In the world coordinate system, compare whether two constructed AABB bounding boxes intersect. Only if an intersection exists will subsequent pixel-level detection be performed; if so, start scanning the X coordinate from the intersection edge with the largest Y coordinate to the right, checking if there is a pixel at that coordinate. If so, a collision has occurred; otherwise, continue scanning the other three intersection edges in sequence. The scanning accuracy can be dynamically adjusted to adapt to different teaching scenarios.
[0097] The pixel detection process is as follows: 1. Coordinate verification: Check whether the coordinates of the transformed image are within the valid range; 2. Pixel access: Access the RGBA value of the corresponding pixel through ImageData; 3. Transparency judgment: Check the alpha channel value, alpha>0 indicates that there is color; 4. Result return: Return whether there is a non-transparent pixel at this position.
[0098] Since individual objects are continuous in an image, boundary collisions are sufficient to determine overall collisions. When performing collision detection, only the boundary lines of the intersection area are detected, rather than the entire area, thereby significantly reducing the number of pixels that need to be detected.
[0099] This solution enables: 2D game collision detection: precise pixel-level collision judgment; image overlap detection: determining whether two images visually overlap; UI element interaction: detecting mouse clicks and interactions with irregularly shaped elements; physics simulation; and serves as a preprocessing step for precise collision detection.
[0100] In the description of embodiments of the present invention, the terms "first," "second," "third," and "fourth" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Thus, a feature defined as "first," "second," "third," or "fourth" may explicitly or implicitly include one or more of that feature. In the description of the present invention, unless otherwise stated, "a plurality of" means two or more.
[0101] In the description of embodiments of the present invention, the term "and / or" is used only to describe the relationship between associated objects, indicating that three relationships can exist. For example, A and / or B can represent three cases: A alone, A and B simultaneously, and B alone. Additionally, the character " / " generally indicates that the preceding and following associated objects are in an "or" relationship.
[0102] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims
1. An image collision detection method, characterized in that, Includes the following steps: Step 1, Establish the world coordinate system: Establish a standard Cartesian coordinate system with the center of the stage area as the origin; Step 2, establish the image coordinate system: take the top left corner of the image as the origin, extend the width of the image to the right as the X-axis, and increase the value from left to right. Extend the height of the image downwards as the Y-axis, and increase the value from top to bottom. All pixels of the image correspond to a coordinate point in the image coordinate system. Step 3, construct the AABB bounding box of the image coordinate system: find the top, bottom, left and right boundaries of the smallest rectangular region containing the object in the image coordinate system using the quad-scan method, i.e., the AABB bounding box; Step 4: Transform the image coordinate system AABB bounding box to the world coordinate system; Step 5: In the world coordinate system, use the transformed AABB bounding box to determine whether a collision has occurred by the intersection of the edges.
2. The image collision detection method according to claim 1, characterized in that, The four-sided scanning method in step 3 specifically involves dividing the scanning direction by the image midpoint (width / 2, height / 2) and scanning the image object using a bisection method to obtain the top, bottom, left, and right boundaries of the AABB bounding box.
3. The image collision detection method according to claim 2, characterized in that, The scanning of the midpoint row of the image object using the bisection method specifically includes: Step 301, Scan the upper boundary. Objective: Find the Y-coordinate of the first row containing non-transparent pixels. Search range: Y-coordinate from the origin Y=0 to the midline Y=height / 2, where height represents the image height. Search strategy: Using Y a =0 and Y b =height / 2 represents the two starting edges, and the midpoint row Y is scanned using a binary search method. mid =m=(Y a +Y b Is the value at ) / 2 empty? If so, use Y. b =Y mid -1 and Y a =0 as the starting edge, then scan using a binary search method. If it is blank, then use Y as the starting edge. a =Y mid +1 and Y b =height / 2 is the starting edge, and then a binary search is performed; the above binary search is repeated until Y is reached. a >Y b At that time, take the midpoint of the object in row Y. mid Given the top boundary of the AABB bounding box, return the value of m, which is the number of blank lines starting from the top; Step 302, Scan the lower boundary. Objective: Find the Y-coordinate of the last row containing non-transparent pixels. Search range: Y-coordinate from Y = height / 2 to Y = height-1. Search strategy, using Y a =height / 2 and Y b =height-1 represents the two starting edges, and the midpoint row Y is scanned using a binary search method. mid =n=(Y a +Y b Is the value at ) / 2 empty? If so, use Y. a =Y mid +1 and Y b = height-1 as the starting edge, then scan using a binary search method. If it is blank, then use Y as the starting edge. b =Y mid -1 and Y a =height / 2 is the starting edge, and then a binary search is performed; the above binary search is repeated until Y is reached. a >Y b At that time, take the midpoint of the object in row Y. mid Given the bottom boundary of the AABB bounding box, return the height-n value, which is the number of blank lines from the bottom. Step 303, scan the left boundary. Objective: Find the X-coordinate of the first column containing non-transparent pixels. Search range: X coordinate from X=0 to X=width / 2, Search strategy: Using X a =0 and X b =width / 2 represents the two starting edges on the left and right. A binary search method is used to scan the midpoint row X. mid =L=(X) a +X b Is the value at ) / 2 blank? If it contains an object, then use X. b =X mid -1 and X a Starting with the edge, a binary search is performed; if it's blank, then X is used as the starting edge. mid +1 and X b Starting with X, scan using a binary search method; repeat the binary search process until X is found. a >X b When, take the midpoint of the object as the X-axis. mid Given the left boundary of the AABB bounding box, return the value L, which is the number of blank columns starting from the left. Step 304, Scan the right boundary. Objective: Find the X-coordinate of the last column containing non-transparent pixels. Search range: X coordinate from X = width / 2 to X = width-1, Search strategy: Using X a =width / 2 and X b =width-1 represents the two starting edges on the left and right, and the midpoint row X is scanned using a binary search method. mid =R=(X) a +X b Is the value at ) / 2 blank? If it contains an object, then use X. a =X mid +1 and X b Starting with the edge, a binary search is performed. If the edge is blank, then X is used as the starting edge. b =X mid -1 and X a Starting with the right edge, scan using a bisection method; repeat the bisection scan until X is reached. a >X b When, take the midpoint of the object as the X-axis. mid Given the right boundary of the AABB bounding box, return the width-R value, which is the number of blank columns from the right.
4. The image collision detection method according to claim 1, characterized in that, In the world coordinate system, every image has a position in the world coordinate system as the basis for image rendering.
5. The image collision detection method according to claim 1, characterized in that, Step 4 specifically includes: calculating the AABB bounding box of the world coordinate system based on the image size, the four boundaries (top, bottom, left, and right) in the image coordinate system, the scaling ratio, and the position of the object in the world coordinate system. The formula is: World coordinate boundary = world position of object ± (image half size - corresponding direction Padding) × scaling ratio, where Padding is the return value of scanning each boundary.
6. The image collision detection method according to claim 5, characterized in that, When calculating the AABB bounding box in the world coordinate system, if the object is rotated, an inverse rotation transformation is applied to calculate the rotated AABB bounding box.
7. The image collision detection method according to claim 1, characterized in that, Step 5 specifically involves: in the world coordinate system, comparing whether the two AABB bounding boxes have an intersection; if they do, then starting from the intersection edge with the largest Y coordinate, scanning the X coordinate to the right to determine if there is a pixel at the coordinate; if there is, a collision occurs; if not, then continue scanning the other three intersection edges in sequence.
8. The image collision detection method according to claim 7, characterized in that, The scanning accuracy of the intersection edges can be dynamically adjusted to adapt to different teaching scenarios.