如何防止box2d中的软体球被破坏?

时间:2013-07-13 06:27:39

标签: cocos2d-iphone box2d-iphone

我已经在我的游戏中使用了软体物理来制造球。现在,当球落在某些平台上时,这些平台也是b2Bodies或者在GrounBody上它完全被毁坏了。它的形状也发生了变化。

我已经提到过这个链接:http://www.uchidacoonga.com/2012/04/soft-body-physics-with-box2d-and-cocos2d-part-44/

但是当我试图更改一些像radius,frequencyHz,dampingRatio等值的值时,它会根据我的第一张图像给出结果,其中我的球看起来不那么形状&破坏了。

- (void) createPhysicsObject:(b2World *)world {
    // Center is the position of the circle that is in the center (inner circle)
    b2Vec2 center = b2Vec2(240/PTM_RATIO, 160/PTM_RATIO);
    b2CircleShape circleShape;
    circleShape.m_radius = 0.20f;

b2FixtureDef fixtureDef;
fixtureDef.shape = &circleShape;
fixtureDef.density = 0.1;
fixtureDef.restitution = -2;
fixtureDef.friction = 1.0;

// Delta angle to step by
deltaAngle = (2.f * M_PI) / NUM_SEGMENTS;

// Radius of the wheel
float radius = 50;

// Need to store the bodies so that we can refer back
// to it when we connect the joints
bodies = [[NSMutableArray alloc] init];

for (int i = 0; i < NUM_SEGMENTS; i++) {
    // Current angle
    float theta = deltaAngle*i;

    // Calculate x and y based on theta
    float x = radius*cosf(theta);
    float y = radius*sinf(theta);
    // Remember to divide by PTM_RATIO to convert to Box2d coordinate
    b2Vec2 circlePosition = b2Vec2(x/PTM_RATIO, y/PTM_RATIO);

    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    // Position should be relative to the center
    bodyDef.position = (center + circlePosition);

    // Create the body and fixture
    b2Body *body;
    body = world->CreateBody(&bodyDef);
    body->CreateFixture(&fixtureDef);

    // Add the body to the array to connect joints to it
    // later. b2Body is a C++ object, so must wrap it
    // in NSValue when inserting into it NSMutableArray
    [bodies addObject:[NSValue valueWithPointer:body]];
}

// Circle at the center (inner circle)
b2BodyDef innerCircleBodyDef;
// Make the inner circle larger
circleShape.m_radius = 0.8f;
innerCircleBodyDef.type = b2_dynamicBody;

// Position is at the center
innerCircleBodyDef.position = center;
innerCircleBody = world->CreateBody(&innerCircleBodyDef);
innerCircleBody->CreateFixture(&fixtureDef);

// Connect the joints
b2DistanceJointDef jointDef;
for (int i = 0; i < NUM_SEGMENTS; i++) {
    // The neighbor
    const int neighborIndex = (i + 1) % NUM_SEGMENTS;

    // Get current body and neighbor
    b2Body *currentBody = (b2Body*)[[bodies objectAtIndex:i] pointerValue];
    b2Body *neighborBody = (b2Body*)[[bodies objectAtIndex:neighborIndex] pointerValue];

    // Connect the outer circles to each other
    jointDef.Initialize(currentBody, neighborBody,
                        currentBody->GetWorldCenter(), 
                        neighborBody->GetWorldCenter() );
    // Specifies whether the two connected bodies should collide with each other
    jointDef.collideConnected = true;
    jointDef.frequencyHz = 25.0f;
    jointDef.dampingRatio = 0.5f;

    world->CreateJoint(&jointDef);

    // Connect the center circle with other circles        
    jointDef.Initialize(currentBody, innerCircleBody, currentBody->GetWorldCenter(), center);
    jointDef.collideConnected = true;
    jointDef.frequencyHz = 25.0;
    jointDef.dampingRatio = 0.5;

    world->CreateJoint(&jointDef);
}
}

这段代码给我的结果如下所示。有什么解决办法可以避免这种情况吗?

我想要这样的输出 为此我应该做出哪些改变?有什么建议 !!请帮忙。 我也想知道背后的原因。

1 个答案:

答案 0 :(得分:0)

看起来三角扇正在与自身作出反应。由于您使用三角形扇形来创建一个球,三角形不应该相互作用,它们只是连接在一起。您提供的网站上的代码有点不同。在第一个jointDef.Initialize之后,频率和dampingRatio都为0.

但是您的NUM_SEGMENTS缺少其他一些信息。提供完整的工作代码/功能(不是整个应用程序),所以其他人也可以编译和检查它。