Box2d multiple fixtures and positioning

前端 未结 1 1525
刺人心
刺人心 2020-12-09 05:08

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 do

相关标签:
1条回答
  • 2020-12-09 05:11

    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);
    
    0 讨论(0)
提交回复
热议问题