11

是否可以动态更改 Box2d b2Body 的形状?我想这样做以实现以下目标。

一世)。以一定的速度增加圆形的半径

ii)。更改精灵边界框的尺寸以匹配不断变化的动画帧。

4

1 回答 1

5
   float radius;
   radius = 1;

创建主体:

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

在您的触摸方法中:

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

   }

每次触摸身体都会变大0.3。

于 2014-07-05T10:15:39.547 回答