Box2d multiple fixtures and positioning

佐手、 提交于 2019-11-28 07:36:30

it's the property of a shape. I did not find such property for b2CircleShape, but for b2PolygonShape has m_centroid paramter - it's the shape center coordinates relative to the body. Specify it to have a valid position of a shape.

For b2PolyganShape there is a method setAsBox(w, h) but alos there is more complex one:

setAsBox(float32 width, float32 height, const b2Vec2 &center, float32 rotation)

Use this method or specify the centroid manualy.

Here is the code for the U shape

b2BodyDef bDef;
bDef.type = b2_dynamicBody;
bDef.position = b2Vec2(0, 0);
b2Body *body = world_->CreateBody(&bDef);

b2PolygonShape shape;
const float32 density = 10;

shape.SetAsBox(1, 0.1);
body->CreateFixture(&shape, density);

shape.SetAsBox(0.1, 1, b2Vec2(-1 + 0.1, 1), 0);
body->CreateFixture(&shape, density);

shape.SetAsBox(0.1, 1, b2Vec2(1 - 0.1, 1), 0);
body->CreateFixture(&shape, density);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!