ios - 如何让物体B和C冲离物体A呢?
巴扎黑
巴扎黑 2017-04-17 12:02:59
0
1
278

在Box2D中,我用如下代码想要实现一个impulse:

b2Vec2 impulse = b2Vec2(4.0f, 15.0f);
body->ApplyLinearImpulse(impulse, body->GetWorldCenter());

这个问题看起来可能很幼稚,但是的确一直困扰着我。如果我有三个物体,A, B, C,而物体A在B和C的中间,我该如何创建一个impulse,让B和C可以以速度v冲离A呢?

原文:How can I make an impulse so that point b moves away from point a in box2d/ cocos2diphone

巴扎黑
巴扎黑

全部回覆(1)
Ty80

答:jrtc27
看了你的問題,我覺得可以用以下程式碼實現這個效果:

b2Vec2 impulseB = bodyB->GetPosition() - bodyA->GetPosition();
impulseB /= impulseB.Length();
impulseB *= predefinedScaleValue; // predefinedScaleValue is your velocity
b2Vec2 impulseC = -impulseB;
bodyB->ApplyLinearImpulse(impulseB, bodyB->GetWorldCenter());
bodyC->ApplyLinearImpulse(impulseC, bodyC->GetWorldCenter());
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!