当前位置: 动力学知识库 > 问答 > 编程问答 >

Cocos2d Box2d multiple b2circle shape fixture for one body with positioning

问题描述:

Anybody knows how to add 2 circle fixture to one b2body with desired positioning?

I know how to add two polygon fixture to one body by using m_centroid. But how can I do it for circle fixtures.

Any answer will be appreciated. I want to stick some object together. I tried joints but they all are elastic. I want distance static.

Thanks everyone!

网友答案:

You should create two fixtures for your body and the shapes of those fixtures should be b2CircleShape

//Create a body. You'll need a b2BodyDef, but I've assumed you know how to use these since you say you've created bodies successfully before.
b2Body* body = world->CreateBody(&bodyDef);

//Create the first circle shape. It's offset from the center of the body by -2, 0.
b2CircleShape circleShape1;
circleShape1.m_radius = 0.5f;
circleShape1.m_p.Set(-2.0f, 0.0f);

b2FixtureDef circle1FixtureDef;
circle1FixtureDef.shape = &circleShape1;
circle1FixtureDef.density = 1.0f;


//Create the second circle shape. It's offset from the center of the body by 2, 0.
b2CircleShape circleShape2;
circleShape2.m_radius = 0.5f;
circleShape2.m_p.Set(2.0f, 0.0f);

b2FixtureDef circle2FixtureDef;
circle2FixtureDef.shape = &circleShape2;
circle2FixtureDef.density = 1.0f;


//Attach both of these fixtures to the body.
body->CreateFixture(&circle1FixtureDef);
body->CreateFixture(&circle2FixtureDef);
分享给朋友:
您可能感兴趣的文章:
随机阅读: