Viktor
Viktor

Reputation: 1225

How to draw indented(concave) solid polygon in cocos2dx

I need to draw indented(concave) solid polygon with some vertices. I use

void HelloWorld::draw(void)
{ 
    CCPoint vertices[5] = {ccp(200, 200), ccp(400, 400), ccp(200, 600), ccp(500, 600), ccp(500, 200)};
    ccDrawSolidPoly(vertices, 5, ccc4f(0.7f, 0.7f, 0.7f, 0.5f));
}

And get Rectangle with triangle inside. But I expect indented(concave) solid polygon as in the picture

obtained and expected

Upvotes: 2

Views: 3011

Answers (2)

Fedor  Petrov
Fedor Petrov

Reputation: 1050

Without modifications to Cocos2d engine you can achive the polygon of the desired form either by starting your array from ccp(400,400) or by adding one more point ccp(500,400)! And then your array of points should start from this point like in the picture I attach.

The reason for this is that cocos2d uses by default GL_TRIANGLE_FAN flag while drawing complex polygons. This means that all the points in the triangles in your polygon will be built relative to the first point in the array of points.

You can go to CCDrawNode.cpp file and replace this flag by GL_TRIANGLE_SPLIT. To know more just google these two flags.

Upvotes: 3

Richard Salim
Richard Salim

Reputation: 129

Try using drawPolygon function in CCDrawNode

void drawPolygon(CCPoint* verts, unsigned int count, const ccColor4F &fillColor,
                         float  borderWidth, const ccColor4F& borderColor)

here is an example

CCPoint vertices[5] = {ccp(200, 200), ccp(400, 400), ccp(200, 600), ccp(500, 600), ccp(500, 200)};
CCDrawNode* polygon = CCDrawNode::create();
//creating red polygon with thin black border
polygon->drawPolygon(vertices, 5, ccc4f(1, 0, 0, 1), 1, ccc4f(0, 0, 0, 1));
addChild(polygon);

I hope it works

Upvotes: 3

Related Questions