applyLinearImpulse method

  1. @override
void applyLinearImpulse(
  1. PhysicsBody body,
  2. double ix,
  3. double iy
)
override

Apply an instantaneous change in momentum to body.

Unlike writing velocity directly this preserves existing motion, which is what knockback, bounce pads and explosions want.

Implementation

@override
void applyLinearImpulse(PhysicsBody body, double ix, double iy) {
  if (!_nativeReady) {
    super.applyLinearImpulse(body, ix, iy);
    return;
  }
  final b2Body = _bodyMap[body];
  if (b2Body == null || !body.isDynamic) return;

  b2Body.applyLinearImpulse(ix, iy);
  // Mirror the resulting velocity change into the Dart body as well.
  //
  // update() pushes body.velocity into native at the top of EVERY frame, so
  // an impulse applied between frames would be integrated by Box2D and then
  // immediately overwritten by the stale Dart value on the next push. The
  // impulse would appear to work for exactly one step and then vanish.
  body.velocity.x += ix * body.inverseMass;
  body.velocity.y += iy * body.inverseMass;
  body.isAwake = true;
  body.sleepTimer = 0.0;
}