rigid_body.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. .. _doc_rigid_body:
  2. Using RigidBody
  3. ===============
  4. What is a rigid body?
  5. ---------------------
  6. A rigid body is one that is directly controlled by the physics engine in order to simulate the behavior of physical objects.
  7. In order to define the shape of the body, it must have one or more :ref:`Shape3D <class_Shape3D>` objects assigned. Note that setting the position of these shapes will affect the body's center of mass.
  8. How to control a rigid body
  9. ---------------------------
  10. A rigid body's behavior can be altered by setting its properties, such as mass and weight.
  11. A physics material needs to be added to the rigid body to adjust its friction and bounce,
  12. and set if it's absorbent and/or rough. These properties can be set in the Inspector or via code.
  13. See :ref:`RigidBody3D <class_RigidBody3D>` and :ref:`PhysicsMaterial <class_PhysicsMaterial>` for
  14. the full list of properties and their effects.
  15. There are several ways to control a rigid body's movement, depending on your desired application.
  16. If you only need to place a rigid body once, for example to set its initial location, you can use the methods provided by the :ref:`Node3D <class_Node3D>` node, such as ``set_global_transform()`` or ``look_at()``. However, these methods cannot be called every frame or the physics engine will not be able to correctly simulate the body's state.
  17. As an example, consider a rigid body that you want to rotate so that it points towards another object. A common mistake when implementing this kind of behavior is to use ``look_at()`` every frame, which breaks the physics simulation. Below, we'll demonstrate how to implement this correctly.
  18. The fact that you can't use ``set_global_transform()`` or ``look_at()`` methods doesn't mean that you can't have full control of a rigid body. Instead, you can control it by using the ``_integrate_forces()`` callback. In this method, you can add *forces*, apply *impulses*, or set the *velocity* in order to achieve any movement you desire.
  19. The "look at" method
  20. --------------------
  21. As described above, using the Node3D's ``look_at()`` method can't be used each frame to follow a target.
  22. Here is a custom ``look_at()`` method that will work reliably with rigid bodies:
  23. .. tabs::
  24. .. code-tab:: gdscript GDScript
  25. extends RigidBody3D
  26. func look_follow(state, current_transform, target_position):
  27. var up_dir = Vector3(0, 1, 0)
  28. var cur_dir = current_transform.basis * Vector3(0, 0, 1)
  29. var target_dir = (target_position - current_transform.origin).normalized()
  30. var rotation_angle = acos(cur_dir.x) - acos(target_dir.x)
  31. state.angular_velocity = up_dir * (rotation_angle / state.step)
  32. func _integrate_forces(state):
  33. var target_position = $my_target_node3d_node.global_transform.origin
  34. look_follow(state, global_transform, target_position)
  35. .. code-tab:: csharp
  36. using Godot;
  37. public partial class MyRigidBody3D : RigidBody3D
  38. {
  39. private void LookFollow(PhysicsDirectBodyState state, Transform3D currentTransform, Vector3 targetPosition)
  40. {
  41. var upDir = new Vector3(0, 1, 0);
  42. var curDir = currentTransform.Basis * new Vector3(0, 0, 1);
  43. var targetDir = (targetPosition - currentTransform.Origin).Normalized();
  44. var rotationAngle = Mathf.Acos(curDir.X) - Mathf.Acos(targetDir.X);
  45. state.SetAngularVelocity(upDir * (rotationAngle / state.GetStep()));
  46. }
  47. public override void _IntegrateForces(PhysicsDirectBodyState state)
  48. {
  49. var targetPosition = GetNode<Node3D>("MyTargetNode3DNode").GetGlobalTransform().Origin;
  50. LookFollow(state, GetGlobalTransform(), targetPosition);
  51. }
  52. }
  53. This method uses the rigid body's ``angular_velocity`` property to rotate the body. It first calculates the difference between the current and desired angle and then adds the velocity needed to rotate by that amount in one frame's time.
  54. .. note:: This script will not work with rigid bodies in *character mode* because then, the body's rotation is locked. In that case, you would have to rotate the attached mesh node instead using the standard Node3D methods.