# Obb directional bounding box algorithm for collision detection of rotating rectangle

Source: Internet
Author: User

In the cocos2dx of the collision detection of the rectangle needs to do a collision check of the rotated rectangle, because the game does not use the box2d and other physical engine, so the obb (oriented bounding box) direction bounding box algorithm, the algorithm is based on the SAT ( Separating axis theorem) separates the Axis law.

Separation Axis law: Two convex polygon objects, if we can find an axis, so that two objects on the axis of the projection does not overlap, then there is no collision between the two objects, the axis is separating axis. that is, the projection of the two polygons on all axes overlaps, and the collision is determined; otherwise, no collisions occur.

Now, let's consider the rectangle, there are 4 sides of the rectangle, then there are 4 axes, because the rectangle is parallel to the edge, so there are two axes is repeated, we only need to check the adjacent two axes, then two rectangles need to check 4 axes.

There are two ways to check the projection: the first, the 4 vertices of each rectangle are projected onto an axis, so that the 4 vertices of the longest line distance, the same treatment of the second rectangle, and finally determine whether 2 rectangular projection distance overlap. The second, the radius of the 2 rectangular distance projection to the axis, and then the center of the 2 rectangular lines projected onto the axis, later to determine the center line projection of 2 rectangles, and 2 rectangular radius projection of the size of the sum.

Since there are many articles to introduce the principle of OBB, so here is not too much to explain, I only put my implementation of the source list for your reference only, the code has been tested, as follows:

`#ifndef _obbrect_h_#define_obbrect_h_#include<math.h>classObbrect { Public: Obbrect (floatXfloatYfloatWidthfloatHeightfloatrotation =0.0f): _x (x), _y (y), _width (width), _height (height), _rotation (rotation) {resetvector (); }    BOOLIntersects (obbrect&Other ) {        floatdistancevector[2] ={other._x-_x, Other._y-_y};  for(inti =0; I <2; ++i) {if(Getprojectionradius (_vectors[i]) +Other.getprojectionradius (_vectors[i])<=dot (distancevector, _vectors[i])) {                return false; }            if(Getprojectionradius (other._vectors[i]) +Other.getprojectionradius (other._vectors[i])<=dot (distancevector, other._vectors[i])) {                return false; }        }        return true; }Private:    voidResetvector () {_vectors[0][0] =cos (_rotation); _vectors[0][1] =sin (_rotation); _vectors[1][0] =-_vectors[0][1]; _vectors[1][1] = _vectors[0][0]; }    floatDotfloata[2],floatb[2]) {        returnABS (a[0] * b[0] + a[1] * b[1]); }    floatGetprojectionradius (floatvector[2]) {        return(_width * DOT (_vectors[0], vector)/2+ _height * DOT (_vectors[1], vector)/2); }    float_x; float_y; float_width; float_height; float_rotation; float_vectors[2][2];};#endif //_obbrect_h_`

Obb directional bounding box algorithm for collision detection of rotating rectangle

Related Keywords:

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.