2012-03-26 51 views
0

我試圖用b2Box來模擬兩個使用鉸鏈(我使用b2RevoluteJoint)連接的物體的行爲。以下是兩個對象從一開始就相對(重疊)的位置。Box2D:無法讓b2RevoluteJoint正常工作

他們都有一個紅點(他們是在同一個全局座標)。我期望更長的物體圍繞這個點旋轉。下面的代碼(anchorView是小廣場,rotorView是長棒):

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIView *anchorView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 30, 30)]; 
    anchorView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.2]; 
    UIView *anchorDot = [[UIView alloc] initWithFrame:CGRectMake(13, 13, 4, 4)]; 
    anchorDot.backgroundColor = [UIColor redColor]; 
    [self.view addSubview:anchorView]; 
    [anchorView addSubview:anchorDot]; 

    UIView *rotorView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 300, 30)]; 
    rotorView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.2]; 
    UIView *rotorDot = [[UIView alloc] initWithFrame:CGRectMake(13, 13, 4, 4)]; 
    rotorDot.backgroundColor = [UIColor redColor]; 
    [self.view addSubview:rotorView]; 
    [rotorView addSubview:rotorDot]; 

    b2Vec2 gravity; gravity.Set(0.0f, -9.81f); 

    world = new b2World(gravity);  
    world->SetContinuousPhysics(true); 

    // Static anchor 
    b2BodyDef anchor; 
    anchor.position.Set(anchorView.center.x/PTM, (1004.0 - anchorView.center.y)/PTM); 
    anchor.type = b2_staticBody;  
    b2Body* anchorBody = world->CreateBody(&anchor); 

    b2PolygonShape anchorBox; 
    anchorBox.SetAsBox(anchorView.frame.size.width/PTM/2.0, anchorView.frame.size.height/PTM/2.0); 
    anchorBody->CreateFixture(&anchorBox, 0.0); 

    // Dynamic rotor 
    b2BodyDef rotor; 
    rotor.position.Set(rotorView.center.x/PTM, (1004.0 - rotorView.center.y)/PTM); 
    rotor.type = b2_dynamicBody; 
    b2Body *rotorBody = world->CreateBody(&rotor); 

    b2PolygonShape rotorBox; 
    rotorBox.SetAsBox(rotorView.frame.size.width/PTM/2.0, rotorView.frame.size.height/PTM/2.0); 
    rotorBody->CreateFixture(&rotorBox, 0.0); 

    rotorView.tag = (int)rotorBody; 

    // Joint 
    b2RevoluteJointDef jointDef; 
    jointDef.Initialize(anchorBody, rotorBody, anchorBody->GetWorldCenter()); 
    world->CreateJoint(&jointDef); 

    // doing stuff 
    rotorBody->SetAngularVelocity(5.0); 

    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0/30.0 target:self selector:@selector(onTimer:) userInfo:rotorView repeats:YES]; 
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];    
} 

- (void)onTimer:(NSTimer *)theTimer { 
    int32 velocityIterations = 8; 
    int32 positionIterations = 1; 

    UIView *rotorView = [theTimer userInfo]; 
    b2Body *rotorBody = (b2Body *)rotorView.tag; 

    world->Step(0.3f/60.0f, velocityIterations, positionIterations); 

    CGFloat angle = rotorBody->GetAngle(); 
    b2Vec2 c = rotorBody->GetWorldCenter(); 

    rotorView.center = CGPointMake(c.x * PTM, 1004.0 - c.y * PTM); 

    CGAffineTransform rt = CGAffineTransformMakeRotation(angle); 
    rotorView.transform = rt; 
} 

開始這個我收到不同類型的運動後。這裏是什麼樣子(我加了澄清幾筆):

更確切地說,紅點沒有綁定在一起,但身體的中心之間的距離始終保持不變,彷彿有一根繩子我畫了虛線。

如果我在調試器中查看jointDef.localAnchorB,它包含長棒座標系中紅點的右座標。我做錯了什麼?

回答

0

已解決。這是所有關於這兩條線:

CGFloat angle = rotorBody->GetAngle(); 

CGAffineTransform rt = CGAffineTransformMakeRotation(angle); 

的Box2D和可可觸摸使用不同的方向進行計數的旋轉角度,所以它必須是:

CGAffineTransform rt = CGAffineTransformMakeRotation(-angle);