2012-02-28 91 views
1

我有一個姿態recignizer,我需要旋轉體:Box2D的身體旋轉

- (void) rotate:(UIGestureRecognizer*)recognizer node:(CCNode*)node 
{ 
    b2Body *body = (b2Body*)[node.parent userData]; 
    UIRotationGestureRecognizer* rotate = (UIRotationGestureRecognizer*)recognizer; 
    b2Vec2 pos = body->GetPosition(); 
    body->SetTransform(pos, (- rotate.rotation)); 

} 

offcorse,當我開始旋轉時,它從零角度開始。 * 但是如何從當前角度繼續旋轉? *我不能只添加rotate.rotation tu當前角度:這種方法每一次都會調用,角度是從開始時的手勢開始計算的。跟蹤實際的當前角度(不anctive手勢的角度),將是一個非常艱鉅的任務,我認爲

回答

1

我找到了解決辦法: 我檢查的手勢的狀態(有一個begining狀態):

- (void) rotate:(UIGestureRecognizer*)recognizer node:(CCNode*)node 
{ 
    b2Body *body = (b2Body*)[node.parent userData]; 
    UIRotationGestureRecognizer* rotate = (UIRotationGestureRecognizer*)recognizer; 
    if (rotate.state == UIGestureRecognizerStateBegan) 
    { 
     baseAngle = body->GetAngle(); 
    } 
    b2Vec2 pos = body->GetPosition(); 
    body->SetTransform(pos, (baseAngle - rotate.rotation)); 

}