Changing box2d body shape dynamically

Is it possible to dynamically change the shape of Box2d b2Body? I want to do this to achieve the following.

I). Increase the radius of the circle at a certain speed

II). Resize the bounding box for the sprite to fit the changing frames of the animation.

+8
cocos2d-iphone box2d box2d-iphone
source share
1 answer
float radius; radius = 1; 

Create body:

 b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(300/PTM_RATIO, 150/PTM_RATIO); body = world->CreateBody(&bodyDef); b2CircleShape circleShape; circleShape.m_radius = radius; b2FixtureDef fixtureDef; fixtureDef.shape = &circleShape; fixtureDef.density = 1; fixtureDef.friction = 0.3f; body ->CreateFixture(&fixtureDef); 

In your sensory method:

  - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (body != NULL) { b2Fixture *fixtureA = body->GetFixtureList(); body->DestroyFixture(fixtureA); b2CircleShape circleShape; circleShape.m_radius = radius + 0.3; b2FixtureDef fixtureDef; fixtureDef.shape = &circleShape; fixtureDef.density = 1; fixtureDef.friction = 0.3f; body->CreateFixture(&fixtureDef); radius = radius + 0.3; } 

With each touch, the body will become larger by 0.3.

+1
source share

All Articles