0

我在我的游戏中使用了软体物理来制作球。现在,当球落在一些同样是 b2Bodies 或 GrounBody 的平台上时,它会完全被破坏并且它的形状也发生了变化。

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

但是当我试图改变半径、频率、阻尼比等一些值时,它会根据我的第一张图像给出结果,其中我的球看起来如此不成形和被破坏。

- (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);
}
}

这段代码给了我这里显示的结果。有什么解决方案可以避免这种情况吗???

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

4

1 回答 1

0

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

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

于 2013-07-13T07:07:00.977 回答