0

我有一个 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());  
    }
}
4

1 回答 1

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);
}
于 2013-05-05T14:58:26.937 回答