Cocos2dx BASICS (27) -- simple Collision Detection

Source: Internet
Author: User

[Nagging]

This section describes simple physical collision detection (non-box2d physical collision): collision detection between Rectangles and circles.



[Simple collision detection]

In some games, collision detection often occurs, such as whether the angry birds collide with the stones after they fly out.

Although there is a box2d physical collision engine, you still need to master the simple collision detection method here.

(1) rectangle and rectangle

(2) circle and circle

(3) rectangle and circle


1. rectangle and rectangle

1.1 raise questions

Problem:Assume there are two rectangles, rect1 and rect2, and determine whether the two moments are overlapped (some areas overlap ).

As shown in the following four figures, images 1, 2, and 4 collide, while Figure 3 does not.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/48/27/wKiom1QFwuCS0a7IAAAnXuLCBYM543.jpg "style =" float: none; "Title =" 1.jpg" alt = "wkiom1jwwucs0a7iaaanxulcbym543.jpg"/> 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/29/wKioL1QFwuKQ7lK2AAAxXV3oZ_k652.jpg "Title =" 2.jpg" style = "float: none;" alt = "wkiol1jwwukq7lk2aaaxxv3oz_k652.jpg"/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/27/wKiom1QFwuCBbwtqAAAkm2811VU152.jpg "Title =" 3.jpg" style = "font-size: 16px; float: none;" alt = "wkiom1jwwucbbwtqaaakm2811vu152.jpg"/> 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/2C/wKioL1QF0Ojz-1qnAAAh5ghkGt8517.jpg "Title =" 4.jpg" style = "font-size: 16px;" alt = "wKioL1QF0Ojz-1qnAAAh5ghkGt8517.jpg"/>


1.2. Solution

The figure shows whether the intersection of two rectangles can be a small rectangle.

The method is as follows:(It can be used to calculate the small rectangle information of the intersection part)

// Bool collision_rectwithrect (ccrect rect1, ccrect rect2) {// calculates the coordinates of the rectangle in the intersection part/the lower left corner: (LX, ly) // coordinates in the upper right corner: (RX, ry) float Lx = max (rect1.getminx (), rect2.getminx (); float Ly = max (rect1.getminy (), rect2.getminy (); float RX = min (rect1.getmaxx (), rect2.getmaxx (); float ry = min (rect1.getmaxy (), rect2.getmaxy (); // determines whether a small rectangle can be formed if (LX> RX | LY> ry) return false; // The rectangle does not intersection else return true; // a collision occurs }//


Of course, you can also use the ccrect class in the cocos2dx engine to determine the existence of a function for rectangular collision.

//// Return bool. The intersection is truerect1.intersectsrect (rect2); // the source code of the intersectsrect () function is as follows: bool ccrect: intersectsrect (const ccrect & rect) const {return! (Getmaxx () <rect. getminx () | rect. getmaxx () <getminx () | getmaxy () <rect. getminy () | rect. getmaxy () <getminy ());}//


2. Circle and circle

2.1 raise questions

Problem:Assume there are two circles circle1 and circle2, and determine whether the two circles collide with each other (some areas overlap ).

As shown in the following three figures, images 1 and 2 collide, while Figure 3 does not.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/48/2C/wKioL1QF06Xgh6yhAABYDrSfp5g151.jpg "style =" float: none; "Title =" 11.jpg" alt = "wkiol142506xgh6yhaabydrsfp5g151.jpg"/> 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/48/2B/wKiom1QF06OQN5SrAABV2uYrcxg422.jpg "Title =" 22.jpg" style = "font-size: 16px; float: none;" alt = "wkiom142506oqn5sraabv2uyrcxg422.jpg"/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/2C/wKioL1QF06Wzz1RTAABSejZG4UM796.jpg "style =" float: none; "Title =" 33.jpg" alt = "wkiol142506wzz1rtaabsejzg4um796.jpg"/>


2.2. Solution

Round collision detection is relatively simple, as long as you determine whether the distance between the two centers is less than the radius of the sum (R1 + R2.

The method is as follows:

// Bool collision_circlewithcircle (ccpoint P1, float R1, ccpoint P2, float R2) {// calculates the distance from the center of the circle to float Dist = p1.getdistance (P2 ); // determine whether the two circles intersection return Dist <(R1 + R2 );}//


3. rectangle and circle

3.1 raise questions

Problem:Suppose there is a rectangle rect, circle, to determine whether the rectangle and the circle collide with each other (some areas overlap ).

As shown in the following four figures, images 1, 2, and 4 collide, while Figure 3 does not.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/48/2E/wKioL1QF73nAZKVTAAA6sBYX2Rs665.jpg "style =" float: none; "Title =" 1111.jpg" alt = "wkiol142573nazkvtaaa6sbyx2rs665.jpg"/> 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/48/2C/wKiom1QF73eQvB-bAAA4x3hbNkA813.jpg "Title =" 2222.jpg" style = "font-size: 16px; float: none;" alt = "wKiom1QF73eQvB-bAAA4x3hbNkA813.jpg"/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/48/2E/wKioL1QF73miRhBnAAAxMVKieIM009.jpg "style =" float: none; "Title =" 3333.jpg" alt = "wkiol142573mirhbnaaaxmvkieim009.jpg"/> 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/48/2C/wKiom1QF73iyAqc2AABSjhSEcrE580.jpg "Title =" 4444.jpg" style = "font-size: 16px; float: none;" alt = "wkiom142573iyaqc2aabsjhsecre580.jpg"/>

3.2. Solution

The judgment of rectangle and circle is complicated. See the following analysis.

(1) first, let the circle roll along the side of the rectangle outside the rectangle, and then link the trajectory of the center to get a rounded rectangle.

(2) If the red area is a rounded rectangle, we only need to determine whether the center is inside the rounded rectangle.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/2E/wKioL1QF9iSgsP6CAADe5iZ6hoA754.jpg "Title =" 888.jpg" alt = "wkiol14259isgsp6caade5iz6hoa754.jpg"/>

(3) If the four 1/4-circle parts on the four corners of the rounded rectangle are removed, you can only determine whether the center of the circle falls in the remaining area. Should you be able to quickly come up with a solution?

You only need to determine whether the center is inside any of the two rectangles.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/2E/wKioL1QF-Q3BIL1ZAACBVOfs2_c233.jpg "Title =" 6.jpg" alt = "wKioL1QF-Q3BIL1ZAACBVOfs2_c233.jpg"/>

(4) then judge whether the center of the circle is in the area of the 1/4 circle on the four corners.

Obviously, you only need to determine whether the distance between the center and the four vertices of the rectangle is smaller than the radius of the circle.

(5) Appeal: (3) (4) Determine whether the circle is rectangular.


The method is as follows:

// Bool collision_rectwithcircle (ccrect rect, ccpoint P, float R) {// obtain the rectangle information // coordinate in the lower left corner: (LX, ly) // coordinate in the upper right corner: (RX, ry) float Lx = rect. getminx (); float Ly = rect. getminy (); float RX = rect. getmaxx (); float ry = rect. getmaxy (); // calculate the float d1 = P. getdistance (CCP (LX, ly); float D2 = P. getdistance (CCP (LX, ry); float D3 = P. getdistance (CCP (RX, ly); float D4 = P. getdistance (CCP (RX, ry )); // determine whether a collision exists. // determine whether the distance is smaller than the radius. If (d1 <r | D2 <r | D3 <r | D4 <r) return true; // whether it is in the rounded rectangle or in the horizontal rectangle if (P. x> (Lx-R) & P. x <(RX + r) & P. y> LY & P. Y <ry) return true; // whether it is in the rounded rectangle, if (P. x> lx & P. x <RX & P. y> (ly-R) & P. Y <(RY + r) return true; // return false without collision ;}//



This article is from the "summer wind" blog, please be sure to keep this source http://shahdza.blog.51cto.com/2410787/1548097

Cocos2dx BASICS (27) -- simple Collision Detection

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.