Possible to control MeshInstance inside RigidBoby?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Maxpilot

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?

:bust_in_silhouette: Reply From: Macryc

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.

Thats what I do, controling the RigidBody inside “_integrate_forces” (set its angular_velocity), while simultaniously controling the child MeshInstance by setting it’s “rotate_y” property via “_physics_process” 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?

Maxpilot | 2020-03-22 12:49

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.

Macryc | 2020-03-22 15:18

Thanks a lot for clarifying!

Maxpilot | 2020-03-22 15:26