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