veloren_voxygen_anim/biped_large/
run.rs

1use super::{
2    super::{Animation, vek::*},
3    BipedLargeSkeleton, SkeletonAttr,
4};
5use common::comp::item::ToolKind;
6use core::{f32::consts::PI, ops::Mul};
7
8pub struct RunAnimation;
9
10type RunAnimationDependency = (
11    Option<ToolKind>,
12    Option<ToolKind>,
13    Vec3<f32>,
14    Vec3<f32>,
15    Vec3<f32>,
16    f32,
17    Vec3<f32>,
18    f32,
19);
20impl Animation for RunAnimation {
21    type Dependency<'a> = RunAnimationDependency;
22    type Skeleton = BipedLargeSkeleton;
23
24    #[cfg(feature = "use-dyn-lib")]
25    const UPDATE_FN: &'static [u8] = b"biped_large_run\0";
26
27    #[cfg_attr(feature = "be-dyn-lib", export_name = "biped_large_run")]
28    fn update_skeleton_inner(
29        skeleton: &Self::Skeleton,
30        (
31            active_tool_kind,
32            _second_tool_kind,
33            velocity,
34            orientation,
35            last_ori,
36            global_time,
37            avg_vel,
38            acc_vel,
39        ): Self::Dependency<'_>,
40        anim_time: f32,
41        rate: &mut f32,
42        s_a: &SkeletonAttr,
43    ) -> Self::Skeleton {
44        let mut next = (*skeleton).clone();
45        let speed = Vec2::<f32>::from(velocity).magnitude();
46        let speedavg = Vec2::<f32>::from(avg_vel).magnitude();
47
48        *rate = 1.0;
49
50        let lab: f32 = 0.65 * s_a.tempo;
51        let speednorm = (speed.min(16.0) / 12.0).powf(0.6);
52        let speednormlow = (speed.min(16.0) / 12.0).powf(4.0);
53
54        let footvertl = (acc_vel * lab + PI * -0.2).sin() * speednorm;
55        let footvertr = (acc_vel * lab + PI * -1.2).sin() * speednorm;
56
57        let footrotl = ((1.0 / (0.5 + (0.5) * ((acc_vel * lab + PI * 1.4).sin()).powi(2))).sqrt())
58            * ((acc_vel * lab + PI * 1.4).sin());
59
60        let footrotr = ((1.0 / (0.5 + (0.5) * ((acc_vel * lab + PI * 0.4).sin()).powi(2))).sqrt())
61            * ((acc_vel * lab + PI * 0.4).sin());
62
63        let amplitude = (speed / 21.0).max(0.25);
64        let amplitude2 = (speed * 1.4 / 21.0).sqrt().max(0.6);
65        let amplitude3 = (speed / 21.0).sqrt().max(0.35);
66        let speedmult = 1.0;
67        let canceler = (speed / 21.0).sqrt();
68
69        let short = (acc_vel * lab * speedmult).sin();
70        //
71        let shortalt = (acc_vel * lab * speedmult + PI * 3.0 + 0.7).sin();
72        let look = Vec2::new(
73            (global_time / 2.0 + anim_time / 2.0)
74                .floor()
75                .mul(7331.0)
76                .sin()
77                * 0.5,
78            (global_time / 2.0 + anim_time / 2.0)
79                .floor()
80                .mul(1337.0)
81                .sin()
82                * 0.25,
83        );
84
85        let speedadjust = if speed < 5.0 { 0.0 } else { speed / 21.0 };
86        let shift1 = speedadjust - PI / 2.0 - speedadjust * PI * 3.0 / 4.0;
87        let shift2 = speedadjust + PI / 2.0 + speedadjust * PI / 2.0;
88        let shift3 = speedadjust + PI / 4.0 - speedadjust * PI / 4.0;
89        let shift4 = speedadjust - PI * 3.0 / 4.0 + speedadjust * PI / 2.0;
90
91        //FL
92        let foot1a = (acc_vel * lab * speedmult + 0.0 + canceler * 0.05 + shift1).sin();
93        let foot1b = (acc_vel * lab * speedmult + 1.1 + canceler * 0.05 + shift1).sin();
94        //FR
95        let foot2a = (acc_vel * lab * speedmult + shift2).sin();
96        let foot2b = (acc_vel * lab * speedmult + 1.1 + shift2).sin();
97        //BL
98        let foot3a = (acc_vel * lab * speedmult + shift3).sin();
99        let foot3b = (acc_vel * lab * speedmult + 0.3 + shift3).sin();
100        //BR
101        let foot4a = (acc_vel * lab * speedmult + 0.0 + canceler * 0.05 + shift4).sin();
102        let foot4b = (acc_vel * lab * speedmult + PI / 2.0 + canceler * 0.05 + shift4).sin();
103        //
104        let slow = (acc_vel * lab * speedmult + PI).sin();
105
106        let ori: Vec2<f32> = Vec2::from(orientation);
107        let last_ori = Vec2::from(last_ori);
108        let tilt = if vek::Vec2::new(ori, last_ori)
109            .map(|o| o.magnitude_squared())
110            .map(|m| m > 0.001 && m.is_finite())
111            .reduce_and()
112            && ori.angle_between(last_ori).is_finite()
113        {
114            ori.angle_between(last_ori).min(0.2)
115                * last_ori.determine_side(Vec2::zero(), ori).signum()
116        } else {
117            0.0
118        } * 1.3;
119
120        let foothoril = (acc_vel * lab + PI * 1.45).sin() * speednorm;
121        let foothorir = (acc_vel * lab + PI * (0.45)).sin() * speednorm;
122        let footstrafel = (acc_vel * lab + PI * 1.45).sin() * speednorm;
123        let footstrafer = (acc_vel * lab + PI * (0.95)).sin() * speednorm;
124        let footvertsl = (acc_vel * lab).sin() * speednorm;
125        let footvertsr = (acc_vel * lab + PI * 0.5).sin() * speednorm;
126        let direction = velocity.y * -0.098 * orientation.y + velocity.x * -0.098 * orientation.x;
127
128        let side = ((velocity.x * -0.098 * orientation.y + velocity.y * 0.098 * orientation.x)
129            * -1.0)
130            .clamp(-1.0, 1.0);
131        let sideabs = side.abs();
132        let x_tilt = avg_vel.z.atan2(avg_vel.xy().magnitude());
133
134        next.jaw.scale = Vec3::one() * 1.02;
135        next.shoulder_l.scale = Vec3::one() * 1.1;
136        next.shoulder_r.scale = Vec3::one() * 1.1;
137        next.hand_l.scale = Vec3::one() * 1.04;
138        next.hand_r.scale = Vec3::one() * 1.04;
139        next.hold.scale = Vec3::one() * 0.0;
140        next.second.scale = Vec3::one() * 0.0;
141
142        if s_a.beast {
143            next.head.position = Vec3::new(0.0, s_a.head.0, 3.0 + s_a.head.1);
144            next.head.orientation = Quaternion::rotation_x(
145                look.y * 0.3 + (speedavg * 0.05) + amplitude * short * -0.18,
146            ) * Quaternion::rotation_z(
147                look.x * 0.3 / ((canceler).max(0.5)) + tilt * -1.2,
148            ) * Quaternion::rotation_y(tilt * 0.8);
149
150            next.jaw.position = Vec3::new(0.0, s_a.jaw.0, s_a.jaw.1);
151            next.jaw.orientation = Quaternion::rotation_x(0.0);
152
153            next.tail.position = Vec3::new(0.0, s_a.tail.0, s_a.tail.1);
154            next.tail.orientation =
155                Quaternion::rotation_x(canceler * 1.0 + amplitude * shortalt * 0.1)
156                    * Quaternion::rotation_z(tilt * 1.5);
157
158            next.upper_torso.position = Vec3::new(
159                0.0,
160                s_a.upper_torso.0,
161                s_a.upper_torso.1, //xtilt
162            );
163            next.upper_torso.orientation = Quaternion::rotation_x(
164                (amplitude * (short * -0.0).max(-0.2)) + 0.0 * (canceler * 6.0).min(1.0), //x_tilt
165            ) * Quaternion::rotation_y(tilt * 0.8)
166                * Quaternion::rotation_z(tilt * -1.5);
167
168            next.lower_torso.position = Vec3::new(0.0, s_a.lower_torso.0, s_a.lower_torso.1);
169            next.lower_torso.orientation =
170                Quaternion::rotation_x(amplitude * short * -0.25 + canceler * -0.4)
171                    * Quaternion::rotation_z(tilt * 1.8)
172                    * Quaternion::rotation_y(tilt * 0.6);
173
174            next.arm_control_l.position = Vec3::new(
175                0.0,
176                0.0 + amplitude3 * foot1b * -1.5 + canceler * -2.0,
177                0.0 + amplitude3 * foot1a * 1.5,
178            );
179            next.arm_control_l.orientation =
180                Quaternion::rotation_x(0.3 * canceler + amplitude3 * foot1a * 0.4)
181                    * Quaternion::rotation_z(tilt * -0.5)
182                    * Quaternion::rotation_y(tilt * 1.5);
183
184            next.arm_control_r.position = Vec3::new(
185                0.0,
186                0.0 + amplitude3 * foot2b * -1.5 + canceler * -2.0,
187                0.0 + amplitude3 * foot2a * 1.5,
188            );
189            next.arm_control_r.orientation =
190                Quaternion::rotation_x(0.3 * canceler + amplitude3 * foot2a * 0.4)
191                    * Quaternion::rotation_z(tilt * -0.5)
192                    * Quaternion::rotation_y(tilt * 1.5);
193
194            next.shoulder_l.position = Vec3::new(-s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
195
196            next.shoulder_r.position = Vec3::new(s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
197
198            next.hand_l.position = Vec3::new(
199                -s_a.hand.0,
200                s_a.hand.1 + foot1a * 1.0,
201                s_a.hand.2 + (foot1a * -3.0).max(1.0) * amplitude2,
202            );
203            next.hand_l.orientation =
204                Quaternion::rotation_x(amplitude2 * foot1b * 0.9 + canceler * 0.9)
205                    * Quaternion::rotation_y(tilt * -1.0);
206
207            next.hand_r.position = Vec3::new(
208                s_a.hand.0,
209                s_a.hand.1 + foot2a * 1.0,
210                s_a.hand.2 + (foot2a * -3.0).max(1.0) * amplitude2,
211            );
212            next.hand_r.orientation =
213                Quaternion::rotation_x(amplitude2 * foot2b * 0.9 + canceler * 0.7)
214                    * Quaternion::rotation_y(tilt * -1.0);
215
216            next.leg_control_l.position = Vec3::new(
217                0.0,
218                0.0 + amplitude3 * foot3b * -1.0 + canceler * -2.0,
219                0.0 + amplitude3 * foot3a * -2.5,
220            );
221            next.leg_control_l.orientation =
222                Quaternion::rotation_x(canceler * -0.4 + amplitude3 * foot3b * 0.4 + 0.5)
223                    * Quaternion::rotation_y(tilt * 1.5)
224                    * Quaternion::rotation_z(tilt * -1.5);
225
226            next.leg_control_r.position = Vec3::new(
227                0.0,
228                0.0 + amplitude3 * foot4b * -1.0 + canceler * -2.0,
229                0.0 + amplitude3 * foot4a * -2.5,
230            );
231            next.leg_control_r.orientation =
232                Quaternion::rotation_x(canceler * -0.4 + amplitude3 * foot4b * 0.4 + 0.5)
233                    * Quaternion::rotation_y(tilt * 1.5)
234                    * Quaternion::rotation_z(tilt * -1.5);
235            next.leg_l.position = Vec3::new(-s_a.leg.0, s_a.leg.1, s_a.leg.2);
236
237            next.leg_r.position = Vec3::new(s_a.leg.0, s_a.leg.1, s_a.leg.2);
238
239            next.foot_l.position = Vec3::new(
240                -s_a.foot.0,
241                s_a.foot.1 + foot3a * 2.0,
242                s_a.foot.2 + (foot3b * 4.0 * amplitude2).max(-1.0),
243            );
244            next.foot_l.orientation = Quaternion::rotation_x(amplitude2 * foot3b * 0.45 + 0.5)
245                * Quaternion::rotation_y(tilt * -1.0);
246
247            next.foot_r.position = Vec3::new(
248                s_a.foot.0,
249                s_a.foot.1 + foot4a * 2.0,
250                s_a.foot.2 + (foot4b * 4.0 * amplitude2).max(-1.0),
251            );
252            next.foot_r.orientation = Quaternion::rotation_x(amplitude2 * foot4b * 0.45 + 0.5)
253                * Quaternion::rotation_y(tilt * -1.0);
254
255            next.torso.position = Vec3::new(
256                0.0,
257                0.0 + (short * 6.0).max(-16.0),
258                speedavg * 1.2 + (short * 6.0).max(-16.0),
259            );
260            next.torso.orientation =
261                Quaternion::rotation_x(x_tilt + amplitude * short * 0.1 + speedavg * -0.045);
262        } else {
263            next.head.position = Vec3::new(0.0, s_a.head.0, s_a.head.1) * 1.02;
264            next.head.orientation = Quaternion::rotation_z(short * -0.18 * speednorm - tilt * 2.0)
265                * Quaternion::rotation_x(0.25 * speednorm);
266
267            next.upper_torso.position = Vec3::new(
268                0.0,
269                s_a.upper_torso.0,
270                s_a.upper_torso.1 + shortalt * -1.5 * speednorm,
271            );
272            next.upper_torso.orientation =
273                Quaternion::rotation_z(short * 0.07 * speednorm + tilt * -1.0)
274                    * Quaternion::rotation_y(tilt);
275
276            next.lower_torso.position = Vec3::new(0.0, s_a.lower_torso.0, s_a.lower_torso.1);
277            next.lower_torso.orientation =
278                Quaternion::rotation_z(short * 0.05 * speednorm + tilt * 0.5)
279                    * Quaternion::rotation_y(tilt * -0.5)
280                    * Quaternion::rotation_x(0.14 * speednorm);
281
282            next.jaw.position = Vec3::new(0.0, s_a.jaw.0, s_a.jaw.1);
283            next.jaw.orientation = Quaternion::rotation_x(0.0);
284
285            next.tail.position = Vec3::new(0.0, s_a.tail.0, s_a.tail.1);
286            next.tail.orientation = Quaternion::rotation_x(shortalt * 0.3 * speednorm);
287
288            next.second.position = Vec3::new(0.0, 0.0, 0.0);
289            next.second.orientation = Quaternion::rotation_x(0.0)
290                * Quaternion::rotation_y(0.0)
291                * Quaternion::rotation_z(0.0);
292
293            match active_tool_kind {
294                Some(ToolKind::Bow) => {
295                    next.main.position = Vec3::new(0.0, -6.0, 0.0);
296                    next.main.orientation =
297                        Quaternion::rotation_y(2.5) * Quaternion::rotation_z(PI / 2.0);
298                },
299                Some(ToolKind::Staff) | Some(ToolKind::Sceptre) => {
300                    next.main.position = Vec3::new(-6.0, -5.0, -12.0);
301                    next.main.orientation =
302                        Quaternion::rotation_y(0.6) * Quaternion::rotation_z(PI / 2.0);
303                },
304                Some(ToolKind::Sword) => {
305                    next.main.position = Vec3::new(-10.0, -8.0, 12.0);
306                    next.main.orientation =
307                        Quaternion::rotation_y(2.5) * Quaternion::rotation_z(PI / 2.0);
308                },
309                Some(ToolKind::Hammer) | Some(ToolKind::Axe) => {
310                    next.main.position = Vec3::new(-6.0, -8.0, 8.0);
311                    next.main.orientation =
312                        Quaternion::rotation_y(2.5) * Quaternion::rotation_z(PI / 2.0);
313                    next.second.position = Vec3::new(6.0, -8.0, 8.0);
314                    next.second.orientation =
315                        Quaternion::rotation_y(-2.5) * Quaternion::rotation_z(PI / 2.0);
316                },
317                _ => {
318                    next.main.position = Vec3::new(-2.0, -5.0, -6.0);
319                    next.main.orientation =
320                        Quaternion::rotation_y(0.6) * Quaternion::rotation_z(PI / 2.0);
321                },
322            }
323
324            next.shoulder_l.position = Vec3::new(
325                -s_a.shoulder.0,
326                s_a.shoulder.1,
327                s_a.shoulder.2 - foothorir * 1.5,
328            );
329            next.shoulder_l.orientation =
330                Quaternion::rotation_x(0.6 * speednormlow + (footrotr * -0.8) * speednorm)
331                    * Quaternion::rotation_y(0.0)
332                    * Quaternion::rotation_z(0.0);
333
334            next.shoulder_r.position = Vec3::new(
335                s_a.shoulder.0,
336                s_a.shoulder.1,
337                s_a.shoulder.2 - foothoril * 1.5,
338            );
339            next.shoulder_r.orientation =
340                Quaternion::rotation_x(0.6 * speednormlow + (footrotl * -0.8) * speednorm)
341                    * Quaternion::rotation_y(0.0);
342
343            next.hand_l.position = Vec3::new(
344                -s_a.hand.0 + foothorir * -1.3,
345                2.0 * speednorm + s_a.hand.1 + foothorir * -5.0,
346                1.5 * speednorm + s_a.hand.2 - foothorir * 4.5,
347            );
348            next.hand_l.orientation =
349                Quaternion::rotation_x(0.6 * speednorm + (footrotr * -1.2) * speednorm)
350                    * Quaternion::rotation_y(footrotr * 0.4 * speednorm);
351
352            next.hand_r.position = Vec3::new(
353                s_a.hand.0 + foothoril * 1.3,
354                2.0 * speednorm + s_a.hand.1 + foothoril * -5.0,
355                1.5 * speednorm + s_a.hand.2 - foothoril * 4.5,
356            );
357            next.hand_r.orientation =
358                Quaternion::rotation_x(0.6 * speednorm + (footrotl * -1.2) * speednorm)
359                    * Quaternion::rotation_y(footrotl * -0.4 * speednorm);
360
361            next.leg_l.position = Vec3::new(
362                -s_a.leg.0,
363                s_a.leg.1 + foothoril * -3.5,
364                s_a.leg.2 + (footvertl * -3.0),
365            ) * 0.98;
366            next.leg_l.orientation = Quaternion::rotation_z(short * 0.18 * speednorm)
367                * Quaternion::rotation_y(tilt * -0.5)
368                * Quaternion::rotation_x(foothoril * -0.8);
369
370            next.leg_r.position = Vec3::new(
371                s_a.leg.0,
372                s_a.leg.1 + foothorir * -3.5,
373                s_a.leg.2 + (footvertr * -3.0),
374            ) * 0.98;
375
376            next.leg_r.orientation = Quaternion::rotation_z(short * 0.18 * speednorm)
377                * Quaternion::rotation_y(tilt * -0.5)
378                * Quaternion::rotation_x(foothorir * -0.8);
379
380            next.foot_l.position = Vec3::new(
381                -s_a.foot.0 + footstrafel * sideabs * 3.0 + tilt * -2.0,
382                s_a.foot.1
383                    + (1.0 - sideabs) * (-1.5 * speednorm + foothoril * -10.5)
384                    + (direction * 5.0).max(0.0),
385                s_a.foot.2
386                    + (1.0 - sideabs) * (1.0 * speednorm + ((footvertl * -4.1).max(-1.0)))
387                    + side * ((footvertsl * 1.5).max(-1.0)),
388            );
389            next.foot_l.orientation = Quaternion::rotation_x(
390                (1.0 - sideabs) * (-0.2 + foothoril * -0.9) + sideabs * -0.5,
391            ) * Quaternion::rotation_y(
392                tilt * 1.0 + side * 0.3 + side * (foothoril * 0.3),
393            ) * Quaternion::rotation_z(side * 0.2);
394
395            next.foot_r.position = Vec3::new(
396                s_a.foot.0 + footstrafer * sideabs * 3.0 + tilt * -2.0,
397                s_a.foot.1
398                    + (1.0 - sideabs) * (-1.5 * speednorm + foothorir * -10.5)
399                    + (direction * 5.0).max(0.0),
400                s_a.foot.2
401                    + (1.0 - sideabs) * (1.0 * speednorm + ((footvertr * -4.1).max(-1.0)))
402                    + side * ((footvertsr * -1.5).max(-1.0)),
403            );
404            next.foot_r.orientation = Quaternion::rotation_x(
405                (1.0 - sideabs) * (-0.2 + foothorir * -0.9) + sideabs * -0.5,
406            ) * Quaternion::rotation_y(
407                tilt * 1.0 + side * 0.3 + side * (foothorir * 0.3),
408            ) * Quaternion::rotation_z(side * 0.2);
409
410            next.torso.position = Vec3::new(0.0, 0.0, 0.0);
411            next.torso.orientation = Quaternion::rotation_x(-0.25 * speednorm);
412        }
413
414        if s_a.float {
415            next.head.orientation = Quaternion::rotation_x(slow * 0.1);
416            next.upper_torso.position = Vec3::new(
417                0.0,
418                s_a.upper_torso.0,
419                s_a.upper_torso.1 + slow * 2.0 + 4.0 * speednorm,
420            );
421            next.upper_torso.orientation = Quaternion::rotation_x(-0.1 * speednorm + slow * 0.05);
422            next.lower_torso.orientation = Quaternion::rotation_z(short * 0.05 * speednorm)
423                * Quaternion::rotation_x(0.14 * speednorm);
424            next.shoulder_l.position = Vec3::new(-s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
425            next.shoulder_l.orientation = Quaternion::rotation_x(-0.4 * speednormlow + slow * 0.1);
426            next.shoulder_r.position = Vec3::new(s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
427            next.shoulder_r.orientation = Quaternion::rotation_x(-0.4 * speednormlow + slow * 0.1);
428            next.hand_l.position = Vec3::new(-s_a.hand.0, s_a.hand.1, s_a.hand.2);
429            next.hand_l.orientation = Quaternion::rotation_x(-0.4 * speednorm + slow * 0.1);
430            next.hand_r.position = Vec3::new(s_a.hand.0, s_a.hand.1, s_a.hand.2);
431            next.hand_r.orientation = Quaternion::rotation_x(-0.4 * speednorm + slow * 0.1);
432            next.foot_l.position = Vec3::new(-s_a.foot.0, s_a.foot.1, s_a.foot.2);
433            next.foot_l.orientation = Quaternion::rotation_x(-0.5 * speednorm + slow * 0.1);
434            next.foot_r.position = Vec3::new(s_a.foot.0, s_a.foot.1, s_a.foot.2);
435            next.foot_r.orientation = Quaternion::rotation_x(-0.5 * speednorm + slow * 0.1);
436        }
437
438        next
439    }
440}