How to accelerate cocos2dx-Box2D development with physicseditor

Source: Internet
Author: User

Attachment: Download vs2010 Project

Link: http://blog.csdn.net/zhangxaochen/article/details/8018489

Here: http://is.gd/VoBVUJ

Physicseditor is a good box2d shape generation tool. It generates a shape plist file by clicking the mouse on the GUI and then uses its API to generate fixtures and add it to the desired body.

Software:

Http://www.codeandweb.com/physicseditor

1. download and install the SDK in a directory similar to the following:

D: \ Program Files \ physicseditor \ examples \ Cocos2d-X \ demo

See examples. However, it is an xcode project. If you use Windows + Visual Studio like me, you need to create a new project and put a directory similar:

D: \ Program Files \ physicseditor \ examples \ Cocos2d-X \ demo \ Classes

Under the helloworldscene. cpp + helloworldscene. h two files copied out, replacing the new cocos2d-x Project 【Note: It is not applicable to cocos2dx versions earlier than 2.0.]

2. In the path:

D: \ Program Files \ physicseditor \ examples \ Cocos2d-X \ demo \ generic-box2d-plist, there are two other files required:

GB2ShapeCache-x.cpp + GB2ShapeCache-x.h, copy them to the classes directory of the new project. If you want to keep only the. h and. cpp files, add the corresponding path to the additional inclusion directory of the project and add the reference of the CPP file to the project.

Generate a running program, click the screen, and continuously add fruit in the screen area. Like this:

========================================================== ========================================================== ============

So how to use this plist file in the code?

1. In the constructor helloworld:

    // load physics shapes    GB2ShapeCache::sharedGB2ShapeCache()->addShapesWithFile("shapedefs.plist");

(As if everyone liked the singleton mode ...)

2. In the member function void helloworld: addnewspritewithcoords (ccpoint P:

void HelloWorld::addNewSpriteWithCoords(CCPoint p){    string name = names[rand()%7];        CCSprite *sprite = CCSprite::spriteWithFile((name+".png").c_str());    sprite->setPosition(p);    addChild(sprite);    b2BodyDef bodyDef;bodyDef.type = b2_dynamicBody;    bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);bodyDef.userData = sprite;b2Body *body = world->CreateBody(&bodyDef);        // add the fixture definitions to the body        GB2ShapeCache *sc = GB2ShapeCache::sharedGB2ShapeCache();    sc->addFixturesToBody(body, name.c_str());     sprite->setAnchorPoint(sc->anchorPointForShape(name.c_str()));}

Create a genie first, and then use b2bodydef to create a body. At this time, the body has no shape. Previously, we assigned a fixture to the body as follows:

b2FixtureDef carShapeDef;carShapeDef.shape=&carShape;carShapeDef.density=10.f;carShapeDef.restitution=0.9f;//carShapeDef.isSensor=true;_carBody->CreateFixture(&carShapeDef);

First create a b2fixturedef, give the shape field a shape, and then xxbody-> createfixture...

This time, we used gb2shapecache:

    GB2ShapeCache *sc = GB2ShapeCache::sharedGB2ShapeCache();    sc->addFixturesToBody(body, name.c_str()); 

Note that there is still a setanchorpoint action in the sample code. In fact, there is no anchorpoint concept in box2d. This SC-> anchorpointforshape (name. c_str () is a tag attached to physicseditor, because if you use physicseditor, the generated coordinate value is calculated as () in the lower left corner of the image by default, as shown in the following figure:


This is equivalent to running anchorpoint (0, 0). If sprite does not reset anchorpoint, the collision detection result is inconsistent with the picture displayed, or debugdraw can directly see that the shape and sprite do not overlap:

Of course, you can also drag "anchorpoint" to the center in physicseditor so that you don't always have to remember to set the anchorpoint of Sprite...

 

Link: http://blog.csdn.net/zhangxaochen/article/details/8018489

{Over }}

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.