Method to enable debugDraw when using the box2d physical engine in the cocos2d-x

Source: Internet
Author: User

Method to enable debugDraw when using the box2d physical engine in the cocos2d-x

Enabling debugDraw makes the world controlled by the physical engine visible. For example, if a rigid body is not bound to a certain genie, it is invisible; if you want to see its position and size so that you can directly observe the physical effects such as the collision between it and other elements, enable debugDraw to make its profile visible.

Online Search for some answers, but has not come out, the main is 1, the answer is corresponding to the old version of the Cocos2d-x, 2. Do not clarify the reference of some macros or header files. 3. The method is indeed wrong and may be outdated.

To use box2d, you must construct a "world", that is, a b2World object. To enable debugDraw, you must assign an additional value to the b2World object. The Code is as follows:

B2Vec2 gravity; gravity. set (0,-10); auto world = new b2World (gravity); world-> SetAllowSleeping (true); world-> SetContinuousPhysics (true ); m_world = new b2World (gravity); debugDraw = new GLESDebugDraw (PTM_RATIO); // create a new debug rendering module uint32 flags = 0; flags + = b2Draw: e_shapeBit; flags + = b2Draw: e_jointBit; flags + = b2Draw: e_centerOfMassBit; debugDraw-> SetFlags (flags ); // display those items m_world-> SetDebugDraw (debugDraw); // set
In the last sentence, copy m_debugDraw of m_world.

Here we will encounter the first pitfall. The member type is b2Draw, which is an abstract class that cannot be instantiated. The purpose of the system itself should be to let us inherit this class, build the shape of debugDraw by yourself. However, I am new to this issue. For example, how can I write the outline of an object in a physical engine on my own. Fortunately, Cocos2d-x source package with demo has been written in the class, you can directly use. It is named GLES-Render.h and GLES-Render.cpp. There is a class that inherits b2Draw, and the method for drawing the object outline has been written. Therefore, the debugDraw object

debugDraw = new GLESDebugDraw(PTM_RATIO);
.

This is initialization, so you still need to call the drawing method and draw it constantly. The method found on the Internet is to override the draw method of the current sence class, but the version I used (3.2) there is no draw method. From the Node class, finish (this keyword is used to modify the method in the source code. After reading the source code, replace it with "Rewrite:

void HelloWorld::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags){    GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION );        m_world->DrawDebugData();    CHECK_GL_ERROR_DEBUG();}

This method calls DrawDebugData ();

Basically, there is no problem. Another pitfall is the PTM_RATIO macro, which is the conversion ratio of the pixel units in the screen and the physical engine world. When building a debugDraw object, you need to include it in:

debugDraw = new GLESDebugDraw(PTM_RATIO);

I set the value based on the 32 I checked on the Internet, but I didn't build this value before, which led to a 32-fold reduction in the outline of the drawn object, and then became very small, so that I thought it was not successfully drawn, find the cause. Details are terrible!




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.