Ray-aabb Cross Detection algorithm

Source: Internet
Author: User

?? Recently, when solving three-dimensional problems, it is necessary to judge whether the segment crosses the cube, and the question can be extended as follows: whether the ray passes through the cube aabb.
?? The commonly used algorithm for collision detection in 3D game development is the axis-aligned rectangular bounding box (axially Aligned bounding box, AABB) box method, the basic idea is to use a cube or a sphere to completely wrap the 3D object, and then according to the distance of the box, Location and other relevant information to calculate whether a collision occurred.

Collision Detection Algorithm for slab

?? This paper mainly discusses the relationship between Ray and AABB, mainly introduces the slab Collision detection algorithm (slabs method) used in box2d collision detection, then implements slab collision detection method using Python language, which can be used for 3D object pickup and other application scenarios.
?? Slab English translation is "flat", this article refers to two parallel plane/line between the space. In 2D space, slab can be understood as the area between two straight lines parallel to the axis, and in 3D space is the area between two planes parallel to the XY plane (or the yz face, XZ face). Thus, we can think of the Aabb box in 3D space as the intersection of the slab in 3 directions formed by the 3 parallel planes of AABB.
?? In addition, the concept of a candidate surface is introduced: in 3D space, we first determine the three faces of the ray, that is to say, we can somehow ignore the AABB relative to Ray Ray's back to determine the three candidate faces. These three candidate faces are the closest faces possible to intersect with Ray Ray.

?? According to this definition, we can get the following three conclusions:

    1. Property One: If a point is in Aabb, then this point must be in the same 3 slab.
    2. Property Two: If a ray and AABB intersect, then the intersection of this ray and 3 slab must have coincident parts.
    3. Property Three: When the Rays intersect with one of the three candidate faces, Ray Ray's origin is longer than the distance from the other faces.

?? The nature of the first and the nature of the two is easier to understand, if the ray and 3 slab intersecting segments are not coincident, then these segments can not be present in 3 slab, it is impossible in the Aabb box.
?? To facilitate understanding of the nature of the third, use 2D graphics to explain:

?? In, our rays are emitted in the lower right corner, to the top left, and the Rays pass through a point where the candidate faces are the Y1 and X2 faces.
?? According to the above-mentioned properties, a point can be seen at the same time in 2D space in the 2 slab, in addition, according to the nature of two, because the ray and the plane intersect, then this ray and the slab intersect part must have coincident parts, because a point on the ray, and in the plane, then can get Max (T1,T2) <=ta <=min (T3,T4); According to the nature of three: when the intersection, you can see T2>T1.
?? Similarly, we can extend the above verification process to three-dimensional. In three-dimensional space, assuming that the distance of the ray to 3 candidates is T1, T2, T3, the distance of the corresponding faces of the candidate faces is T4, T5, T6, then according to the nature two, the condition of the ray and AABB collision is Max (T1,T2,T3) <=min (T4,T5,T6); If a crossover occurs, the distance from the ray to the nearest intersection is Max (T1,T2,T3), depending on the nature of the three.

?? On the basis of the above properties, it is necessary to determine whether the ray and AABB intersect in three steps:

    1. How to determine the candidate faces: as long as the plane equation into Ray Ray's equation, the two plane of the T value, and then the lower T value of the natural first with the Ray intersection, then it is a candidate surface. The ray can be represented by the parametric equation as r (t) = P0 + t D (where P0 is the starting point of the Ray and D is the direction vector of the Ray)
    2. How to determine the equation of candidate faces. The plane is x n=d by an implicit definition of the equation (where x is the point on the plane, N is the plane normal vector, and D is the distance from the origin to the plane). Since the slab plane of AABB is parallel to two axes respectively, its face normals always have two components 0, and the other component is always 1, so we uniformly use a normal with an axis component of 1. If the above equation represents the left side of the Aabb box, then n in the formula means (1,0,0), but the above formula represents the right side of the Aabb box, the value of n is still (1,0,0).
    3. How to judge whether the intersection point is on the Aabb box. According to the nature of the second judgment, that is, the ray and AABB collision condition is Max (T1,T2,T3) <=min (T4,T5,T6).
Formula derivation of collision detection algorithm

?? The formula for calculating the T value is deduced as follows:

Collision detection Algorithm Python source code

Finally, I enclose my Python code snippet, which updates the code in real time on GitHub

# Ray-aabb Method intersection returns TRUE, otherwise false is returned# tdpoint = Collections.namedtuple ("Tdpoint", ["X", "Y", "Z"])# AABB has the largest and smallest points, the data structure is {Max=tdpoint,min=tdpoint}# Ray consists of the origin and direction, where the direction vector is 1 and the data structure is {Tdpoint,tdpoint}defIntersectwithaabb (Aabb,ray): tmin=0Tmax=10000    # <editor-fold desc= "parallel to X axis" >    if(Math.fabs (ray[1].x)<0.000001):if(ray[0].x<AABB.min. x)or(ray[0].x>AABB.Max. x):return  False    Else: Ood=1.0/ray[1].x T1=(AABB.min. x-ray[0].x)*Ood T2=(AABB.Max. x-ray[0].x)*Ood# T1 Do the candidate plane, T2 do the far plane        if(T1>T2): Temp=T1 T1=T2 T2=TempifT1>Tmin:tmin=T1ifT2<Tmax:tmax=T2ifTmin>Tmaxreturn False    # </editor-fold>    # <editor-fold desc= "parallel to Y axis" >    if(Math.fabs (ray[1].Y)<0.000001):if(ray[0].y<AABB.min. Y)or(ray[0].y>AABB.Max. Y):return  False    Else: Ood=1.0/ray[1].Y T1=(AABB.min. Y-ray[0].Y)*Ood T2=(AABB.Max. Y-ray[0].Y)*Ood# T1 Do the candidate plane, T2 do the far plane        if(T1>T2): Temp=T1 T1=T2 T2=TempifT1>Tmin:tmin=T1ifT2<Tmax:tmax=T2ifTmin>Tmaxreturn False    # </editor-fold>    # <editor-fold desc= "parallel to Z axis" >    if(Math.fabs (ray[1].Z)<0.000001):if(ray[0].z<AABB.min. z)or(ray[0].z>AABB.Max. Z):return  False    Else: Ood=1.0/ray[1].z T1=(AABB.min. z-ray[0].Z)*Ood T2=(AABB.Max. z-ray[0].Z)*Ood# T1 Do the candidate plane, T2 do the far plane        if(T1>T2): Temp=T1 T1=T2 T2=TempifT1>Tmin:tmin=T1ifT2<Tmax:tmax=T2ifTmin>Tmaxreturn False    # </editor-fold>    return True

Cross-detection algorithm for Ray and axial bounding box Aabb in from 3D space
Collision detection from box2d rays and AABB

Ray-aabb Cross 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.