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

在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

巴扎黑
巴扎黑

reply all(1)
Ty80

Answer: jrtc27
After reading your question, I think you can use the following code to achieve this effect:

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());
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!