Jondolf / avian

ECS-driven 2D and 3D physics engine for the Bevy game engine.
https://crates.io/crates/avian3d
Apache License 2.0
1.55k stars 120 forks source link

Reduce implicit defaults by requiring more components #504

Open Jondolf opened 2 months ago

Jondolf commented 2 months ago

For Bevy 0.15, which will have required components.

Objective

Currently, Avian has implied defaults for numerous components. This means that when a component doesn't exist on an entity, a default value or behavior is used in its place. Examples include:

and probably many more.

It seems like these kinds of implied default values for components should generally be discouraged. Quoting Cart (the Project Lead of Bevy) on Discord, in the context of UI:

I would (personally) like to move away from "implied default values":

  • It makes it harder to reason about what those are, as they often don't live in / near the struct, but rather in a system somewhere
    • This also makes it possible to have different implied default values, as each system can choose how to interpret it
  • It forces archetype moves when changing to a non-default value
  • It makes it hard to query for things (ex: give me everything with a transparent background) because now you need to check both Option<BackgroundColor> and BackgroundColor(Color::NONE), and it means you need to know to do both.
  • It makes it harder to discover functionality, as the "optional" component is now floating / decoupled

along with:

In general I think if you ask yourself "does this Component have a X", then it should have the component. For example, the answer to the question "does this Node have a z index?" seems to be pretty clearly "yes it has a local z index of 0", so it should have a ZIndex component to reflect that.

This was initially in the context of UI, so I asked if this should also be considered a "best practice" that the ecosystem as a whole should follow (along with some component examples), to which the response was:

Imo in pretty much all of these cases, if you ask yourself the question "does this entity have this property", the answer is "yes". For the reasons stated above, I think they should be required.

In summary, it seems like there is quite a clear preference (from Cart) towards not having implied default values for components. In practice, this would just mean having more components automatically inserted via the required components functionality coming in Bevy 0.15.

It didn't seem like there was full consensus on this in general though, and it's not entirely clear which components count as being a "property" of an entity. I'm personally not fully sold on requiring all of the components listed earlier in cases where the component is mostly intended for opting out of default behavior, or just otherwise very niche (e.g. GravityScale and SpeculativeMargin), but we can probably decide on a case-by-case basis.

Pros

Cons