Operating system or device, Godot version, GPU Model and driver (if graphics related):
Godot 3.0, 3.1, 3.2, 4.0, 4.1
There's a PR which fixes this: #29867.
Issue description:
The center of mass is always at the Node's origin apparently. If the shapes are offset by either transformation matrix or simply shifted by coordinates away from origin, the new center of mass formed by shapes is not taken into account. So the only way to fix this is by moving shapes to the RigidBody2D's origin and repositioning of RigidBody2D whenever shapes are changed (which is not recommended AFAIK in terms of collision checking). Here's an example:
EDIT: these three shapes are actually individual ConvexPolygon2D shapes
Now, inspired by PR #7426 by @m4nu3lf (3D case), I've made some progress computing center of mass successfully based on shapes transformations, but my knowledge of physics is basic, here's a better result that I've made (compare middle box):
This feature/bug was proposed/described here several times, but for some reason or another it was mistakenly ignored or issues were closed as fixed, see #7136 and #8638.
Steps to reproduce:
Link to minimal example project:
center-of-mass-2d.zip
Center of mass cannot be determined from just the shapes because the density of an object may vary. For example, the center of mass of a front-engine car is typically not in the middle of the car but closer to the front.
If we assume that density is positive though (like in models of real life), the center of mass should be somewhere (and could be anywhere) inside the convex hull of the shapes.
I forgot to mention that my point is that sometimes you would want the center of mass to not just assume constant density. If this feature is added at all, it should be optional.
@raymoo Yeah, and having the ability to set center of mass manually would be a partial solution to simulate variable density I suppose.
The better option is to assume uniform density + have an input option for a texture density. I may look at this center of mass issue later, at least calculating the center of mass based on uniform density
Not critical for the upcoming 3.0 release, so moving to the next milestone. A fix can still be cherry-picked for 3.0.x maintenance releases once available in the master branch.
Is this issue going to be moved to 3.2 again? I need this issue to be fixed eventually so I might start digging through code and physics more thoroughly...
Does it only depend on the center of mass or something else as well? I imagine the different center of mass affects angular velocity/inertia for each point of the rigid body that needs some fancy matrix calculation...
I'll appreciate any help/pointers regarding where I should start looking at this.
Is this issue going to be moved to 3.2 again?
Sadly yes, we're running out of time for such issues. I'd suggest you try to discuss it with @reduz on IRC to have pointers on where to look into.
Adding my voice to this, I would like to be able to set the center of mass manually, at least from a script. The use case for that is a game where you can build a vehicle by combining blocks of variable mass, and that affects where you want the center of mass to be. That also means that center of mass can change dynamically without having to offset every shape.
Note: I mean this both for 2D and 3D. Does that require another issue?
@Zylann yeah, having an ability to manually set center of mass is another feature, the current problem is actually trying to resolve proper impulse response between rigid bodies with different distribution of mass as it seems, whatever the center of mass.
I mean, I think this could be solved by finding proper geometric center of mass (centroid) whenever shapes are changed first (I think I've managed to do this here similarly to how 3D version does it), and only then apply additional offset relative to actual center of mass. That way you can always revert the center of mass to shapes' centroid offset anytime (if this is the right way to simulate distribution of mass at all)
Yes, center of mass and origin could definitely be different so the center of mass can be modified/recomputed without changing the origin.
Beign alble to compute the center of mass/moment of inertia for the combination of one body's shapes (with different densities) is how things are usually done in physics simulation.
3.2 has been out, any news about this? :) being able to just offset it manually would already do wonders.
@Maveyyl #29867 is there to fix it and I've tested it myself in my own project, but unfortunately it wasn't merged for 3.2. Expect this issue to be here for as long as 4.1 comes out I guess (because allegedly that's when @reduz plans to rework the 2D physics engine).
@Xrayez Nice! I'll stick to my dirty hacks and wait patiently for 4.1. :)
Most helpful comment
Yes, center of mass and origin could definitely be different so the center of mass can be modified/recomputed without changing the origin.
Beign alble to compute the center of mass/moment of inertia for the combination of one body's shapes (with different densities) is how things are usually done in physics simulation.