当前位置 : 主页 > 手机开发 > 其它 >

cocos2d-iphone – 动态改变box2d体形

来源:互联网 收集:自由互联 发布时间:2021-06-13
是否可以动态改变Box2d b2Body的形状?我想这样做以实现以下目标. 一世).以一定的速率增加圆形的半径 ⅱ).更改精灵的边界框的尺寸以匹配更改的动画帧. float radius; radius = 1; 创建身体:
是否可以动态改变Box2d b2Body的形状?我想这样做以实现以下目标.

一世).以一定的速率增加圆形的半径

ⅱ).更改精灵的边界框的尺寸以匹配更改的动画帧.

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.

网友评论