+2 votes

I am trying to make a game where the player can place items on a raft arbitrarily which should impact it's center of gravity.

The raft is a RigidBody2D with a single collision object. I can iterate through the raft and the items on the raft to calculate the point where the center of gravity should be set to. Is there either a way to get this to happen automatically, or a function that will allow me to set the CoG?

Alternatively, I suppose I could use an extra node to offset all the RigidBody's children in the opposite direction, but that seems gross and I'm concerned the movement of the collision object might cause the CoG to change.

in Engine by (103 points)

The RigidBodies Center of Gravity will indeed change if you put another RigidBody on it. Like you expect it in the real world. This is because the items will push with their gravity force on the rafts rigidbody. Just be sure to define the Rigidbody mass values accordingly.

I've some buoyancy code with damping in this demo here:
https://godotdevelopers.org/forum/discussion/18499/tutorial-multi-touch-controls-demo-project

Just in case you need code for the floating raft. That demo is for 2.1.4, but I can provide it also for 3 alpha if required.

Thanks for your comment!

Are you saying that I can add other rigid bodies as children, and they'll effect the center of mass of the parent - or just that rigid bodies interacting can have the same effect?

To clarify, my goal is that the items on the raft be fixed to the raft, such that I move the overall raft to move all the items (no sliding around).

It doesn't seem to matter physics-wise if other RigidBodies are children or not. They effect the center of mass of the RigidBody they're physically on. But they're "loose".

If you want to prevent sliding around, you can experiment joining the bodies with "Joints".

But if you already found a satisfying workaround then you may as well stick with that. Joints/Rigidbodies/Collision shapes can be tricky to handle and may unnecessarily consume cpu time.

1 Answer

+4 votes
Best answer

It seems a RigidBody2D's center of gravity is always at the origin of the RigidBody2D, so instead of setting the CG directly you have to offset the shape(s) of the body. In your case maybe you could calculate the new CG, move the shape so it matches the wanted CG position and then move the RigidBody2D an opposite but equal amount so the shape ends up back in it's original world position.

by (1,554 points)
selected by

Thanks, I tried hacking this in and it works for now!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.