The following articles are reproduced to http://blog.csdn.net/i_dovelemon/article/details/32904251, thanks to bloggers for sharing knowledge and learning from bloggers.
Introduction
In 3D collision detection, in order to speed up the efficiency of collision detection and reduce unnecessary collision detection, basic geometry is used as the bounding body of the object (bounding Volume, BV) for testing. Collision detection of the basic bounding body is relatively inexpensive and much easier, so there is no need for more complex collision detection if it is not passed in the collision detection of the basic bounding body.
And for different properties, different shapes of the model, according to the circumstances of the choice of different bounding body, generally speaking, the surrounding body is divided into the following: Sphere, AABB, OBB, 8-dop, convex hull of these kinds of common. Next, we will tell you how to use the sphere surround body.
Representation Method
To use the bounding body, we need to know how to represent a bounding sphere. For a sphere, it is very simple, as shown below:
[CPP] view plain copy struct Sphere {VECTOR3 Center; float radious; };
So, with the midpoint and the radius, we can only determine a bounding sphere.
collision detection between the surrounding spheres
For the collision detection between the surrounding ball, it is very simple, as long as the distance between the two to determine whether the distances between the sphere is less than the sum of their two radii. If it is less than, then the two surrounding spheres intersect, and if not, separate from each other. The following is the code for Collision detection:[CPP] View plain copy int testspheresphere (sphere a, sphere b) { VECTOR3 d = a.center - b.center ; float dist2 = Dot (d, d); float radisum = a.radious + b.radious ; if (dist2 < radisum * radisum) return 1 ; else return 0 ; }
It's simple, No. Since the square calculation of the open-squared consumes a lot of CPU, we directly compare the square of the distance between the sphere and the sum of their radius, and the result is consistent with the sum of the distance and radius.
calculation of the bounding sphere
There are many algorithms for the calculation of spherical bounding bodies, and two common computational methods are described in this article. If you use DirectX, you'll know that DirectX has a d3dxcomputboundingsphere function built into it. Instead of using this function, we will use our own calculations to create a calculation that surrounds the sphere.
First, we introduce the first method of calculating the bounding sphere.
mean method (this method should be very slow)
We know that in the representation of a 3D model, a series of vertices is generally used to describe a model. So, to ask for a bounding sphere, we have to determine the sphere that surrounds it, and then calculate the distance between each vertex and the sphere, choosing the longest distance as the radius of the bounding sphere. This simple algorithm is able to determine a bounding sphere. So, there is a question of how to determine the sphere of the model.
We use the following simple method to calculate the spherical sphere of this model. We add all the vertices and divide by the number of vertices so that we can get a position of the sphere.
Here, the theory of this method has been introduced, it is easy not ... Here's a look at the code section of this method:
[CPP] View plain copy void sphere::computeboundingsphereaverage (vector3* vertices, unsigned Int vertex_num) { //compute the center point VECTOR3 total ; total.x = 0 ; total.y = 0 ; total.z = 0 ; for (int i = 0 ; i < vertex_num ; i ++) { total.x += vertices[i].x ; total.y += vertices[i].y ; total.z += vertices[i].z ; }// end for total.x /= vertex_num ; total.y /= vertex_num ; total.z /= vertex_num ;