I mention about TexturePacker in my previous post.
Now is PhysicsEditor.

PhysicsEditor

Create a physics file

First of all, create a physics file with PhysicsEditor. Just drag image(s) to the left pane.

Drag images to PhysicsEditor

Traverse body shape

  1. Set the Exporter to Chipmunk generic (PLIST) - BETA
  2. Set the Relative both values to 0.5 to move the anchor point to the center
  3. Click on that icon

Adjust tolerance

You will see the face is highlighted, PhysicsEditor helps you to generate vertices/points of the image.
You can change the tolerance value (by default is 1), of course, the lower the better, but will be slower (theoretically).

Image highlighted

Once you have done, the image is now highlighted with its shape.

Publish physics

Publish it as .plist file. You have done the part.

Add the physics file to Xcode project

Add physics to Xcode

Drag the published file to Xcode file pane and select Create folder references.

File inspector

It will looks like this.

Test it out in code

Before actually use the physics file, let’s try with the default API. Edit HelloWorldScene.cpp

1
2
auto spriteBody = PhysicsBody::createBox(sprite->getContentSize());
sprite->setPhysicsBody(spriteBody);

Rectangle shape

It is rectangle.

Now, add PEShapeCache_X3_0.h and PEShapeCache_X3_0.cpp to project (credit: https://github.com/baibai2013/PhysicsEditor-Loader-for-cocos2d-x-3.0).

Update AppDelegate.cpp, add the physics file you generated just now to cache

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "AppDelegate.h"
#include "HelloWorldScene.h"
#include "PEShapeCache_X3_0.h" // Make sure you include it
// ...
bool AppDelegate::applicationDidFinishLaunching() {
// ...
// after if (!glview) { ... }
PEShapeCache::getInstance()->addBodysWithFile("physics/body.plist");
// ...
}

Then in HelloWorldScene.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "HelloWorldScene.h"
#include "PEShapeCache_X3_0.h"
// ...
bool HelloWorld::init()
{
// ...
auto spriteBody = PEShapeCache::getInstance()->getPhysicsBodyByName("2dx"); // the name you put in PhysicsEditor
sprite->setPhysicsBody(spriteBody);
}

Then finally you get

Custom shape

Although it is not perfect yet, but it basically solve the issue that I faced. Will continue to seek for solution to make it perfect :)

I created a demo project, please refer to Cocos2d-x-PhysicsEditor-demo.

References: