用poly2tri库来完成任意多边形三角化,从而在游戏中转化成刚体和渲染图片

b2BodyDef TriangleBodyDef;
	TriangleBodyDef.type = b2_dynamicBody;
	TriangleBodyDef.position.Set(0,0);

	b2Body *Triangle = world->CreateBody(&TriangleBodyDef); 
	b2FixtureDef sd2;
	sd2.density = 2.0f;
	sd2.restitution = 0.2;

	p2t::CDT cdt(polyes);
	cdt.Triaresultingngulate();
	//get the  triangles
	vector<p2t::Triangle*> tris = cdt.GetTriangles();
	for (size_t t = 0; t < tris.size(); t++) {
		p2t::Triangle* tri = tris[t];
		b2Vec2 vertices[3];
		b2PolygonShape TriangleShapeDef;
		for (int i = 0; i < 3; i++) {
			vertices[i].x = tri->GetPoint(i)->x/PTM_RATIO;
			vertices[i].y = tri->GetPoint(i)->y/PTM_RATIO;
		}
		TriangleShapeDef.Set(vertices, 3);
		sd2.shape = &TriangleShapeDef;
		Triangle->CreateFixture(&sd2);
	}
	Triangle->SetUserData(filledPolygon);

开源库下载地址:https://code.google.com/p/poly2tri/

vector<p2t::Point *> HelloWorld::tools_toPoint(vector<CCPoint> temp)
{
	vector<p2t::Point *> p2tPoints;
	for (int i = 0; i < temp.size(); i++)
	{
		p2tPoints.push_back(new p2t::Point(temp[i].x, temp[i].y));
	}
	return p2tPoints;
}



用poly2tri库来完成任意多边形三角化,从而在游戏中转化成刚体和渲染图片_第1张图片

 
 
 
 
 
 
 
 
 
 
 
 
 
 
vector<p2t::Point *> HelloWorld::tools_toPoint(vector<CCPoint> temp)
{
	vector<p2t::Point *> p2tPoints;
	for (int i = 0; i < temp.size(); i++)
	{
		p2tPoints.push_back(new p2t::Point(temp[i].x, temp[i].y));
	}
	return p2tPoints;
}


你可能感兴趣的:(cocos2d-x,box2D,任意多边形三角化)