destroyFixture method

void destroyFixture(
  1. Fixture fixture
)

Destroy a fixture. This removes the fixture from the broad-phase and destroys all contacts associated with this fixture. This will automatically adjust the mass of the body if the body is dynamic and the fixture has positive density. All fixtures attached to a body are implicitly destroyed when the body is destroyed.

Warning: This function is locked during callbacks.

Implementation

void destroyFixture(Fixture fixture) {
  assert(!world.isLocked);
  assert(fixture.body == this);

  // Remove the fixture from this body's singly linked list.
  assert(fixtures.isNotEmpty);
  final removed = fixtures.remove(fixture);

  // You tried to remove a shape that is not attached to this body.
  assert(
    removed,
    'You tried to remove a fixture that is not attached to this body',
  );

  // Destroy any contacts associated with the fixture.
  var i = 0;
  while (i < contacts.length) {
    final contact = contacts[i];
    if (fixture == contact.fixtureA || fixture == contact.fixtureB) {
      // This destroys the contact and removes it from
      // this body's contact list.
      world.contactManager.destroy(contact);
    } else {
      /// Increase index only if contact was not deleted and need move to next
      /// one.
      /// If contact was deleted, then index should not be increased.
      i++;
    }
  }

  if ((flags & activeFlag) == activeFlag) {
    final broadPhase = world.contactManager.broadPhase;
    fixture.destroyProxies(broadPhase);
  }

  // Reset the mass data.
  resetMassData();
}