Box2d多个夹具和定位 [英] Box2d multiple fixtures and positioning
问题描述
我试图在Box2d(在Cocos2d中)通过连接3个矩形来创建一个U形:像这样:| _ |
I'm attempting to create a "U" shape in Box2d (in Cocos2d) by joining 3 rectangles like so: |_|
不是正确的解决方案在这里,因为我不想要任何运动,所以我已经创建了一个主体是中间位和两个夹具的两侧。我把两边添加到中间位,像这样:
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);
这样工作,但是两个装置都被添加到mainBody的中心。我似乎无法解决如何相对于主体定位夹具。
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
paramter - 它是相对于身体。指定它有一个形状的有效位置。
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)
但是alos有更复杂的:
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);
这篇关于Box2d多个夹具和定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!