我试图通过像这样连接 3 个矩形在 Box2d(在 Cocos2d 中)中创建一个U"形:|_|
I'm attempting to create a "U" shape in Box2d (in Cocos2d) by joining 3 rectangles like so: |_|
听起来关节在这里不是正确的解决方案,因为我不想要任何运动,所以我创建了一个主体,它是中间位和侧面的 2 个固定装置.我已经像这样将两侧添加到中间位:
It sounds like joints are not the correct solution here since I don't want any movement so I have created a main body which is the middle bit and 2 fixtures for the sides. I've added the two sides to the middle bit like this:
mainBody->CreateFixture(&leftFixtureDef);
mainBody->CreateFixture(&rightFixtureDef);
这可行,但是两侧的固定装置都被添加到主体的中心.我似乎无法弄清楚如何相对于主体定位固定装置.将精灵/节点附加到夹具并更改位置似乎没有什么区别.
This works, however both side fixtures get added to the center of the mainBody. I can't seem to work out how to position the fixtures relative to the main body. Attaching a sprite/node to the fixture and changing the position doesn't seem to make a difference.
有什么想法吗?
非常感谢.
这是一个形状的属性.我没有为 b2CircleShape 找到这样的属性,但是对于 b2PolygonShape
有 m_centroid
参数 - 它是相对于身体的形状中心坐标.将其指定为具有有效的形状位置.
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.
对于 b2PolyganShape 有一个方法 setAsBox(w, h)
但还有一个更复杂的方法:
For b2PolyganShape there is a method setAsBox(w, h)
but alos there is more complex one:
setAsBox(float32 width, float32 height, const b2Vec2 ¢er, float32 rotation)
使用此方法或手动指定质心.
Use this method or specify the centroid manualy.
这是U形的代码
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);