Circular bounding boundary of the box2d of cocos2d-x practical effect

Source: Internet
Author: User

This article is based on cocos2d-x2.2.3, not written in the version after 3.0, simply provides a way to use this feature point in the future, and does not give a detailed account of the basic knowledge of box2d physics engine, please refer to other technical documents by yourself.

Using the box2d physics engine, we usually need to give ourselves a boundary to limit the body's range of activity, most commonly using four edges around the edge of the screen to form a rectangle. But what if our needs are in a circular range, and just want to determine the range by a center point and radius? In fact, the arc can also be seen as an infinite number of small line segments, when there are enough segments to infinity toward the circle, so we can take a certain number of points in the circumference of the range to be taken, the various points to connect the scope of the surrounding, it can be regarded as a circular area. What we need to do is to determine the coordinates of these points, the end-to-end connection can be surrounded by an approximate circle of the area, of course, the more the more round, the more the calculation, the lower the performance, so take appropriate, this example takes 24 points.

Thus, when we create the physical world initialization, we can enclose each point data in a circular bounding box, as follows:

       <span style= "Font-family:comic Sans MS;" >//Set gravity parameter B2vec2 gravity;gravity. Set (0.0f,0.0f);//create World M_world = new B2world (gravity);//whether to allow hibernation m_world->setallowsleeping (true);//Whether continuous physical testing m_world- >setcontinuousphysics (TRUE);//Ground Object Definition B2bodydef groundbodydef;groundbodydef.position.set (0,0); groundBody = m_ World->createbody (&groundbodydef);//define the number of points float Pointnum = 24;//Defines the radius of the circle boundary radius = 300;//defines the center of the Circle M_center = CCP ( 640,400);//array of data ccpoint point[24];for (int i= 0; i < Pointnum; i++) {//calculates the angle, coordinates (first 0°) of the starting point of the line segments connected to each two points, and (f) float angle =  loat) i/pointnum *pi*2;float bx = radius * cos (angle); float by = radius * sin (angle);//center plus offset is the current point coordinate bx + = M_center.x;by + = M_center.y;point[i].setpoint (Bx,by);} Point[23].setpoint (POINT[0].X,POINT[0].Y); for (int i= 0; i < pointNum-1; i++) {//Set Edge range Groundbox.set (B2VEC2 (point[i ].x/ptm_ratio,point[i].y/ptm_ratio), B2VEC2 (point[i+1].x/ptm_ratio,point[i+1].y/ptm_ratio));groundBody-> Createfixture (&groundbox,100);} </span>



The effect is as follows:



above just give the main implementation of the code snippet, for reference only, you can also download the code to run the sample, according to the need to adjust the parameters, their mark, lest the need for future time to find out!! Code and resources below, please create your own project run


Click to download the reference source code

Circular bounding boundary of the box2d of cocos2d-x practical effect

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.