Today, I saw an article in csdn, writing the collision detection between the rectangle and the circle, learning from the author's detection ideas, I used it in the cocos2d-x.
Original article address: Click to open the link
The collision detection between rectangle and the collision detection between point and rectangle are provided in the cocos2d-x, as follows:
Function used to detect the collision between a rectangle and a rectangle:
Bool intersectsRect (const CCRect & rect) usage: rect1.intersectsRect (rect2 );
Detection
Point and rectangleCollision function: bool containsPoint (const CCPoint & point) usage: rect. containsPoint (point );
Next we will focus on rectangular and circular Collision Detection: PS: First let's talk about the rectangular Rect in the cocos2d-x, The Rect structure has four parameters, x, y, width, height, as shown in
Create a coordinate system with the center of the circle as the origin. Collision Detection is divided into two situations: 1. How can the entire rectangle be located in the same quadrant to determine whether it is only in one quadrant? Check whether a pair of diagonal vertices are in the same quadrant. Then, you can determine whether the x and y coordinates of these two vertices are the same. If at least one of the four vertices is inside the circle, the rectangle and the circle will collide. Otherwise, no. 2. the rectangle is located in two or four quadrants (no case of three). At this time, it can detect the collision between the rectangle and the circle's outer Cut Square. Their detection results are the same as those of the rectangle and the circle.
The implementation is as follows and comments are necessary:
/// CGCircle. h // HelloCpp // Created by Mike on 14-1-16. /// # ifndef _ HelloCpp _ CGCircle __# define _ HelloCpp _ CGCircle __# include
# Include "cocos2d. h "using namespace std; using namespace cocos2d; class CGCircle {private: // float _ radius; // radius // Point _ position; // center public: CC_SYNTHESIZE (float, _ radius, Radius); CC_SYNTHESIZE (Point, _ position, Position); CGCircle (float radius, Point position): _ radius (radius), _ position (position ){}; bool isContainRect (Rect rect) ;};# endif/* defined (_ HelloCpp _ CGCircle __)*/
/// CGCircle. cpp // HelloCpp // Created by Mike on 14-1-16. //// # include "CGCircle. h "bool CGCircle: isContainRect (Rect rect) {bool _ isContian = false; Point orign = _ position; // center, create the origin of the coordinate system // The excircle square Rect (x, y, width, heigth) x, y is the origin, and the lower left vertex Rect square = Rect: Rect (_ position. x-_ radius, _ position. y-_ radius, 2 * _ radius, 2 * _ radius); do {Point rectVertex0 = rect. origin; // the lower left Point rectVertex1 = rect. origin + Point (rect. size. width, 0); // the lower right Point rectVertex2 = rect. origin + Point (rect. size. width, rect. size. height); // the top right Point rectVertex3 = rect. origin + Point (0, rect. size. height); // Point pos [] = {rectVertex0, rectVertex1, rectVertex2, rectVertex3}; if (pos [1]. x-orign. x) * (pos [3]. x-orign. x)> 0 & (pos [1]. y-orign. y) * (pos [3]. y-orign. y)> 0) {// the lower right vertex and the upper left vertex are in the same quadrant, it indicates that the entire rectangle is located in the same quadrant. if at least one vertex exists in the circle, it will intersection for (int I = 0; I <4; I ++) {// determine whether the four vertices of a rectangle have at least one if (pos [I]. x-orign. x) * (pos [I]. x-orign. x) + (pos [I]. y-orign. y) * (pos [I]. y-orign. y) <= _ radius * _ radius) {goto next ;}} if (square. intersectsRect (rect) {goto next;} for (int I = 0; I <4; I ++) {if (pos [I] = orign) {goto next ;}} break; next: _ isContian = true;} while (0); return _ isContian ;}
Test code:
Size visibleSize = Director: getInstance ()-> getVisibleSize (); auto circleS = Sprite: create ("circle.png"); circleS-> setPosition (Point (visibleSize. width/2, visibleSize. height/2); this-> addChild (circleS, 0); auto rectS = Sprite: create ("rect.png "); rectS-> setPosition (Point (circleS-> getPositionX ()-100, circleS-> getPositionY (); this-> addChild (rectS, 0 ); CGCircle circle0 = CGCircle (circleS-> getCont EntSize (). width/2, circleS-> getPosition (); Rect rect0 = Rect (rectS-> getPositionX ()-rectS-> getContentSize (). width/2, rectS-> getPositionY ()-rectS-> getContentSize (). height/2, rectS-> getContentSize (). width, rectS-> getContentSize (). height); if (circle0.isContainRect (rect0) {CCLOG ("circleS collided with rectS");} else CCLOG ("circleS not collided with rectS"); float radius = 30.0; point position = Point (300,300); CGCircl E circle = CGCircle (radius, position); Rect rect1 = Rect (280,290, 10, 30); if (circle. isContainRect (rect1) {CCLOG ("rectangle 1 collided with circle");} Rect rect2 = Rect (210,290, 10, 30); if (! Circle. isContainRect (rect2) {CCLOG ("rectangle 2 does not conflict with circle ");}
Test output:
Cocos2d: circleS and rectS do not collide cocos2d: the rectangle 1 and the circle collide cocos2d: the rectangle 2 and the circle are not collided
If you have any questions, please correct them ~