2013-05-05 68 views
0

我有一個CCNode子類由一個圍繞一個錨點旋轉的矩形Sprite組成。我的對象顯示在世界中,但我似乎無法讓b2RevoluteJoint正常工作。該對象簡單地保持靜態。b2RevoluteJoint在cocos2d

這是我的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()); 
    } 
} 

回答

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