Cocos2d-x V3 accurate physics body shape with PhysicsEditor
First of all, create a physics file with PhysicsEditor. Just drag image(s) to the left pane.
- Set the Exporter to
Chipmunk generic (PLIST) - BETA
- Set the Relative both values to
0.5to move the anchor point to the center
- Click on that icon
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).
Once you have done, the image is now highlighted with its shape.
Publish it as
.plist file. You have done the part.
Drag the published file to Xcode file pane and select
Create folder references.
It will looks like this.
Before actually use the physics file, let’s try with the default API. Edit HelloWorldScene.cpp
auto spriteBody = PhysicsBody::createBox(sprite->getContentSize());
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
Then in HelloWorldScene.cpp
Then finally you get
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.