AABB (axial parallel box surrounded by aixe align bounding box) in 3D space

Source: Internet
Author: User
Introduction

In the previous article, we talked about how to obtain the model's ball through the model's vertices, and also about the AABB box besides the ball. This chapter describes how to obtain its AABB Box Based on the coordinates of the model.




Representation Method

There are many Representation Methods in the AABB box, which are summarized as follows:

Max-min Notation: Use a point in the upper right corner and the lower left corner to uniquely define an surround body.

Center-radious representation: center points are used to represent the midpoint. radious is an array that stores the radius of the box in the X, Y, and Z directions.

Min-width: We use min to define the point in the lower left corner, and use width to save the length in the X, Y, and Z directions.

Different methods have different collision detection algorithms, and different representation methods are also applicable in different situations. Therefore, you need to consider carefully when designing your own applications.

In this article, we will use the max-min representation method, as shown below:

class AABB{   ....public:    VECTOR3 max ;    VECTOR3 min ;};
The code for collision detection is as follows:

bool AABB::isCollided(AABB* a){if(max.x < a->min.x || min.x > a->max.x) return false ;if(max.y < a->min.y || min.y > a->max.y) return false ;if(max.z < a->min.z || min.z > a->min.z) return false ;return true ;}// end for isCollided



AABB Box Structure

There are many methods to construct the AABB box, some of which are very simple and some are very complicated. Here we will introduce two basic construction methods. They are also very simple and easy to master.

The first is a fixed-size AABB box. After the AABB box is constructed, no matter how the surrounding object rotates, it does not need to be re-constructed.

The second is a compact one. An AABB box is constructed using the X, Y, and Z axes at the longest and farthest points up and down.



Fixed size AABB box

As mentioned above, a fixed-size AABB box needs to be surrounded by objects that are still in the surrounding body no matter how they are rotated. Therefore, we first construct a sphere for this object, and then construct an AABB Box Based on this sphere. In this way, no matter how you rotate it, it is still in the surrounding body.

However, building a ball for this object can also meet these requirements. However, if you cannot use the ball to enclose the ball under certain restrictions, you can use this method to build a fixed size AABB box.

The core of this algorithm is how to build a sphere, which I have already described in the previous chapter and will not be repeated, interested readers can go to the blog's article about how to find a 3D sphere.

After the ball is surrounded, we can use the following method to calculate the fixed size AABB box:

void AABB::computeFixedAABB(Sphere *s){max.x = s->center.x + s->radious ;max.y = s->center.y + s->radious ;max.z = s->center.z + s->radious ;min.x = s->center.x - s->radious ;min.y = s->center.y - s->radious ;min.z = s->center.z - s->radious ;}// end for computeFixedAABB



Compact point AABB box

The AABB box construction method is to obtain the farthest and closest points in the X, Y, and Z directions from the vertex set, and then use them to construct an AABB box. This method is also very simple. I will directly explain the code to you:

void AABB::computeAABBFromOriginalPointSet(VECTOR3* vertices, unsigned int vertex_num){unsigned int minX_i = 0 , maxX_i = 0 ;extrameDistanceAlongDir(MAKE_VECTOR3(1,0,0), vertices, vertex_num, &minX_i, &maxX_i);min.x = vertices[minX_i].x ;max.x = vertices[maxX_i].x ;unsigned int minY_i = 0 , maxY_i = 0;extrameDistanceAlongDir(MAKE_VECTOR3(0,1,0),vertices, vertex_num, &minY_i, &maxY_i);min.y = vertices[minY_i].y ;max.y = vertices[maxY_i].y ;unsigned int minZ_i = 0 , maxZ_i = 0;extrameDistanceAlongDir(MAKE_VECTOR3(0,0,1),vertices, vertex_num, &minZ_i, &maxZ_i);min.z = vertices[minZ_i].z ;max.z = vertices[maxZ_i].z ;}// end for computeAABBFromOriginalPointSetvoid AABB::extrameDistanceAlongDir(VECTOR3 dir, VECTOR3* vertices, unsigned int vertex_num, unsigned int* min, unsigned int*max){float maxProj = FLT_MIN , minProj = FLT_MAX ;for(unsigned int i = 0 ; i < vertex_num ; i ++){float proj = 0 ;Vec3Dot(proj, vertices[i], dir);if(proj > maxProj){maxProj = proj ;*max = i ;}if(proj < minProj){minProj = proj ;*min = i ;}}// end for}// end for extrameDistanceAlongDir

There are two functions above. The first function is the AABB box calculation method called for the user. You only need to pass in the model's vertex set.

The second function is used to obtain the longest and shortest projection point on the specified axis. This function is very simple. You only need to call a dot operation to obtain it.

After the longest and shortest points are projected on the X, Y, and Z axes, we take the coordinates of the corresponding axes to form Max and min respectively, the AABB box is constructed. Is it easy ??




AABB type

The complete class of AABB is as follows:

//--------------------------------------------------------------------------------------------------// declaration: Copyright (c), by XJ , 2014 . All right reserved .// brief: This file will define the Axie aligned bounding box.// author: XJ// date: 2014 / 6 / 22// file: AABB.h// version: 1.0//---------------------------------------------------------------------------------------------------#pragma once#include"XJMath.h"#include"Sphere.h"namespace XJCollision{/*** brief: We use the Max-min representation*/class AABB{public:AABB();AABB(VECTOR3 min, VECTOR3 max);public:/*** Compute the fixed AABB*/void computeFixedAABB(Sphere* s);/*** Compute the AABB from the original point set*/void computeAABBFromOriginalPointSet(VECTOR3 *vertices, unsigned int vertex_num);/*** Collision Detection between two AABB*/bool isCollided(AABB* a);private:/*** Compute the least and most distance along the specific direction*/void extrameDistanceAlongDir(VECTOR3 dir, VECTOR3 * vertices, unsigned int vertex_num, unsigned int * min, unsigned int * max);public:VECTOR3 max ;VECTOR3 min ;};};

#include"AABB.h"#include<cmath>#include<float.h>using namespace XJCollision ;AABB::AABB():max(),min(){}AABB::AABB(VECTOR3 _max, VECTOR3 _min){max.x = _max.x ; max.y = _max.y ; max.z = _max.z ;min.x = _min.x ; min.y = _min.y ; min.z = _min.z ;}void AABB::computeFixedAABB(Sphere *s){max.x = s->center.x + s->radious ;max.y = s->center.y + s->radious ;max.z = s->center.z + s->radious ;min.x = s->center.x - s->radious ;min.y = s->center.y - s->radious ;min.z = s->center.z - s->radious ;}// end for computeFixedAABBvoid AABB::computeAABBFromOriginalPointSet(VECTOR3* vertices, unsigned int vertex_num){unsigned int minX_i = 0 , maxX_i = 0 ;extrameDistanceAlongDir(MAKE_VECTOR3(1,0,0), vertices, vertex_num, &minX_i, &maxX_i);min.x = vertices[minX_i].x ;max.x = vertices[maxX_i].x ;unsigned int minY_i = 0 , maxY_i = 0;extrameDistanceAlongDir(MAKE_VECTOR3(0,1,0),vertices, vertex_num, &minY_i, &maxY_i);min.y = vertices[minY_i].y ;max.y = vertices[maxY_i].y ;unsigned int minZ_i = 0 , maxZ_i = 0;extrameDistanceAlongDir(MAKE_VECTOR3(0,0,1),vertices, vertex_num, &minZ_i, &maxZ_i);min.z = vertices[minZ_i].z ;max.z = vertices[maxZ_i].z ;}// end for computeAABBFromOriginalPointSetvoid AABB::extrameDistanceAlongDir(VECTOR3 dir, VECTOR3* vertices, unsigned int vertex_num, unsigned int* min, unsigned int*max){float maxProj = FLT_MIN , minProj = FLT_MAX ;for(unsigned int i = 0 ; i < vertex_num ; i ++){float proj = 0 ;Vec3Dot(proj, vertices[i], dir);if(proj > maxProj){maxProj = proj ;*max = i ;}if(proj < minProj){minProj = proj ;*min = i ;}}// end for}// end for extrameDistanceAlongDirbool AABB::isCollided(AABB* a){if(max.x < a->min.x || min.x > a->max.x) return false ;if(max.y < a->min.y || min.y > a->max.y) return false ;if(max.z < a->min.z || min.z > a->min.z) return false ;return true ;}// end for isCollided

Program instance

The following two types of graphs are the boxes calculated using the first and second calculation methods:

 

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.