Android detection collision Summary

Source: Internet
Author: User
Android game detection collision method
I. map lattice division Detection
The simplest kind of detection is to divide a map (or scene, in short it refers to the range of collision occurrence) into grids, similar to the legend of the fairy sword. Assume that the map has 800 * 600px, and 20*20 pixels are a grid. It can be classified as 40*30 grids. The objects involved in the detection in the map store their own grid coordinates, and the collision judgment is obvious. For example, two objects can be considered as collision in adjacent cells, or two objects in the same grid. There is a requirement to use this method, that is, all objects in the map that may participate in the collision should be around 20*20 pixels or an integer multiple of them. For example, the house occupies 3*3 grids, and so on. If you do not comply with this rule, some objects only occupy half of the grid, the detection looks very rough in the eyes of players. This kind of detection is like enlarging a map's pixels by dozens of times. Compared with Pixel-by-pixel detection, the efficiency is improved by dozens or even hundreds of times. This method can be used for games that do not have strict detection requirements, such as mine-based RPG games and smart games such as box pushing.
Ii. Rectangle Detection
When an object in a map cannot be drawn exactly by an integer multiple of the block size, another method is required. This method is applicable when an object in a map is approximately a rectangle or is not a rectangle But has low collision precision requirements. Each object records the coordinates of the upper left corner of the smallest rectangle that can be framed by itself and the length and width of the rectangle. Collision degrades to determine whether the rectangle and the rectangle overlap, and this only requires four comparisons, and the speed is very fast. However, in order to determine the objects in the entire scenario, the first object must be taken, all other objects must be iterated for judgment, and the second object must be taken, and all objects except the first and second objects must be iterated for judgment, and so on. Total to be performed (n-1 )! Only when the secondary rectangle is determined can all collision possibilities be obtained accurately.

3. Circular Detection
Similar to the previous method, the difference is that a rectangle is replaced by the smallest circle that can contain objects. It mainly takes into account that the shape of an object in the game is smooth, such as a character role. The Calculation of determining whether two circles collide is simple, that is, to determine whether the distance between the two centers is smaller than the sum of the radius of the two circles. Although the accuracy of Sphere detection is improved in some cases, the speed is lost because the square sum of squares is used for calculating the vertex distance. I don't know how slow it is. In addition, (n-1) is also required to calculate all collision possibilities of the entire map )! Time comparison.

Iv. pixel detection
Accurate to the pixel level, it is no longer more accurate than this, relative, and the efficiency is also the lowest. How can we determine whether two objects collide? In the past, when PNG images were not popular, the transparent part of the images used in the game was specified to be represented by a certain color, such as magenta. Just like the green screen blue screen in a movie, the pixels of these colors are processed as transparent points. In order to determine the detection, you need to prepare a black-and-white image of the original image. The black area indicates transparent, each pixel value in this image is 0 or 1. When detecting the image, take the black and white images of the two images for calculation. If the result is 1 (with white points overlapping), the result is a collision. However, with PNG and xNa, pixel-by-pixel detection is relatively simple. First, you still need to have a rectangular box to enclose the object. The overlapping rectangular area obtained through rectangular detection can greatly reduce the number of detected pixels. Then, in this area, the points of the two images are iterated row by row and column. If there is a color between the two images at a certain point, it is judged as a collision. Similarly, perform (n-1 )! After the comparison, the possibility of a full map collision is obtained.

V. quad-tree Detection
Accurately speaking, this is the optimization strategy based on the third or fourth methods, or the combination of the first method and the last three methods. This is mainly for the final (n-1 )! Time comparison. The method is to divide the map into grids like the first method. The size of the grid should be able to accommodate the largest object in a map of about 10, for example, a 800*600 map may be divided into nine zones. Similarly, each object must record its own zone coordinates and rectangular box. If the object is completely located in the zone, you only need to determine the collision with other objects in the zone. If the object is located in a certain area but a small part is located in the next area, an additional object needs to iterate over the next area. This loss of efficiency is tolerable, compared to an object that iterates the entire map.

How do I know which objects are located in the same zone as the object? Isn't it necessary to iterate all objects? At this time, this is where the question is played. It is called the quad-tree detection (of course, this name is obtained by myself) because those blocks are linked in the form of a quad-tree, you can obtain the objects in the upper, lower, and lower adjacent blocks of a block. objects can be stored in a list of blocks. In this way, you can retrieve objects in the next area without traversing all objects. When the map is large, the advantages of the quad-tree are very good.

Vi. 3D Collision Detection
The above is the collision method I have mastered. There may be more. The 3D detection is actually an extension of 2D, for example, the rectangular detection becomes Cube Body Detection, circular detection is extended to spherical detection, and Quadtree detection evolved to octree detection.

Of course, there are exceptions. The pixel-by-pixel detection method does not correspond to each other in 3D, because the smallest element in 3D represents a triangle rather than a vertex. In fact, we can also say that triangle-by-triangle detection is a 3D version of pixel-by-pixel detection, but after all, it is a plane Collision Detection and requires a certain calculation formula, rather than just a match or a click. I will not go into details here.


Android region collision
Http://www.apkbus.com/android-71766-1-1.html

Android rectangle collision
Http://www.apkbus.com/android-70408-1-1.html

Android round collision
Http://www.apkbus.com/android-70407-1-1.html

Android pixel collision  
Http://www.apkbus.com/android-71769-1-1.html  

Android line segment and rectangle Collision Detection
Http://www.apkbus.com/android-70405-1-1.html

Simple Collision Detection for 3D games  
Http://www.apkbus.com/android-70398-1-1.html 

Circle and rectangle Collision Detection in 2D games  
Http://www.apkbus.com/android-70411-1-1.html  
Related Article

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.