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

Center of mass for RigidBody2D #12353

Closed
Tracked by #45334
Xrayez opened this issue Oct 23, 2017 · 13 comments · Fixed by #49610
Closed
Tracked by #45334

Center of mass for RigidBody2D #12353

Xrayez opened this issue Oct 23, 2017 · 13 comments · Fixed by #49610

Comments

@Xrayez
Copy link
Contributor

Xrayez commented Oct 23, 2017

Operating system or device, Godot version, GPU Model and driver (if graphics related):
Godot 3.0, 3.1, 3.2, 4.0, 4.1

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:

broken-center

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):

somewhat-broken-center

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:

  1. Create RigidBody2D.
  2. Attach any collision shape to it.
  3. Set transform for collision shape so it's visually away from the RigidBody2D's origin.
  4. See how it behaves.

Link to minimal example project:
center-of-mass-2d.zip

@akien-mga akien-mga added this to the 3.0 milestone Oct 23, 2017
@raymoo
Copy link
Contributor

raymoo commented Oct 24, 2017

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.

@raymoo
Copy link
Contributor

raymoo commented Oct 24, 2017

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.

@Xrayez
Copy link
Contributor Author

Xrayez commented Oct 24, 2017

@raymoo Yeah, and having the ability to set center of mass manually would be a partial solution to simulate variable density I suppose.

@razcore-rad
Copy link
Contributor

razcore-rad commented Dec 25, 2017

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

@akien-mga
Copy link
Member

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.

@Xrayez
Copy link
Contributor Author

Xrayez commented Dec 16, 2018

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.

@akien-mga
Copy link
Member

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.

@akien-mga akien-mga modified the milestones: 3.1, 3.2 Jan 19, 2019
@Zylann
Copy link
Contributor

Zylann commented Apr 14, 2019

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?

@Xrayez
Copy link
Contributor Author

Xrayez commented Apr 15, 2019

@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)

@abakobo
Copy link

abakobo commented May 6, 2019

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.

@Maveyyl
Copy link

Maveyyl commented Mar 31, 2020

3.2 has been out, any news about this? :) being able to just offset it manually would already do wonders.

@Xrayez
Copy link
Contributor Author

Xrayez commented Mar 31, 2020

@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).

@Maveyyl
Copy link

Maveyyl commented Mar 31, 2020

@Xrayez Nice! I'll stick to my dirty hacks and wait patiently for 4.1. :)

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