Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sensors and mass properties #355

Closed
wants to merge 1 commit into from
Closed

Sensors and mass properties #355

wants to merge 1 commit into from

Conversation

yrns
Copy link
Contributor

@yrns yrns commented Mar 29, 2024

Objective

I was surprised to find that a sensor added as a child of a dynamic body affected the parent's behavior. Even with the density set to zero the difference was noticeable.

Solution

  • Make the default mass and density zero for sensors.

and/or:

  • Ignore sensors when updating parents' mass properties.

Maybe instead of adding a special case for sensors (which would be a breaking change), a note should be added to the sensor documentation indicating you need to set the mass properties to zero if you don't want to affect any parents?


Changelog

tbd

Migration Guide

tbd

- Default density and mass 0.0 for sensors
- Ignore sensors when updating parents' mass properties
@Jondolf
Copy link
Owner

Jondolf commented Jun 22, 2024

Hi, I do agree we should change this. I looked into it a bit, and it seems like the sensor equivalents in Godot (Area2D/Area3D) and Unity (trigger property) also don't contribute to the mass properties of the rigid body.

I think the semantics we should go for are that sensors don't affect physics at all, and are only for detecting intersections. The way I would do this is slightly different from this PR though:

  • Don't run update_collider_mass_properties for sensors. This PR already does this :)
  • When Sensor is added to a collider, remove the collider's mass properties (if any) from the rigid body's mass properties.
  • When Sensor is removed from a collider, update the collider's mass properties and add them to the rigid body's mass properties.

I think this could be implemented robustly in Bevy 0.14 using component lifecycle hooks or observers. Notably different from this PR, I would not make the default ColliderDensity zero, because it is rather implicit, and if I later removed the Sensor component from the collider, I would expect that it would have mass properties again like a default collider.

Do you mind if I adopt this and implement it for the upcoming Bevy 0.14 update with lifecycle hooks (or observers)? I can still add you as a co-author :)

@Jondolf Jondolf added the C-Bug Something isn't working label Jun 22, 2024
@yrns
Copy link
Contributor Author

yrns commented Jun 22, 2024

All that sounds good. Thanks for taking a look.

@Jondolf
Copy link
Owner

Jondolf commented Jun 22, 2024

Closing in favor of #381 :)

@Jondolf Jondolf closed this Jun 22, 2024
Jondolf added a commit that referenced this pull request Jun 23, 2024
# Objective

Similar to #355 by @yrns, but without special casing `ColliderDensity` defaults, and implemented with observers.

Sensor colliders currently contribute to the mass properties of rigid bodies. This differs from most peoples' expectations, and is also different from many existing engines such as Godot and Unity. Sensor colliders should have no impact on the physics simulation.

## Solution

- When the `Sensor` component is added to an entity, remove the collider's contribution on the rigid body's mass properties.
- When the `Sensor` component is removed from an entity, add the collider's mass properties to the rigid body's mass properties.

## Testing

There is a test to make sure that the mass properties of a rigid body are updated accordingly when the `Sensor` component is first added and then removed from a collider.

---

## Migration Guide

Colliders with the `Sensor` component no longer contribute to the mass properties of rigid bodies. You can add mass for them by adding another collider that is *not* a sensor, or by manually adding mass properties with the `MassPropertiesBundle` or its components.

Additionally, the mass properties of `Sensor` colliders are no longer updated automatically, unless the `Sensor` component is removed.

---------

Co-authored-by: Al McElrath <hello@yrns.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants