0

我有两个对象......一个静态和一个动态(它是一个球)。静态对象具有多边形形状。有时当我击球时,球会穿过静态物体:(。我玩过密度和其他属性,但没有成功。有人知道我如何让静态物体变得不可穿透。

这是我的代码

// Create ball body
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(110 / 2 / PTM_RATIO, 300 / PTM_RATIO);
ballBodyDef.userData = playerCartoonSprite;
ballBodyDef.bullet = YES;
playerCartoonBody = localWorld->CreateBody(&ballBodyDef);

// Create circle shape
b2CircleShape circle;
circle.m_radius = 10.0 / PTM_RATIO;

// Create shape definition and add to body
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 0.0f;
ballShapeDef.friction = 0.3f;
ballShapeDef.restitution = 1.f;
playerCartoonFixture = playerCartoonBody->CreateFixture(&ballShapeDef);

// static polygon object

    b2BodyDef bodyDef;
    bodyDef.type = b2_staticBody;

    int num = 6;
    b2Vec2 verts[] = {
        b2Vec2(8.6f / PTM_RATIO, 89.3f / PTM_RATIO),
        b2Vec2(5.0f / PTM_RATIO, 81.7f / PTM_RATIO),
        b2Vec2(10.5f / PTM_RATIO, 61.9f / PTM_RATIO),
        b2Vec2(13.1f / PTM_RATIO, 9.1f / PTM_RATIO),
        b2Vec2(17.3f / PTM_RATIO, 9.9f / PTM_RATIO),
        b2Vec2(12.7f / PTM_RATIO, 70.6f / PTM_RATIO)
    };

    b2PolygonShape boxShape;
    boxShape.Set(verts, num);
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &boxShape;
    bodyDef.position.Set(teeterSprite.position.x / PTM_RATIO, teeterSprite.position.y / PTM_RATIO);
    fixtureDef.shape = &boxShape;
    m_bodyA = world->CreateBody( &bodyDef );
    m_bodyA->CreateFixture( &fixtureDef );
4

1 回答 1

0

我认为这可以解决我的问题http://www.emanueleferonato.com/2008/12/19/understanding-custom-polygons-in-box2d/

于 2013-03-01T15:20:26.233 回答