开发者社区 问答 正文

如何让物体B和C冲离物体A呢?

在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呢?
screenshot

展开
收起
a123456678 2016-07-20 18:00:10 1878 分享 版权
1 条回答
写回答
取消 提交回答
  • 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());
    2019-07-17 19:59:09
    赞同 展开评论
问答地址: