[Solved] Complex physic object with vertices - Cocos2d-X V3

Hi,

I want create a complex physic object using that vertices:

int num = 5;
verts[0].Set(-40.7f / PTM_RATIO, 83.8f / PTM_RATIO);
verts[1].Set(29.8f / PTM_RATIO, 83.4f / PTM_RATIO);
verts[2].Set(182.9f / PTM_RATIO, -88.7f / PTM_RATIO);
verts[3].Set(-184.6f / PTM_RATIO, -88.7f / PTM_RATIO);
verts[4].Set(-40.2f / PTM_RATIO, 83.7f / PTM_RATIO);

But how to do it with new PhysicObject from Cocos2d-X V3?

Thanks.

After searching a lot, i see that have a method “createPolygon”, but im trying using it without success:

My code:

       Point vers[] = {
            Point(40.4f / PTM_RATIO, 86.0f / PTM_RATIO),
            Point(-45.2f / PTM_RATIO, 86.5f / PTM_RATIO),
            Point(-183.8f / PTM_RATIO, -88.0f / PTM_RATIO),
            Point(182.5f / PTM_RATIO, -88.1f / PTM_RATIO),
            Point(39.8f / PTM_RATIO, 85.4f / PTM_RATIO)
        };
        
        auto enemyBody = PhysicsBody::createPolygon(vers, (sizeof(vers)/sizeof(vers[0])), PHYSICSSHAPE_MATERIAL_DEFAULT);

The error:

Aborting due to Chipmunk error: Polygon is concave or has a reversed winding. Consider using cpConvexHull() or CP_CONVEX_HULL().
	Failed condition: cpPolyValidate(verts, numVerts)

I do know the correct direction counter clock or inverse, but i tried the two directions, and get the same error on both.

Can anyone help me please?

Can anyone help me? It is a bug?

Answer is in error text: “Consider using cpConvexHull() or CP_CONVEX_HULL()”
What you are trying to do is create concave polygon which leads to error. You need to decompose object to convex polygons or create convex hull with cpConvexHull().

Im trying use it. But i dont know how!

Can you help me?

Well, i solve the problem understanding the problem :slight_smile:

Searching on web, i see it:


and

The problem with Physic Engine is that we cant use “CONCAVE” shape, one point cannot “enter” in the shape area. When you see the image, it is clear.

After change the points to make points only around the object, it works.

Im posting here to who dont understand it, like me :slight_smile: and help community.

Thanks.

Are you fixed? I have the same problem!

I solved it with inverse y value. I draw object in TileMap from bottom-left position.
If you won’t inverse, you can try draw object from top-right position.

  long count = platformVertex.size();
  Vec2 vertex[count];
  Vec2 prev = Vec2::ZERO;
  int i =0 ;
  for( Value pointVal : platformVertex)
  {
    ValueMap point = pointVal.asValueMap();
    CCLOG("pol x = %f y = %f", point["x"].asFloat(), point["y"].asFloat());
     prev = startPoint + Vec2(point["x"].asFloat(), point["y"].asFloat() * -1);
     vertex[i] = prev;
    i++;
  }  
  ValueMap lastPoint = platformVertex[0].asValueMap();
  vertex[count-1] = startPoint + Vec2(lastPoint["x"].asFloat(), lastPoint["y"].asFloat() * -1);
  Vec2 off= Vec2::ZERO;
  PhysicsBody* body = PhysicsBody::createPolygon(vertex, count, PhysicsMaterial(1.0, 0.0f, 1.0f), off);