prime31 / Nez

Nez is a free 2D focused framework that works with MonoGame and FNA
MIT License
1.76k stars 355 forks source link

More fixes for disabled Colliders #740

Closed thatpixelguy closed 1 year ago

thatpixelguy commented 1 year ago

Fixes an issue where when an Entity is enabled, it will call OnEnabled() on all of its components, even those that are individually disabled. This appears to be a bug, as it's very counter-intuitive for Component.OnEnabled() to be called when the component is still disabled.

This behavior was causing a bug with the Collider class which can be reproduced as follows:

var collider = entity.AddComponent(new BoxCollider());
collider.Enabled = false;

// wait an update cycle

entity.Enabled = false;

// wait an update cycle

entity.Enabled = true;

// collider.OnEnabled() will then be called and the disabled collider will register itself to the physics system

(If it's desirable for a disabled Component to know when the entity is enabled, I'd propose adding an OnEntityEnabled() method to the Component class.)

I've also made several other small fixes -- methods like BoxCollider.SetSize would re-register the collider with the physics system when the collider was disabled.

Finally, the ColliderTriggerHelper class would check for trigger collisions on all of the entity's attached colliders, even if they're disabled. It will simply skip over disabled colliders now.

prime31 commented 1 year ago

Ok. I tested on a few projects and this looks good. Merging!