b2RevoluteJoint in cocos2d

时间:2013-05-05 10:43:50

标签: c++ ios objective-c cocos2d-iphone box2d

我有一个CCNode子类,它由一个围绕锚点旋转的矩形精灵组成。我的对象在世界中显示,但我似乎无法使b2RevoluteJoint正常工作。该对象只是保持静止。

这是我的RotatingArm课程的外观。

-(id) init {
    if (self = [super init]) {
        rect = [CCSprite spriteWithFile:@"square.png"];
        [self addChild:rect z:1];
    }
    return self;
}

-(void) addBodyToWorld: (b2World*) world {
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(self.position.x / PTM_RATIO,self.position.y / PTM_RATIO);
    bodyDef.userData = self;
    bodyDef.fixedRotation = true;
    self->body = world->CreateBody(&bodyDef);

   b2PolygonShape shape;
   shape.SetAsBox(rect.contentSize.width/PTM_RATIO/2,rect.contentSize.height/PTM_RATIO/2);
   [self createFixture:&shape forBody:body];

   [self addRotationJointInWorld:world];
}


- (void) addRotationJointInWorld:(b2World*)world  {

    CCSprite *sprite = [CCSprite spriteWithFile:@"anchor.png"];
    sprite.color = ccc3(50, 250, 50);
    [self addChild:sprite z:2];

    b2BodyDef rotationpointdef;
    rotationpointdef.type = b2_staticBody;
    rotationpointdef.position.Set(0,0);
    rotationpointdef.userData = sprite;
    rotationpointdef.fixedRotation = TRUE;
    rotationpoint = world->CreateBody(&rotationpointdef);

   b2PolygonShape rotationpointbox;
   rotationpointbox.SetAsBox(sprite.boundingBox.size.width / PTM_RATIO / 2.0,
                   sprite.boundingBox.size.height / PTM_RATIO / 2.0);

   [self createFixture:&rotationpointbox forBody:rotationpoint];

   b2RevoluteJointDef armJointDef;
   armJointDef.Initialize(rotationpoint, body, b2Vec2(rect.boundingBox.size.width/2/PTM_RATIO/2,rect.boundingBox.size.height/2/PTM_RATIO/2));
   armJointDef.enableMotor = TRUE;
   armJointDef.enableLimit = NO;
   armJointDef.motorSpeed  = 2;
   armJointDef.maxMotorTorque = 4800;

   armJoint = (b2RevoluteJoint*)world->CreateJoint(&armJointDef);

}


-(void) createFixture: (b2Shape*) shape forBody:(b2Body *) thebody {
    b2FixtureDef fixtureDef;
    fixtureDef.shape = shape;
    fixtureDef.density = 10.0f;
    thebody->CreateFixture(&fixtureDef);
}

然后在我的主游戏层中,我简单地实例化一个旋转的手臂对象:

RotatingArm *arm = [[[RotatingArm alloc] init] autorelease];
    arm.position = ccp(screen.width/2, screen.height/2);
    [self addChild:arm z:3];
    [arm addBodyToWorld:_world];

我的主游戏图层还有一个更新精灵位置的更新方法

 -(void) update: (ccTime) dt {
    world->Step(dt, 10, 10);
    for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
      if (b->GetUserData() != NULL) {

        CCSprite *sprite = (CCSprite *)b->GetUserData();
        sprite.position = ccp(b->GetPosition().x * PTM_RATIO,
                              b->GetPosition().y * PTM_RATIO);

        sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());  
    }
}

1 个答案:

答案 0 :(得分:0)

我设法让它发挥作用。 在我的addbodytoworld方法

bodyDef.fixedRotation = false;

而不是真实。 (我想这是主要问题)

我稍微简化了addrotationJoint方法

- (void) addRotationJoint {
    b2BodyDef bodyDef;
    bodyDef.position.Set(0,0);
    bodyDef.type = b2_staticBody;
    b2Body* staticBody = body->GetWorld()->CreateBody(&bodyDef);

    b2RevoluteJointDef armJointDef;
    armJointDef.Initialize(staticBody, body, b2Vec2(body->GetWorldCenter()));
    armJointDef.enableMotor = TRUE;
    armJointDef.enableLimit = NO;
    armJointDef.motorSpeed  = 2;
    armJointDef.maxMotorTorque = 4800;

    armJoint = (b2RevoluteJoint*)body->GetWorld()->CreateJoint(&armJointDef);
}