createBody method
You should create the Forge2D Body in this method when you extend the BodyComponent.
The default implementation creates the body from bodyDef and the shapes from shapeSpecs. When the userData of bodyDef or of a shape's ShapeDef is a ContactCallbacks, contact and sensor events are automatically enabled for that shape, since Forge2D only generates events for shapes that have opted in to them. If you override this method you have to enable the event flags yourself.
Note that the event flags are set on the ShapeDef of the ShapeSpec itself, since Forge2D snapshots them when the shape is created. Don't share a single ShapeDef instance between components if you don't want them to share those flags.
Implementation
Body createBody() {
assert(
bodyDef != null,
'Ensure this.bodyDef is not null or override createBody',
);
final body = world.createBody(bodyDef);
final bodyHasCallbacks = bodyDef!.userData is ContactCallbacks;
for (final spec in shapeSpecs ?? const <ShapeSpec>[]) {
final definition = spec.definition ?? ShapeDef();
if (bodyHasCallbacks || definition.userData is ContactCallbacks) {
definition.enableContactEvents = true;
definition.enableSensorEvents = true;
}
body.createShape(spec.geometry, definition);
}
return body;
}