veloren_voxygen_anim/character/
run.rs

1use super::{
2    super::{Animation, vek::*},
3    CharacterSkeleton, SkeletonAttr,
4};
5use common::comp::item::{Hands, ToolKind};
6use core::{f32::consts::PI, ops::Mul};
7
8pub struct RunAnimation;
9
10type RunAnimationDependency = (
11    Option<ToolKind>,
12    Option<ToolKind>,
13    (Option<Hands>, Option<Hands>),
14    Vec3<f32>,
15    Vec3<f32>,
16    Vec3<f32>,
17    f32,
18    Vec3<f32>,
19    f32,
20    Option<Vec3<f32>>,
21);
22
23impl Animation for RunAnimation {
24    type Dependency<'a> = RunAnimationDependency;
25    type Skeleton = CharacterSkeleton;
26
27    #[cfg(feature = "use-dyn-lib")]
28    const UPDATE_FN: &'static [u8] = b"character_run\0";
29
30    #[cfg_attr(feature = "be-dyn-lib", export_name = "character_run")]
31
32    fn update_skeleton_inner(
33        skeleton: &Self::Skeleton,
34        (
35            active_tool_kind,
36            second_tool_kind,
37            hands,
38            velocity,
39            orientation,
40            last_ori,
41            global_time,
42            avg_vel,
43            acc_vel,
44            wall,
45        ): Self::Dependency<'_>,
46        anim_time: f32,
47        rate: &mut f32,
48        s_a: &SkeletonAttr,
49    ) -> Self::Skeleton {
50        let mut next = (*skeleton).clone();
51
52        let speed = Vec2::<f32>::from(velocity).magnitude();
53        *rate = 1.0;
54        let impact = (avg_vel.z).max(-8.0);
55        let speednorm = (speed / 9.4).powf(0.65);
56
57        let lab: f32 = 0.6 / s_a.scaler.powf(0.75);
58
59        let footrotl = ((1.0 / (0.5 + (0.5) * ((acc_vel * 1.6 * lab + PI * 1.4).sin()).powi(2)))
60            .sqrt())
61            * ((acc_vel * 1.6 * lab + PI * 1.4).sin());
62
63        let footrotr = ((1.0 / (0.5 + (0.5) * ((acc_vel * 1.6 * lab + PI * 0.4).sin()).powi(2)))
64            .sqrt())
65            * ((acc_vel * 1.6 * lab + PI * 0.4).sin());
66
67        let noisea = (acc_vel * 11.0 + PI / 6.0).sin();
68        let noiseb = (acc_vel * 19.0 + PI / 4.0).sin();
69
70        let back_speed = 2.6;
71
72        let dirside = orientation.xy().dot(velocity.xy()).signum();
73        let foothoril = if dirside > 0.0 {
74            (acc_vel * 1.6 * lab + PI * 1.45).sin() * dirside
75        } else {
76            (acc_vel * back_speed * lab + PI * 1.45).sin() * dirside
77        };
78        let foothorir = if dirside > 0.0 {
79            (acc_vel * 1.6 * lab + PI * (0.45)).sin() * dirside
80        } else {
81            (acc_vel * back_speed * lab + PI * (0.45)).sin() * dirside
82        };
83        let strafeside = orientation
84            .xy()
85            .dot(velocity.xy().rotated_z(PI * -0.5))
86            .signum();
87        let footstrafel = (acc_vel * 1.6 * lab + PI * 1.5).sin() * strafeside;
88        let footstrafer = (acc_vel * 1.6 * lab + PI).sin() * -strafeside;
89
90        let footvertl = if dirside > 0.0 {
91            (acc_vel * 1.6 * lab).sin()
92        } else {
93            (acc_vel * back_speed * lab).sin()
94        };
95        let footvertr = if dirside > 0.0 {
96            (acc_vel * 1.6 * lab + PI).sin()
97        } else {
98            (acc_vel * back_speed * lab + PI).sin()
99        };
100        let footvertsl = (acc_vel * 1.6 * lab).sin();
101        let footvertsr = (acc_vel * 1.6 * lab + PI * 0.5).sin();
102
103        let shortalt = (acc_vel * lab * 3.2 + PI / 1.0).sin();
104        let shortalt2 = (acc_vel * lab * 3.2).sin();
105
106        let short = ((5.0 / (1.5 + 3.5 * ((acc_vel * lab * 1.6 + PI * 0.5).sin()).powi(2))).sqrt())
107            * ((acc_vel * lab * 1.6 + PI * 0.5).sin());
108
109        let side =
110            (velocity.x * -0.098 * orientation.y + velocity.y * 0.098 * orientation.x) * -1.0;
111        let sideabs = side.abs();
112        let ori: Vec2<f32> = Vec2::from(orientation);
113        let last_ori = Vec2::from(last_ori);
114        let tilt = if vek::Vec2::new(ori, last_ori)
115            .map(|o| o.magnitude_squared())
116            .map(|m| m > 0.001 && m.is_finite())
117            .reduce_and()
118            && ori.angle_between(last_ori).is_finite()
119        {
120            ori.angle_between(last_ori).min(0.2)
121                * last_ori.determine_side(Vec2::zero(), ori).signum()
122        } else {
123            0.0
124        } * 1.3;
125
126        let head_look = Vec2::new(
127            (global_time + anim_time / 18.0).floor().mul(7331.0).sin() * 0.2,
128            (global_time + anim_time / 18.0).floor().mul(1337.0).sin() * 0.1,
129        );
130
131        next.head.position = Vec3::new(0.0, s_a.head.0 * 1.5, s_a.head.1 + short * 0.1);
132        next.head.orientation =
133            Quaternion::rotation_z(tilt * -2.5 + head_look.x * 0.2 + short * -0.3 * speednorm)
134                * Quaternion::rotation_x(head_look.y + 0.45 * speednorm + shortalt2 * -0.05);
135        next.head.scale = Vec3::one() * s_a.head_scale;
136
137        next.chest.position = Vec3::new(
138            0.0,
139            s_a.chest.0,
140            s_a.chest.1 + 1.0 * speednorm + shortalt * 1.1,
141        );
142        next.chest.orientation = Quaternion::rotation_x(impact * 0.07)
143            * Quaternion::rotation_z(short * 0.4 * speednorm + tilt * -0.6)
144            * Quaternion::rotation_y(tilt * 2.0 + short * 0.2 * speednorm)
145            * Quaternion::rotation_x(shortalt2 * 0.03 + speednorm * -0.5 + tilt.abs());
146
147        next.belt.position = Vec3::new(0.0, 0.25 + s_a.belt.0, 0.25 + s_a.belt.1);
148        next.belt.orientation = Quaternion::rotation_x(0.1 * speednorm)
149            * Quaternion::rotation_z(short * -0.2 + tilt * -1.1)
150            * Quaternion::rotation_y(tilt * 0.5);
151
152        next.back.position = Vec3::new(0.0, s_a.back.0, s_a.back.1);
153        next.back.orientation =
154            Quaternion::rotation_x(-0.05 + short * 0.02 + noisea * 0.02 + noiseb * 0.02)
155                * Quaternion::rotation_y(foothorir * 0.35 * speednorm.powi(2));
156
157        next.shorts.position = Vec3::new(0.0, 0.65 + s_a.shorts.0, 0.65 * speednorm + s_a.shorts.1);
158        next.shorts.orientation = Quaternion::rotation_x(0.2 * speednorm)
159            * Quaternion::rotation_z(short * -0.9 * speednorm + tilt * -1.5)
160            * Quaternion::rotation_y(tilt * 0.7 + short * 0.08);
161
162        next.hand_l.position = Vec3::new(
163            -s_a.hand.0 * 1.2 - foothorir * 1.3 * speednorm
164                + (foothoril.abs().powi(2) - 0.5) * speednorm * 4.0,
165            s_a.hand.1 * 1.3 + foothorir * -7.0 * speednorm.powi(2) * (1.0 - sideabs),
166            s_a.hand.2 - foothorir * 2.75 * speednorm
167                + foothoril.abs().powi(3) * speednorm.powi(2) * 8.0,
168        );
169        next.hand_l.orientation =
170            Quaternion::rotation_x(
171                0.6 * speednorm + (footrotr * -1.5 + 0.5) * speednorm.powi(2) * (1.0 - sideabs),
172            ) * Quaternion::rotation_y(footrotr * 0.4 * speednorm + PI * 0.07);
173
174        next.hand_r.position = Vec3::new(
175            s_a.hand.0 * 1.2 + foothoril * 1.3 * speednorm
176                - (foothorir.abs().powi(2) - 0.5) * speednorm * 4.0,
177            s_a.hand.1 * 1.3 + foothoril * -7.0 * speednorm.powi(2) * (1.0 - sideabs),
178            s_a.hand.2 - foothoril * 2.75 * speednorm
179                + foothorir.abs().powi(3) * speednorm.powi(2) * 8.0,
180        );
181        next.hand_r.orientation =
182            Quaternion::rotation_x(
183                0.6 * speednorm + (footrotl * -1.5 + 0.5) * speednorm.powi(2) * (1.0 - sideabs),
184            ) * Quaternion::rotation_y(footrotl * -0.4 * speednorm - PI * 0.07);
185
186        next.foot_l.position = Vec3::new(
187            -s_a.foot.0 + footstrafel * sideabs * 7.0 + tilt * -10.0,
188            s_a.foot.1 + (1.0 - sideabs) * (-1.5 * speednorm + foothoril * -10.0 * speednorm),
189            s_a.foot.2
190                + (1.0 - sideabs) * (1.25 + (footvertl * -5.0).max(-1.0)) * speednorm
191                + side * ((footvertsl * 1.5).max(-1.0)),
192        );
193        next.foot_l.orientation = Quaternion::rotation_x(
194            (1.0 - sideabs) * (foothoril + 0.3 * (1.0 - sideabs)) * -1.5 * speednorm
195                + sideabs * -0.5,
196        ) * Quaternion::rotation_y(
197            tilt * -0.5 + side * (foothoril * 0.3) + footstrafer * side * 0.5,
198        ) * Quaternion::rotation_z(
199            side * 1.3 * orientation.xy().dot(velocity.xy() / (speed + 0.01)),
200        );
201
202        next.foot_r.position = Vec3::new(
203            s_a.foot.0 + footstrafer * sideabs * 7.0 + tilt * -10.0,
204            s_a.foot.1 + (1.0 - sideabs) * (-1.5 * speednorm + foothorir * -10.0 * speednorm),
205            s_a.foot.2
206                + (1.0 - sideabs) * (1.25 + (footvertr * -5.0).max(-1.0)) * speednorm
207                + side * ((footvertsr * -1.5).max(-1.0)),
208        );
209        next.foot_r.orientation = Quaternion::rotation_x(
210            (1.0 - sideabs) * (foothorir + 0.3 * (1.0 - sideabs)) * -1.5 * speednorm
211                + sideabs * -0.5,
212        ) * Quaternion::rotation_y(
213            tilt * -0.5 + side * (foothorir * 0.3) - footstrafer * side * 0.5,
214        ) * Quaternion::rotation_z(
215            side * 1.3 * orientation.xy().dot(velocity.xy() / (speed + 0.01)),
216        );
217
218        next.shoulder_l.position = Vec3::new(-s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
219        next.shoulder_l.orientation =
220            Quaternion::rotation_x(short * 0.15 * speednorm + (footrotl * 0.5 + 0.5) * speednorm);
221        next.shoulder_l.scale = Vec3::one() * 1.1;
222
223        next.shoulder_r.position = Vec3::new(s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
224        next.shoulder_r.orientation =
225            Quaternion::rotation_x(short * -0.15 * speednorm + (footrotr * 0.5 + 0.5) * speednorm);
226        next.shoulder_r.scale = Vec3::one() * 1.1;
227
228        next.glider.position = Vec3::new(0.0, 0.0, 10.0);
229        next.glider.scale = Vec3::one() * 0.0;
230
231        next.do_tools_on_back(hands, active_tool_kind, second_tool_kind);
232
233        next.do_hold_lantern(s_a, anim_time, acc_vel, speednorm, impact, tilt);
234
235        next.torso.position = Vec3::new(0.0, 0.0, 0.0);
236
237        if wall.is_some_and(|e| e.y > 0.5) {
238            let push = (1.0 - orientation.x.abs()).powi(2);
239            let right_sub = -(orientation.x).min(0.0);
240            let left_sub = (orientation.x).max(0.0);
241            next.hand_l.position = Vec3::new(
242                -s_a.hand.0,
243                s_a.hand.1,
244                s_a.hand.2 + push * 5.0 + 2.0 * left_sub,
245            );
246            next.hand_r.position = Vec3::new(
247                s_a.hand.0,
248                s_a.hand.1,
249                s_a.hand.2 + push * 5.0 + 2.0 * right_sub,
250            );
251            next.hand_l.orientation =
252                Quaternion::rotation_x(push * 2.0 + footrotr * -0.2 * right_sub)
253                    * Quaternion::rotation_y(1.0 * left_sub)
254                    * Quaternion::rotation_z(2.5 * left_sub + 1.0 * right_sub);
255            next.hand_r.orientation =
256                Quaternion::rotation_x(push * 2.0 + footrotl * -0.2 * left_sub)
257                    * Quaternion::rotation_y(-1.0 * right_sub)
258                    * Quaternion::rotation_z(-2.5 * right_sub - 1.0 * left_sub);
259        } else if wall.is_some_and(|e| e.y < -0.5) {
260            let push = (1.0 - orientation.x.abs()).powi(2);
261            let right_sub = (orientation.x).max(0.0);
262            let left_sub = -(orientation.x).min(0.0);
263            next.hand_l.position = Vec3::new(
264                -s_a.hand.0,
265                s_a.hand.1,
266                s_a.hand.2 + push * 5.0 + 2.0 * left_sub,
267            );
268            next.hand_r.position = Vec3::new(
269                s_a.hand.0,
270                s_a.hand.1,
271                s_a.hand.2 + push * 5.0 + 2.0 * right_sub,
272            );
273            next.hand_l.orientation =
274                Quaternion::rotation_x(push * 2.0 + footrotr * -0.2 * right_sub)
275                    * Quaternion::rotation_y(1.0 * left_sub)
276                    * Quaternion::rotation_z(2.5 * left_sub + 1.0 * right_sub);
277            next.hand_r.orientation =
278                Quaternion::rotation_x(push * 2.0 + footrotl * -0.2 * left_sub)
279                    * Quaternion::rotation_y(-1.0 * right_sub)
280                    * Quaternion::rotation_z(-2.5 * right_sub - 1.0 * left_sub);
281        } else if wall.is_some_and(|e| e.x < -0.5) {
282            let push = (1.0 - orientation.y.abs()).powi(2);
283            let right_sub = -(orientation.y).min(0.0);
284            let left_sub = (orientation.y).max(0.0);
285            next.hand_l.position = Vec3::new(
286                -s_a.hand.0,
287                s_a.hand.1,
288                s_a.hand.2 + push * 5.0 + 2.0 * left_sub,
289            );
290            next.hand_r.position = Vec3::new(
291                s_a.hand.0,
292                s_a.hand.1,
293                s_a.hand.2 + push * 5.0 + 2.0 * right_sub,
294            );
295            next.hand_l.orientation =
296                Quaternion::rotation_x(push * 2.0 + footrotr * -0.2 * right_sub)
297                    * Quaternion::rotation_y(1.0 * left_sub)
298                    * Quaternion::rotation_z(2.5 * left_sub + 1.0 * right_sub);
299            next.hand_r.orientation =
300                Quaternion::rotation_x(push * 2.0 + footrotl * -0.2 * left_sub)
301                    * Quaternion::rotation_y(-1.0 * right_sub)
302                    * Quaternion::rotation_z(-2.5 * right_sub - 1.0 * left_sub);
303        } else if wall.is_some_and(|e| e.x > 0.5) {
304            let push = (1.0 - orientation.y.abs()).powi(2);
305            let right_sub = (orientation.y).max(0.0);
306            let left_sub = -(orientation.y).min(0.0);
307            next.hand_l.position = Vec3::new(
308                -s_a.hand.0,
309                s_a.hand.1,
310                s_a.hand.2 + push * 5.0 + 2.0 * left_sub,
311            );
312            next.hand_r.position = Vec3::new(
313                s_a.hand.0,
314                s_a.hand.1,
315                s_a.hand.2 + push * 5.0 + 2.0 * right_sub,
316            );
317            next.hand_l.orientation =
318                Quaternion::rotation_x(push * 2.0 + footrotr * -0.2 * right_sub)
319                    * Quaternion::rotation_y(1.0 * left_sub)
320                    * Quaternion::rotation_z(2.5 * left_sub + 1.0 * right_sub);
321            next.hand_r.orientation =
322                Quaternion::rotation_x(push * 2.0 + footrotl * -0.2 * left_sub)
323                    * Quaternion::rotation_y(-1.0 * right_sub)
324                    * Quaternion::rotation_z(-2.5 * right_sub - 1.0 * left_sub);
325        };
326
327        next
328    }
329}