bevy_tnua/util/
command_impl_helpers.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use bevy::prelude::*;

use bevy_tnua_physics_integration_layer::data_for_backends::TnuaVelChange;
use bevy_tnua_physics_integration_layer::math::{AdjustPrecision, Float, Quaternion, Vector3};

use crate::{TnuaActionContext, TnuaBasisContext};

/// Helper trait for implementing basis and actions.
///
/// Methods of this trait typically return a [`TnuaVelChange`] that can be added to
/// [`motor.lin`](crate::TnuaMotor::lin) or [`motor.ang`](crate::TnuaMotor::ang).
pub trait MotionHelper {
    fn frame_duration(&self) -> Float;
    fn up_direction(&self) -> Dir3;
    fn gravity(&self) -> Vector3;
    fn position(&self) -> Vector3;
    fn velocity(&self) -> Vector3;
    fn rotation(&self) -> Quaternion;
    fn angvel(&self) -> Vector3;

    /// A force for cancelling out the gravity.
    fn negate_gravity(&self) -> TnuaVelChange {
        TnuaVelChange::acceleration(-self.gravity())
    }

    /// A force for setting the velocity to the desired velocity, with acceleration constraints.
    ///
    /// The `dimlim` parameter can be used to only set the velocity on certain axis/plane.
    fn adjust_velocity(
        &self,
        target: Vector3,
        acceleration: Float,
        dimlim: impl Fn(Vector3) -> Vector3,
    ) -> TnuaVelChange {
        let delta = dimlim(target - self.velocity());
        let allowed_this_frame = acceleration * self.frame_duration();
        if delta.length_squared() <= allowed_this_frame.powi(2) {
            TnuaVelChange::boost(delta)
        } else {
            TnuaVelChange::acceleration(delta.normalize_or_zero() * acceleration)
        }
    }

    /// A force for setting the vertical velocity to the desired velocity, with acceleration constraints.
    fn adjust_vertical_velocity(&self, target: Float, acceleration: Float) -> TnuaVelChange {
        let up_vector = self.up_direction().adjust_precision();
        self.adjust_velocity(up_vector * target, acceleration, |v| {
            v.project_onto_normalized(up_vector)
        })
    }

    /// A force for setting the horizontal velocity to the desired velocity, with acceleration constraints.
    fn adjust_horizontal_velocity(&self, target: Vector3, acceleration: Float) -> TnuaVelChange {
        self.adjust_velocity(target, acceleration, |v| {
            v.reject_from_normalized(self.up_direction().adjust_precision())
        })
    }

    /// Calculate the rotation around `up_direction` required to rotate the character from the
    /// current forward to `desired_forward`.
    fn turn_to_direction(&self, desired_forward: Dir3, up_direction: Dir3) -> TnuaVelChange {
        let up_vector = up_direction.adjust_precision();
        let current_forward = self.rotation().mul_vec3(Vector3::NEG_Z);
        let rotation_along_up_axis = crate::util::rotation_arc_around_axis(
            up_direction,
            current_forward,
            desired_forward.adjust_precision(),
        )
        .unwrap_or(0.0);
        let desired_angvel = rotation_along_up_axis / self.frame_duration();
        let existing_angvel = self.angvel().dot(up_vector);
        let torque_to_turn = desired_angvel - existing_angvel;
        TnuaVelChange::boost(torque_to_turn * up_vector)
    }

    /// A force for stopping the character right before reaching a certain point.
    ///
    /// Please note that unlike most [`MotionHelper`] methods that "stack" on impulses and forces
    /// already applied, this one needs to be able to "cancel" the previous changes (on the
    /// relevant axis, at least) which is why it has a `current_vel_change` argument. Due to that,
    /// this must be the **last** velchange applied to the motor (at least, to it's
    /// [`lin`](crate::TnuaMotor::lin) field, on the axis of `direction`)
    ///
    /// `direction` is the direction _from_ the character _toward_ that point. So, for example, in
    /// order to stop a character climbing a ladder when reaching the top of the ladder, `stop_at`
    /// needs to be the coordinates of the top of the ladder while `direction` needs to be
    /// `Dir3::Y` (the UP direction)
    ///
    /// Velocities perpendicular to the `direction` will not be affected.
    fn hard_stop(
        &self,
        direction: Dir3,
        stop_at: Vector3,
        current_vel_change: &TnuaVelChange,
    ) -> TnuaVelChange {
        let expected_velocity =
            self.velocity() + current_vel_change.calc_mean_boost(self.frame_duration());
        let expected_velocity_in_direction = expected_velocity.dot(direction.adjust_precision());
        if expected_velocity_in_direction <= 0.0 {
            return TnuaVelChange::default();
        }
        let distance_in_direction = (stop_at - self.position()).dot(direction.adjust_precision());
        let max_allowed_velocity = distance_in_direction / self.frame_duration();
        let velocity_to_cut = expected_velocity_in_direction - max_allowed_velocity;
        if 0.0 < velocity_to_cut {
            TnuaVelChange::boost(velocity_to_cut * -direction.adjust_precision())
        } else {
            TnuaVelChange::default()
        }
    }
}

impl MotionHelper for TnuaBasisContext<'_> {
    fn frame_duration(&self) -> Float {
        self.frame_duration
    }

    fn up_direction(&self) -> Dir3 {
        self.up_direction
    }

    fn gravity(&self) -> Vector3 {
        self.tracker.gravity
    }

    fn position(&self) -> Vector3 {
        self.tracker.translation
    }

    fn velocity(&self) -> Vector3 {
        self.tracker.velocity
    }

    fn rotation(&self) -> Quaternion {
        self.tracker.rotation
    }

    fn angvel(&self) -> Vector3 {
        self.tracker.angvel
    }
}

impl MotionHelper for TnuaActionContext<'_> {
    fn frame_duration(&self) -> Float {
        self.frame_duration
    }

    fn up_direction(&self) -> Dir3 {
        self.up_direction
    }

    fn gravity(&self) -> Vector3 {
        self.tracker.gravity
    }

    fn position(&self) -> Vector3 {
        self.tracker.translation
    }

    fn velocity(&self) -> Vector3 {
        self.tracker.velocity
    }

    fn rotation(&self) -> Quaternion {
        self.tracker.rotation
    }

    fn angvel(&self) -> Vector3 {
        self.tracker.angvel
    }
}