在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
PHP中文网2017-04-17 12:04:59
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());