{
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set(location.x / PTM_RATIO, location.y / PTM_RATIO);
bodyDef.userData = pSprite;
//bodyDef.userData = nullptr;
b2Body* body = _world->CreateBody(&bodyDef);
body->SetLinearVelocity(b2Vec2(1, 0));
b2PolygonShape poly;
b2Vec2 vertices[7];
vertices[0].Set(0, 0);
vertices[1].Set(1, 0);
vertices[2].Set(1, 1);
vertices[3].Set(0.75f, 1);
vertices[4].Set(0.5f, 1.5f);
vertices[5].Set(0.25f, 1);
vertices[6].Set(0, 1);
poly.Set(vertices, 7);
b2FixtureDef fixtureDef;
fixtureDef.shape = &poly;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.5f;
fixtureDef.restitution = 0.0f;
body->CreateFixture(&fixtureDef);
}
我使用顶点值制作box2d的主体。
(它使用的是b2ChainShape,所以内部是空的)
为什么会出现这些差异?
我不能解决这个问题吗?
b2PolygonShape
不能用于concave polygons;它只是为convex polygons设计的。
b2PolygonShape
的实现使用convex hull计算算法,该算法将点集从你想要的(如你的第一张图片所示)变成你得到的(如第二张图片所示)。
这被列为common mistake:使用b2Polygon
创建凹多边形。
您可以使用引用的b2ChainShape
。
如果内部区域为空,则不是您想要的,您可以使用一组凸多边形来构建凹形。
只需要两个凸多边形即可创建您显示的第一个形状。这些凸多边形中的一个是三角形,另一个是矩形(或正方形)。
希望这可以帮助。