2012-03-26 17 views
0

私は、b2Boxを使用してヒンジ(私はb2RevoluteJointを使用しています)で接続された2つのボディの動作をモデル化しようとしています。最初の2つのオブジェクトの相対的な位置(オーバーラップ)を次に示します。Box2D:b2RevoluteJointを正しく動作させることができません。

それらの両方は、赤い点を(それらが同じグローバル座標である)を有します。私は長いオブジェクトがこのドットの周りを回転することを期待しています。

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

これを起動した後、私は運動の異なる種類を取得しています:ここでは、コード(anchorViewは小さな正方形、rotorViewは長い棒である)です。ここでは次のようになります(私は明確化のためのいくつかのストロークを追加):

より正確には、赤いドットが一緒にバインドされていないが、しかし、ロープがありますかのように体中心間の距離は常に、同じままそこで私は点線を描いた。

デバッガでjointDef.localAnchorBを見ると、ロングスティックの座標系内に赤い点の右座標が含まれています。何が間違っていますか?

答えて

0

解決済み。

CGFloat angle = rotorBody->GetAngle(); 

、それがなければなりませんので

CGAffineTransform rt = CGAffineTransformMakeRotation(angle); 

BOX2DとCocoa Touchのは、回転角度をカウントするための異なる方向を使用します:それはすべての2行についてだった

CGAffineTransform rt = CGAffineTransformMakeRotation(-angle); 
関連する問題