0 votes

Have a scene with a RigidBody, this RigidBody contains a MeshInstance. As I understand it, you can (should) not control the RigidBody directly, as it is controled by the physics engine. What about the MeshInstance inside the RigidBody? In my project I can control it directly (rotation), while the physics engine still controls the parent RigidBody. Is it perfectly ok to do so?

in Engine by (62 points)

1 Answer

0 votes
Best answer

You can override a rigid body's behavior with integrate_forces or simply in physics process but only to a certain extent. I have a rigid body character which is free falling and I can control it's movement. I also have a 'controller' type node as a child of this rigid body, and a 'meshes' node parented to the controller. All of my character's movements (aside from basic position/rotation in 3d space) are animated inside of the controller.

Eg, when you want to move the falling rigid body to the left, you can use apply_impulse with a left direction vector in physics process. In addition, you can animate the mesh inside the rigid body (ie, parented to it) at the same time, thus creating the illusion of, say, the player mesh rotating left as its rigid body parent is moving left also.

by (433 points)
selected by

Thats what I do, controling the RigidBody inside "integrateforces" (set its angularvelocity), while simultaniously controling the child MeshInstance by setting it's "rotatey" property via "physicsprocess" of the RigidBody:

func _integrate_forces(state):
     if freed:
          freed = false
          state.set_angular_velocity(Vector3(0.0, ang_vel, 0.0))

func _physics_process(delta):
     if moved:
        ang_vel = delta_angle / delta

Works perfect in one test scene with only one RigidBody in it. My concern was, that this is not a good way and maybe s.th. will break with say more RigidBodys of that kind. My thoughts are: After all the Mesh is a child of RigidBody and as such also indirectly controled by the physics engine. But one shouldn't control things directly that are controled by physics. Is it nevertheless valid, to control it directly?

Well if it works for one rigid body, why would it not work for more? Also, ultimately you will want to limit the number of active rigid bodies is your scene for sake of performance anyway. Rigid body physics works on the rigid body node itself, and indirectly on everything that's parented to it. You can animate the children to your heart's content and it will work because that's how the engine was built - you will not 'break' anything. But performance will be impacted so you will need to find a way to balance things out.

Thanks a lot for clarifying!

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.