Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Utilisation de RigidBody
Qu'est-ce qu'un corps rigide ?
A rigid body is one that is directly controlled by the physics engine in order to simulate the behavior of physical objects. In order to define the shape of the body, it must have one or more Shape3D objects assigned. Note that setting the position of these shapes will affect the body's center of mass.
Comment contrôler un corps rigide
A rigid body's behavior can be altered by setting its properties, such as mass and weight. A physics material needs to be added to the rigid body to adjust its friction and bounce, and set if it's absorbent and/or rough. These properties can be set in the Inspector or via code. See RigidBody3D and PhysicsMaterial for the full list of properties and their effects.
Il y a plusieurs façons de contrôler le mouvement d'un corps rigide, selon l'application désirée.
Si vous n'avez besoin de placer qu'une seule fois un corps rigide, par exemple pour définir son emplacement initial, vous pouvez utiliser les méthodes fournies par le nœud Node3D, telles que set_global_transform()
ou look_at()
. Cependant, ces méthodes ne peuvent pas être appelées toutes les images ou le moteur physique ne sera pas capable de simuler correctement l'état du corps. Par exemple, considérez un corps rigide que vous voulez faire pivoter pour qu'il pointe vers un autre objet. Une erreur courante lors de l'implémentation de ce type de comportement est d'utiliser look_at()
chaque image, ce qui casse la simulation physique. Ci-dessous, nous allons montrer comment implémenter correctement ceci.
Le fait que vous ne puissiez pas utiliser les méthodes set_global_transform()
ou look_at()
ne signifie pas que vous ne pouvez pas avoir un contrôle total sur un corps rigide. Au lieu de cela, vous pouvez le contrôler en utilisant le callback _integrate_forces()
. Dans cette méthode, vous pouvez ajouter des forces, appliquer des impulsions, ou régler la vitesse afin d'obtenir le mouvement que vous souhaitez.
La méthode "look at"
As described above, using the Node3D's look_at()
method can't be used each frame to follow a target.
Here is a custom look_at()
method called look_follow()
that will work with rigid bodies:
extends RigidBody3D
var speed: float = 0.1
func look_follow(state: PhysicsDirectBodyState3D, current_transform: Transform3D, target_position: Vector3) -> void:
var forward_local_axis: Vector3 = Vector3(1, 0, 0)
var forward_dir: Vector3 = (current_transform.basis * forward_local_axis).normalized()
var target_dir: Vector3 = (target_position - current_transform.origin).normalized()
var local_speed: float = clampf(speed, 0, acos(forward_dir.dot(target_dir)))
if forward_dir.dot(target_dir) > 1e-4:
state.angular_velocity = local_speed * forward_dir.cross(target_dir) / state.step
func _integrate_forces(state):
var target_position = $my_target_node3d_node.global_transform.origin
look_follow(state, global_transform, target_position)
using Godot;
public partial class MyRigidBody3D : RigidBody3D
{
private float _speed = 0.1f;
private void LookFollow(PhysicsDirectBodyState3D state, Transform3D currentTransform, Vector3 targetPosition)
{
Vector3 forwardLocalAxis = new Vector3(1, 0, 0);
Vector3 forwardDir = (currentTransform.Basis * forwardLocalAxis).Normalized();
Vector3 targetDir = (targetPosition - currentTransform.Origin).Normalized();
float localSpeed = Mathf.Clamp(_speed, 0.0f, Mathf.Acos(forwardDir.Dot(targetDir)));
if (forwardDir.Dot(targetDir) > 1e-4)
{
state.AngularVelocity = forwardDir.Cross(targetDir) * localSpeed / state.Step;
}
}
public override void _IntegrateForces(PhysicsDirectBodyState3D state)
{
Vector3 targetPosition = GetNode<Node3D>("MyTargetNode3DNode").GlobalTransform.Origin;
LookFollow(state, GlobalTransform, targetPosition);
}
}
This method uses the rigid body's angular_velocity
property to rotate the body.
The axis to rotate around is given by the cross product between the current forward direction and the direction one wants to look in.
The clamp
is a simple method used to prevent the amount of rotation from going past the direction which is wanted to be looked in,
as the total amount of rotation needed is given by the arccosine of the dot product.
This method can be used with axis_lock_angular_*
as well. If more precise control is needed, solutions such as ones relying on Quaternion may be required,
as discussed in Utiliser les transformations 3D.