2010-10-08 61 views
0
轉動

我試圖旋轉一個box2d正文,通過在我的iPhone應用程序中的box2d的GetUserData()綁定到一個cocos2d精靈。具體來說,我試圖抓住最新的觸摸位置並在該方向旋轉我的box2d主體。cocos2d + box2d:向點

對於box2d我相當缺乏經驗,所以任何意見將不勝感激。下面是一個快速刺傷我如何想象我會操縱球員box2d身體。我想澄清:

1)如果這是做事的正確方法。 2)我如何計算玩家和最後一次觸摸位置之間的角度,以便在該方向上旋轉我的玩家。

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    b2Body *pBody = self.playerBody; 

    if(pBody != NULL) { 
     for(UITouch *touch in touches) { 
      CGPoint location = [touch locationInView: [touch view]]; 
      location = [[CCDirector sharedDirector] convertToGL: location]; 

      CCSprite *myActor = (CCSprite*)pBody->GetUserData(); 
      pBody->SetTransform(pBody->GetPosition(), angleToRotateByInRadians); 
     } 
    } 
} 
+0

不要使用的setTransform。 (對於任何查看此問題的人) – tallen11 2012-08-04 01:07:29

回答

0

獲取的角度(弧度)兩點之間:

atan2(pointOne.x - pointTwo.x, pointOne.y - pointTwo.y) 
+2

不應該是'atan2(ydifference,xdifference)'而不是? – Lukman 2011-05-04 00:11:31