Sprite Polygon PhysicsBody

Sprite Polygon PhysicsBody
0.0 0

#1

Hollo Community,
can some one help understand how create PhysicsBody from Polygon Sprite?
i`m try:

auto pinfo = AutoPolygon::generatePolygon( "Some link to sprite" );
spr = Sprite::create( pinfo );
auto body = PhysicsBody::createPolygon(<#const cocos2d::Vec2 *points#>, <#int count#>);

But i cant understand where i can get Vec2 points and int count?

The new Sprite have some info (verticals count, and triangle count) but not any properties with Vec2.

Thank you for help!


#2

This is a bit more complicated: AutoPolygon gives you a bunch of triangles - the PhysicsBody::createPolygon requires a convex polygon with clockwise winding… so these are 2 different things. The vertex count might even be limited. I think Box2d’s maximum count for 1 polygon is 8.

If you want to try this you’ll have to merge the triangles to form polygons. An option would be to start with one triangle and add more as long as the whole thing stays convex. If you can’t add any more triangles start a new polygon. Add all the polygons as PhysicsShapes to your physics body to form a compound object.

I would propose that you don’t follow this path because

  • Autopolygon is optimized for rendering - not for best fitting physics - that is a difference. A polygon traced with Autopolygon will always be bigger than the original sprite - Otherwise you would see rendering artifacts.
  • You have close to no control over the generated polygons
  • Tracing the shape in the app will increase your startup time
  • Triangle meshes and physics outlines are 2 different things

I would try some different approach: Generate the collision shapes offline. This gives you a bunch of advantages:

  • You can generate and tweak the polygons in a visual editor e.g. by using PhysicsEditor
  • Loading the prepares polygons is way faster
  • You can set additional parameters like mass etc
  • The solution is battle proven and works out of the box
  • See https://github.com/CodeAndWeb/PhysicsEditor-Cocos2d-x

#3

Hello,

have you get any success with this? I’m looking for the same option.
I want to create a PhysicsBody with just rendered Sprite object.
Is it possible with this engine?