PhysicsEditor是一款box2d形狀生成器,只需點點鼠標即可生成plist圖形文件,還有很有特色的魔術棒工具,可自動完成標記定點和分隔polygon的過程。可以直接在cocos2dx中使用。
如圖所示:可以對邊緣進行不規則圖形的選取
官方提供了Cocos2dx的第三方庫,但是採用的是2.x的接口,3.x下面依然是可以使用的,不過在這添加了採用3.x接口封裝的方法
void GB2ShapeCache::addShapesWithV3(const std::string &plist) {
//const char *fullName = CCFileUtils::sharedFileUtils()->fullPathForFilename(plist.c_str()).c_str();
auto dict = FileUtils::getInstance()->getValueMapFromFile(plist);
//基礎屬性
auto metadataDict = dict["metadata"].asValueMap();
auto format = metadataDict["format"].asInt();
ptmRatio = metadataDict["ptm_ratio"].asFloat();
//body屬性]
b2Vec2 vertices[b2_maxPolygonVertices];
std::string bodyName;
auto bodyDict = dict["bodies"].asValueMap();
for (auto obj : bodyDict) {
bodyName = obj.first;
auto bodyData = bodyDict[bodyName].asValueMap();
BodyDef *bodyDef = new BodyDef();
bodyDef->anchorPoint = PointFromString(bodyData["anchorpoint"].asString());
auto fixtureList = bodyData["fixtures"].asValueVector();
FixtureDef **nextFixtureDef = &(bodyDef->fixtures);
for (auto fixture : fixtureList) {
b2FixtureDef basicData;
ValueMap vecmap = fixture.asValueMap();
basicData.density = vecmap["density"].asFloat();
basicData.friction = vecmap["friction"].asFloat();
basicData.restitution = vecmap["restitution"].asFloat();
basicData.filter.categoryBits = vecmap["filter_categoryBits"].asInt();
basicData.filter.groupIndex = vecmap["filter_groupIndex"].asInt();
basicData.filter.maskBits = vecmap["filter_maskBits"].asFloat();
basicData.isSensor = vecmap["isSensor"].asBool();
std::string cb = vecmap["userdataCbValue"].asString();
int callbackData = 0;
if (cb.compare("") == 0) {
callbackData = std::atoi(cb.c_str());
}
std::string fixtureType = vecmap["fixture_type"].asString();
if (fixtureType == "POLYGON") {
auto vec2Aarry = vecmap["polygons"].asValueVector();
for (auto vec : vec2Aarry) {
FixtureDef *fix = new FixtureDef();
fix->fixture = basicData; // copy basic data
fix->callbackData = callbackData;
b2PolygonShape *polyshape = new b2PolygonShape();
int vindex = 0;
auto posArray = vec.asValueVector();
for (auto pos : posArray) {
Vec2 offset = PointFromString(pos.asString());
vertices[vindex].x = (offset.x / ptmRatio);
vertices[vindex].y = (offset.y / ptmRatio);
vindex++;
}
polyshape->Set(vertices, vindex);
fix->fixture.shape = polyshape;
// create a list
*nextFixtureDef = fix;
nextFixtureDef = &(fix->next);
}
}
else if (fixtureType == "CIRCLE") {
FixtureDef *fix = new FixtureDef();
fix->fixture = basicData; // copy basic data
fix->callbackData = callbackData;
auto circleData = vecmap["circle"].asValueMap();
b2CircleShape *circleShape = new b2CircleShape();
circleShape->m_radius = circleData["radius"].asFloat() / ptmRatio;
Vec2 p = PointFromString(circleData["position"].asString());
circleShape->m_p = b2Vec2(p.x / ptmRatio, p.y / ptmRatio);
fix->fixture.shape = circleShape;
// create a list
*nextFixtureDef = fix;
nextFixtureDef = &(fix->next);
}
else {
CCAssert(0, "Unknown fixtureType");
}
}
// add the body element to the hash
shapeObjects[bodyName] = bodyDef;
}
1、使用方法
// Load shapes,傳入PE工具生產的plist文件,一個具有不規則形狀的物理碰撞就實現了
GB2ShapeCache::sharedGB2ShapeCache()->addShapesWithV3("sharp.plist");
2、下載地址