A 3D Game collision detection Solution

Source: Internet
Author: User

Collision Detection on3DThe game is crucial. A good collision detection requires that the characters can be smoothly moved in the scene, and the steps within a certain height can automatically go up, while the high steps block people, when a slope with a small slope can be up, the slope is too large to block people, in the case of various forward direction is blocked, we should try to let the characters slide along a reasonable direction rather than forced to stop. In addition to meeting these requirements, it is necessary to ensure accuracy and stability, so as to prevent people from going through the wall in special circumstances.

Collision Detection should be done well and won't be noticed, because it is in line with our common sense in our daily life. It is easy to find that the characters are often stuck and cannot move forward or the characters have crossed the obstacle. Therefore, most people think that the write Collision DetectionCodeIs a thankless thing,AlgorithmComplex and easy-to-useBug. Let's go back to the question and see how we can solve this problem.

earlier 3D game collision detection was mostly based on grids or BSP / span> tree, the lattice-based system is easy to implement but not accurate enough. It is not strictly 3D collision detection. Collision Detection Based on the BSP tree was once very popular, and the algorithm was basically mature and finalized. However, its inherent shortcomings make it unsuitable for current games. The BSP tree requires a long pre-processing time and is not suitable for computing during loading, BSP usually generates a polygon with three to four times the number of the original polygon, considering that you do not need to save information such as the normal, color, and UV , you also need to increase the resource capacity by nearly doubled. In a large game, you can change the capacity of model resources from 200 m added 400 m most people are reluctant to accept it. Currently, for any complex triangle set ( mesh ) most Collision Detection Based on bvtree ( bounding volume tree ) , It Can Be AABB tree , OBB tree , or K-dop tree , this is also a popular practice of today's various physical engines and collision detection engines.

collision detection is classified by data structure, based on the detection method, it can be divided into discrete point Collision Detection and continuous collision detection ( CCD continuous collision detection ). The collision detection of discrete points is to specify the two static collision bodies of T at a certain time point to see if they overlap, if there is no overlap, the distance from the nearest vertex is returned. If there is overlap, the depth and direction of the overlap are returned. Consecutive collision detection specifies the positions of two collision bodies at t1 and t2 , check whether the collision occurs during the time from t1 to t2 , if a collision occurs, the position and normal of the first collision point are returned. Continuous collision detection is the most natural collision detection, which greatly facilitates the compilation of the Collision Response logic and can easily avoid overlapping or traversing objects. The collision detection of discrete points is not so friendly. When the collision is detected, the two objects already overlap. If there are triangle mesh objects, many triangles already overlap, how to separate two overlapping objects and exercise in a proper way is a challenge. Although continuous collision detection is the most natural method, its implementation is very complicated and Its computing overhead is also large, therefore, most mature physical engines and collision detection engines still adopt discrete point-based collision detection. To avoid the overlap between objects or cross each other, they all adopt relatively small simulated step sizes.

Due to the complexity of the collision detection engine and high efficiency requirements, we should try to use the mature and complete engine instead of self-developed. After evaluation, I decided to adoptOpcodeThe collision detection engine is used to detect the collision of characters and scenes in games.OpcodeThe main function is to useAABB treeManage complex triangle collections and Ray and sphere,CubeBody, another set of triangles, and so on. If the overlap is detected, all triangles that overlap are returned.OpcodeIt features high memory usage optimization and excellent efficiency,OdeThe bottom layer of the physical engine uses it for complex triangles.MeshCollision Detection,OpcodeThe author is alsoNovodex(PhysxCore developers of the physical engine, saidNovodexAdoptedOpcode. It can be seen thatOpcodeMaturity and efficiency.

The engine to be used is determined. The algorithms discussed below are irrelevant to the specific engine and are suitable for collision detection engines of any discrete points. We useAABBUse the box to represent the characters in the scenario and see how to implement it.ArticleEffect.

First, let's explain the method of detecting the ground. Four rays are cast down along the four vertical edges of the character's box. The end point of the ray is slightly lower than the bottom of the character's foot (that is, the length of the ray is limited ), if a collision occurs with the scene and the slope of the collision plane is smaller than a certain value, the height of the collision point is obtained. Otherwise, this Ray is regarded as not detecting the ground. Use the maximum ground height detected by all rays as the final ground height. If none of the four rays detect the ground, the person is considered to be suspended.

 Vd =Number of Characters in the current frame

P0 =Current location of the character box Center

Bongroundp1 ;//Whether the character is standing on the ground

Bongroundp3 ;//Whether the character is standing on the ground

Bonground ;//Whether the character is standing on the ground

 

P1 = P0 + VD

InP1Location Detection ground

If (Ground Detected)

{

Place the box to the ground to get the locationP2

Bongroundp1 = true;

}

Else

{

P2 = p1;

Bongroundp1 = false;

}

TestP2Whether the surrounding box of point overlaps with the scenario

If (Overlapping)

{

Obtain the normal of all overlapping triangles, add them, and take the average value to obtain the normal.Normal

Multiply the normal and the upward vector to get the tangent direction.Tangent

//Calculate the position of the character sliding along the tangent.P0Computing.

//If you want to smooth the slide, you canP0Introduced to the normal direction

// P3 = P0 + normal * 0.1f + VD. Dot (tangent );

P3 = P0 + VD. Dot (tangent );

 

InP3Location Detection ground

If (Ground Detected)

{

Place the box to the ground to get the locationP4

Bongroundp3 = true;

}

Else

{

P4 = P3;

Bongroundp3 = false;

}

 

TestP4Whether the surrounding box of point overlaps with the scenario

If (Overlapping)

{

TestP1Whether the surrounding box of point overlaps with the scenario

If (Overlapping)

{

//A reasonable Moving position cannot be obtained, and the character position remains unchanged.

//Wait for the player to adjust the forward direction of the next frame for the test.

SetP0New position as a character

Bonground = true;

Return;

}

Else

{

SetP1New position as a character

Bonground = bongroundp1;

Return;

}

}

Else

{

SetP4New position as a character

Bonground = bongroundp3;

Return;

}

}

Else

{

SetP2New position as a character

Bonground = bongroundp1;

Return;

}

 

the above algorithm basically achieves the effect mentioned at the beginning of the article. In some complex situations, the movement of characters is still not smooth, but no obstacle is found. In most cases, the new coordinates of a person are returned by the P2 point. The worst case is that the person gets stuck and returns the P0 point. In p4 3.0 GB supports 120 games with 30 frames in the worst case. number of frames.

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.