BOX2D circle surrounding boundary of cocos2d-x Practical Effect

Source: Internet
Author: User

BOX2D circle surrounding boundary of cocos2d-x Practical Effect

This article is based on the cocos2d-x2.2.3, not written in Versions later than 3.0, just provide a way of thinking, to facilitate other scenarios in the future to use this function can be referred, the basic knowledge of the box2d physical engine is not detailed. For more information, see other technical documents.

To use the box2d physical engine, we usually need to specify a boundary to limit the body's activity range. The most common thing is to use four sides to enclose the boundary of the screen to form a rectangle. But what should we do if we want to determine the range through a center point and a radius within a circular range? In fact, an arc can also be seen as composed of numerous small line segments. When the number of line segments is large enough, it infinitely approaches the circle. Therefore, we can take a certain number of points on the circumference of a range, A circular area is formed by concatenating the points. What we need to do is to determine the coordinates of these points. The first and end links can be connected to an area with an approximate circle. Of course, the more points, the more computation, the lower the performance, so we can take appropriate measures, in this example, 24 points are taken.

In this way, when we create the physical world initialization, We can enclose the point data in a circular box as follows:

// Set the gravity parameter b2Vec2 gravity; gravity. set (0.0f, 0.0f); // create the world m_world = new b2World (gravity); // whether to allow sleep m_world-> SetAllowSleeping (true ); // whether to perform consecutive physical tests m_world-> SetContinuousPhysics (true); // The Ground Object defines b2BodyDef groundBodyDef; groundBodyDef. position. set (300); groundBody = m_world-> CreateBody (& groundBodyDef); // defines the number of points float pointNum = 24; // defines the radius of the Circular Boundary =; // define the center m_center = ccp (640,400); // The CCPoint point array for storing the point data [24]; for (int I = 0; I <pointNum; I ++) {// calculate the angle, coordinate (first 0 °) float angle = (float) I/pointNum * PI * 2 of the starting point of each line segment connected to two points; float bx = radius * cos (angle); float by = radius * sin (angle); // the offset of the center plus the offset is the current 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 the 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 );}

 


The effect is as follows:

 

 

The above is just the main implementation code segment, which is for reference only. You can also download the code and run it on your own. Adjust the parameters as needed and mark it yourself, so that you do not need to find it later !! The code and resources are listed below. Create your own project and run it.

 

 

Related Article

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.