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

Proper support for custom mass properties in 2D/3D physics bodies #49610

Merged

Conversation

pouleyKetchoupp
Copy link
Contributor

@pouleyKetchoupp pouleyKetchoupp commented Jun 14, 2021

Default center of mass in 2D:
Compute center of mass based on shapes by default in 2D, same as in 3D.
This change breaks compatibility because it was always using the center of the body before, although in common cases the computed center of mass is still going to be at the center.

Custom center of mass:
Can be set through the physics server using:
PhysicsServer2D.body_set_param(get_rid(), PhysicsServer2D.BODY_PARAM_CENTER_OF_MASS, center_of_mass)
Can be reset through the physics server using:
PhysicsServer2D.body_reset_mass_properties(get_rid())

3D uses a similar API.

Edit: Now can be set in RigidBody2D and RigidBody3D nodes as well with properties:
center_of_mass_mode: set to Auto or Custom
center_of_mass: custom center of mass (relative to the body's center)

Custom inertia:
Can be set through the physics server using:
PhysicsServer2D.body_set_param(get_rid(), PhysicsServer2D.BODY_PARAM_INERTIA, inertia)
Can be reset through the physics server using:
PhysicsServer2D.body_reset_mass_properties(get_rid())

Can be also set on rigid body nodes through the inertia property, with 0 meaning default calculated inertia.

3D has a similar API, although it's also possible to setup a custom inertia on specific axes, and set the other ones to 0 in order to keep the default computation. That's useful to make a body rotate more or less easily on a specific axis, without affecting other axes.

Full list of changes:
-Added support for custom inertia and center of mass in 3D
-Added support for custom center of mass in 2D
-Calculated center of mass from shapes in 2D (same as in 3D)
-Updated forces and joints to use center of mass in 2D
-Fixed mass properties calculation with disabled shapes in 2D/3D
-Removed first_integration which is not used in 2D and doesn't seem to make a lot of sense (prevents omit_force_integration to work during the first frame)
-Support for custom inertia on different axes for RigidBody3D
-mass property uses a more reasonable slider range by default (max 1000), with the possibility to set higher values

Closes #godotengine/godot-proposals#945
Fixes #12353
Supersedes #29867
Addresses issue from #49185 (review)

@pouleyKetchoupp
Copy link
Contributor Author

Rebased and added the possibility to set a custom center of mass in RigidBody2D/RigidBody3D nodes.

Changes:
-Added support for custom inertia and center of mass in 3D
-Added support for custom center of mass in 2D
-Calculated center of mass from shapes in 2D (same as in 3D)
-Fixed mass properties calculation with disabled shapes in 2D/3D
-Removed first_integration which is not used in 2D and doesn't seem to
make a lot of sense (prevents omit_force_integration to work during the
first frame)
-Support for custom inertia on different axes for RigidBody3D
@pouleyKetchoupp pouleyKetchoupp force-pushed the center-of-mass-calculation branch from 3b04109 to 82ea2a7 Compare September 6, 2021 17:20
@pouleyKetchoupp pouleyKetchoupp merged commit b277c88 into godotengine:master Sep 6, 2021
@pouleyKetchoupp pouleyKetchoupp deleted the center-of-mass-calculation branch September 6, 2021 18:31
@Zoomulator
Copy link

Zoomulator commented Sep 9, 2021

Could this be cherry-picked to 3.x by setting the default center_of_mass to use Custom, Vector3(0,0,0) to avoid breaking compatibility?

@pouleyKetchoupp
Copy link
Contributor Author

@Zoomulator Ok, it should be possible but I need to check what can be safely backported as there are changes in the way things are calculated in the physics server itself.

@Zoomulator
Copy link

@pouleyKetchoupp I worried that there might be adjacent changes in 4.0.
I'd greatly appreciate it if you gave it a try, if it's not too much trouble! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Center of mass for RigidBody2D
3 participants