2012-08-05 28 views
1

我通過屏幕創建character who movedbox2d cocos2d。計算SetLinearVelocity的方向

字符是box2d世界中的傳感器運動體。因爲,我想要實體與碰撞,但沒有重力。我嘗試使用精靈,但碰撞邊界框有很多問題,因爲物理bodes精靈動畫。

對於轉換邏輯,我打算使用SetLinearVelocity(b2Vec2)。 (0,0.5)移動身體頂部,身體 - > SetLinearVelocity(0.5,0.5)移動身體45°角,它的工作是正確的。

但我很愚蠢 - 如何移動身體使用這種方法不同點。

換句話說,我需要創建模擬CCMoveTo。問題 - 如何爲SetLinearVelocity計算真實的b2Vec2。

另外,如果你通過屏幕知道的過渡體更好的辦法,請分享你的想法

回答

3

O'k。在cocos2d-iphone.org論壇上,我得到答案。謝謝,夥計們。

CGPoint direction = ccp(targetPos.x - startPos.x, targetPos.y - startPos.y); 
direction = ccpNormalize(direction); 
float speed = 0.25f; 
b2Vec2 vel = b2Vec2(speed*direction.x/kPointsToMeterRatio, speed*direction.y/kPointsToMeterRatio); 
body->SetLinearVelocity(vel); 
+0

嘿羅姆我需要一些關於跳躍精靈的幫助。你可以幫我嗎?? – Leena 2012-08-30 09:08:34