Reputation: 5154
I'm applying an impulse on a box2d object with something like this:
b2Vec2 impulse = b2Vec2(4.0f, 15.0f);
body->ApplyLinearImpulse(impulse, body->GetWorldCenter());
I know this is probably high school math, and I promise I have made efforts to discover this for myself; please forgive my ignorance.
If I have objects a, b and c - and object a is at the midpoint of b and c, how can I create a Box2D impulse so that objects b and c move away from a at velocity v?
Upvotes: 5
Views: 301
Reputation: 8536
Try using this:
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());
I hope it's clear what's going on here. If not, just ask :)
Upvotes: 3