0
votes

I have used soft body physics in my game to make ball.Now when ball falls down on some platforms which are also b2Bodies or on GrounBody it completely destroyed & it's shape also changed.

I have referred this link : http://www.uchidacoonga.com/2012/04/soft-body-physics-with-box2d-and-cocos2d-part-44/

but when i am trying to change some of values like radius,frequencyHz ,dampingRatio etc then it gives result as per my first image , in which my ball looks so unshaped & destructed .

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

This code give me the result as shown here.Is there any solution to avoid this situation ???

i want output like this for that what changes i should made ? any suggestions !! please help. i would also like to know the reason behind this.

1

1 Answers

0
votes

It looks like the triangle fan is reacting with itself. Since you use a triangle fan to create a ball the triangles shouldn't interact, they are connected only. The code on the website you provided is a little bit different. After the first jointDef.Initialize the frequency and dampingRatio are 0.

But some other informations are missing like your NUM_SEGMENTS. Provide complete working code/functions (not the whole application), so someone other could compile and check it also.