Cocos2d-x V3 accurate physics body shape with PhysicsEditor
I mention about TexturePacker in my previous post.
Now is PhysicsEditor.
Create a physics file
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.5
to 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.
Add the physics file to Xcode project
Drag the published file to Xcode file pane and select Create folder references
.
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 | 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
1 |
|
Then in HelloWorldScene.cpp
1 |
|
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.
References: