Heim  >  Fragen und Antworten  >  Hauptteil

ios - 如何让物体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呢?

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

巴扎黑巴扎黑2718 Tage vor282

Antworte allen(1)Ich werde antworten

  • PHP中文网

    PHP中文网2017-04-17 12:04:59

    答: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());
    

    Antwort
    0
  • StornierenAntwort