Cocos2d-x tutorial (33)-3D object AABB Collision Detection Algorithm

Source: Internet
Author: User

Cocos2d-x 3.3beta0 AABB collision detection OBB Ma zongyang


Welcome to Cocos2d-x chat group: 193411763


Reprinted please indicate the original source: http://blog.csdn.net/u012945598/article/details/38870705


Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------


After the support for 3D objects is added to Cocos2d-x version 3.x, the collision detection method of 3D objects is also updated. One of the simplest collision detection methods is AABB collision detection.


1. AABB box

In the game, to simplify collision detection operations between objects, a regular geometric shape is usually created to enclose the objects.

Specifically, the AABB (axis-aligned bounding box) box is called the axis to enclose it.


AABB box in two-dimensional scenarios has the characteristics :( Note: Because the Cocos2d-x is based on OpenGL ES, so all coordinate systems in the right hand Cartesian coordinate system)


(1) a quadrilateral is used to enclose an object.

(2) Each side of the quadrilateral is perpendicular to the axis of the coordinate system.


1-1:

Figure 1-1


Features of the AABB enclosure in 3D scenarios:


(1) The format is cubes.

(2) Each side of the cubes is parallel to a coordinate plane.

1-2:

Figure 1-2 (Image Source: Baidu)

In Figure 1-2, an OBB (oriented bounding box) is displayed on the rightmost side to show more clearly the features of the AABB box.

We can see that the most direct difference between the aabb and the obb is that the AABB box cannot be rotated, while the OBB box can be rotated, that is, directed.


2. AABB collision detection principle in two-dimensional scenarios first, let's look at an object collision diagram in two-dimensional scenarios:

Figure 2-1


In Figure 2-1, the projection of object A and object B in the X and Y axes is performed respectively. The maximum coordinate of the Y axis of object A is Y1, and the minimum coordinate is Y2, in the X axis direction, the minimum coordinate X1 and the maximum coordinate X2 are the same as those in object B.

In the figure, the red area overlaps the projection of object A and object B.


We can see that AABB collision detection has the following rules:

Objects A and B are projected along the two coordinate axes respectively. Only when the two coordinate axes overlap can the two objects cause a collision.

Therefore, when performing a two-dimensional game AABB Collision Detection in a program, you only need to verify whether object A and object B meet the following conditions:

(1) the minimum value of the Y axis of object A is greater than the maximum value of the Y axis of object B;

(2) the minimum value of the x-axis direction of object A is greater than the maximum value of the x-axis direction of object B;

(3) the minimum value of the Y axis of object B is greater than the maximum value of the Y axis of object;

(4) the minimum value of the x-axis direction of object B is greater than the maximum value of the x-axis direction of object;

If the above conditions are met, it is proved that object A and object B do not overlap. Otherwise, it is proved that object A and object B overlap.


3. the AABB collision detection principle in 3D scenarios first, let's look at the two-dimensional object A and object B in 2-1, we can find that we only need to know two pieces of information to determine whether object A and object B overlap:

(1) minimum vertex information of object A, that is, the lower-left vertex of object A in Figure 2-1, and the maximum vertex information of object A, that is, the upper-right vertex of object A in Figure 2-1.

(2) The minimum vertex information of object B and the maximum vertex information of object B.

That is to say, in the collision detection of two-dimensional scenes, the vertex coordinate information of each object can be determined by two coordinates, that is, two coordinates can identify an object, therefore, the collision detection of two objects only requires four coordinate points.


As you can see in Figure 1-2, the AABB box of an object in a 3D scenario is a, and its coordinate system only has one Z axis while it is in a 2D coordinate system, in fact, in 3D scenarios, the AABB Collision Detection of objects can still be achieved through the determination of four points. That is, two vertices, maximum and minimum, are selected from the eight vertices of object A and the eight vertices of object B for comparison. The eight vertices in the AABB box of a 3D object can still be identified by two vertices, as shown in 3-1:

Figure 3-1


As long as the coordinates of the Black points in the graph are determined, all information of the eight vertices can be determined.


At Cocos2d-x 3. in Version X, the AABB class is provided for developers to save information about the maximum and minimum vertices of the box, and an interface for each sprite3d object to obtain the AABB box is provided, the AABB class also provides methods for determining the corresponding collision detection.


Next we will analyze the source code of AABB:


Ccaabb. h file

Ccaabb. cpp File

4. Finally, AABB met Zhuang CE's algorithm, although its calculation method is simple and fast, but it is only applicable to games with no precision. Compared with AABB collision detection, OBB collision detection is also an algorithm that is closer to an object and more precise. The corresponding OBB collision detection method is also provided in the Cocos2d-x, as shown in 4-1:

Figure 4-1


Cocos2d-x tutorial (33)-3D object AABB Collision Detection Algorithm

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.