veloren_voxygen_anim/biped_small/
run.rs

1use super::{
2    super::{Animation, vek::*},
3    BipedSmallSkeleton, SkeletonAttr,
4};
5use core::{f32::consts::PI, ops::Mul};
6
7pub struct RunAnimation;
8
9type RunAnimationDependency = (Vec3<f32>, Vec3<f32>, Vec3<f32>, f32, Vec3<f32>, f32);
10
11impl Animation for RunAnimation {
12    type Dependency<'a> = RunAnimationDependency;
13    type Skeleton = BipedSmallSkeleton;
14
15    #[cfg(feature = "use-dyn-lib")]
16    const UPDATE_FN: &'static [u8] = b"biped_small_run\0";
17
18    #[cfg_attr(feature = "be-dyn-lib", unsafe(export_name = "biped_small_run"))]
19
20    fn update_skeleton_inner(
21        skeleton: &Self::Skeleton,
22        (velocity, orientation, last_ori, global_time, _avg_vel, acc_vel): Self::Dependency<'_>,
23        anim_time: f32,
24        rate: &mut f32,
25        s_a: &SkeletonAttr,
26    ) -> Self::Skeleton {
27        let mut next = (*skeleton).clone();
28        let speed = Vec2::<f32>::from(velocity).magnitude();
29        *rate = 1.0;
30        let speednorm = (speed / 9.4).powf(0.4);
31
32        let lab: f32 = 1.0;
33
34        let footrotl = ((5.0 / (0.5 + (5.5) * ((acc_vel * 1.4 * lab + PI * 1.4).sin()).powi(2)))
35            .sqrt())
36            * ((acc_vel * 1.4 * lab + PI * 1.4).sin());
37
38        let footrotr = ((5.0 / (0.5 + (5.5) * ((acc_vel * 1.4 * lab + PI * 0.4).sin()).powi(2)))
39            .sqrt())
40            * ((acc_vel * 1.4 * lab + PI * 0.4).sin());
41
42        let shortalter = (acc_vel * lab * 1.4 + PI / -2.0).sin();
43
44        let fast = (anim_time * 20.0).sin();
45
46        let foothoril = (acc_vel * 1.4 * lab + PI * 1.45).sin();
47        let foothorir = (acc_vel * 1.4 * lab + PI * (0.45)).sin();
48        let footstrafel = (acc_vel * 1.4 * lab + PI * 1.45).sin();
49        let footstrafer = (acc_vel * 1.4 * lab + PI * (0.95)).sin();
50
51        let footvertl = (acc_vel * 1.4 * lab).sin();
52        let footvertr = (acc_vel * 1.4 * lab + PI).sin();
53        let footvertsl = (acc_vel * 1.4 * lab).sin();
54        let footvertsr = (acc_vel * 1.4 * lab + PI * 0.5).sin();
55
56        let shortalt = (acc_vel * lab * 1.4 + PI / 2.0).sin();
57
58        let short = ((5.0 / (1.5 + 3.5 * ((acc_vel * lab * 1.4).sin()).powi(2))).sqrt())
59            * ((acc_vel * lab * 1.4).sin());
60        let direction = velocity.y * -0.098 * orientation.y + velocity.x * -0.098 * orientation.x;
61
62        let side =
63            (velocity.x * -0.098 * orientation.y + velocity.y * 0.098 * orientation.x) * -1.0;
64        let sideabs = side.abs();
65        let ori: Vec2<f32> = Vec2::from(orientation);
66        let last_ori = Vec2::from(last_ori);
67        let tilt = if vek::Vec2::new(ori, last_ori)
68            .map(|o| o.magnitude_squared())
69            .map(|m| m > 0.001 && m.is_finite())
70            .reduce_and()
71            && ori.angle_between(last_ori).is_finite()
72        {
73            ori.angle_between(last_ori).min(0.2)
74                * last_ori.determine_side(Vec2::zero(), ori).signum()
75        } else {
76            0.0
77        } * 1.3;
78
79        let head_look = Vec2::new(
80            (global_time + anim_time / 18.0).floor().mul(7331.0).sin() * 0.2,
81            (global_time + anim_time / 18.0).floor().mul(1137.0).sin() * 0.1,
82        );
83        next.head.position = Vec3::new(0.0, -1.0 + s_a.head.0, s_a.head.1 + short * 0.1);
84        next.head.orientation =
85            Quaternion::rotation_z(tilt * -2.5 + head_look.x * 0.2 - short * 0.02)
86                * Quaternion::rotation_x(head_look.y + 0.45 * speednorm);
87
88        next.chest.position = Vec3::new(
89            0.0,
90            s_a.chest.0,
91            s_a.chest.1 + 1.0 * speednorm + shortalt * -0.8,
92        );
93        next.chest.orientation = Quaternion::rotation_z(short * 0.06 + tilt * -0.6)
94            * Quaternion::rotation_y(tilt * 1.6)
95            * Quaternion::rotation_x(shortalter * 0.035 + speednorm * -0.4 + (tilt.abs()));
96        next.main.position = Vec3::new(4.0, -4.0, -3.0);
97        next.main.orientation = Quaternion::rotation_y(-0.5) * Quaternion::rotation_z(PI / 2.0);
98        next.second.position = Vec3::new(-4.0, -4.0, -3.0);
99        next.second.orientation = Quaternion::rotation_y(0.5) * Quaternion::rotation_z(-PI / 2.0);
100
101        next.pants.position = Vec3::new(0.0, s_a.pants.0, s_a.pants.1);
102        next.pants.orientation = Quaternion::rotation_x(0.1 * speednorm)
103            * Quaternion::rotation_z(short * 0.25 + tilt * -1.5)
104            * Quaternion::rotation_y(tilt * 0.7);
105
106        if s_a.wing_for_foot {
107            next.hand_l.position = Vec3::new(-s_a.hand.0, s_a.hand.1 - 2.0, s_a.hand.2);
108            next.hand_l.orientation = Quaternion::rotation_x(0.4);
109
110            next.hand_r.position = Vec3::new(s_a.hand.0, s_a.hand.1 - 2.0, s_a.hand.2);
111            next.hand_r.orientation = Quaternion::rotation_x(0.4);
112
113            next.foot_l.position = Vec3::new(-s_a.foot.0, s_a.foot.1, s_a.foot.2);
114            next.foot_l.orientation =
115                Quaternion::rotation_x(-0.6 - fast * 0.6) * Quaternion::rotation_z(fast * 0.4);
116
117            next.foot_r.position = Vec3::new(s_a.foot.0, s_a.foot.1, s_a.foot.2);
118            next.foot_r.orientation =
119                Quaternion::rotation_x(-0.6 - fast * 0.6) * Quaternion::rotation_z(fast * -0.4);
120        } else {
121            next.hand_l.position = Vec3::new(
122                -s_a.hand.0 + footrotr * -1.3 * speednorm,
123                1.0 * speednorm + s_a.hand.1 + footrotr * -2.5 * speednorm,
124                s_a.hand.2 - footrotr * 1.5 * speednorm,
125            );
126            next.hand_l.orientation =
127                Quaternion::rotation_x(0.4 * speednorm + (footrotr * -1.2) * speednorm)
128                    * Quaternion::rotation_y(footrotr * 0.4 * speednorm);
129
130            next.hand_r.position = Vec3::new(
131                s_a.hand.0 + footrotl * 1.3 * speednorm,
132                1.0 * speednorm + s_a.hand.1 + footrotl * -2.5 * speednorm,
133                s_a.hand.2 - footrotl * 1.5 * speednorm,
134            );
135            next.hand_r.orientation =
136                Quaternion::rotation_x(0.4 * speednorm + (footrotl * -1.2) * speednorm)
137                    * Quaternion::rotation_y(footrotl * -0.4 * speednorm);
138
139            next.foot_l.position = Vec3::new(
140                -s_a.foot.0 + footstrafel * sideabs * 3.0 + tilt * -2.0,
141                s_a.foot.1
142                    + (1.0 - sideabs) * (-1.0 * speednorm + footrotl * -2.5 * speednorm)
143                    + (direction * 5.0).max(0.0),
144                s_a.foot.2
145                    + (1.0 - sideabs)
146                        * (2.0 * speednorm + ((footvertl * -1.1 * speednorm).max(-1.0)))
147                    + side * ((footvertsl * 1.5).max(-1.0)),
148            );
149            next.foot_l.orientation = Quaternion::rotation_x(
150                (1.0 - sideabs) * (-0.2 * speednorm + foothoril * -0.9 * speednorm)
151                    + sideabs * -0.5,
152            ) * Quaternion::rotation_y(
153                tilt * 2.0 + side * 0.3 + side * (foothoril * 0.3),
154            ) * Quaternion::rotation_z(side * 0.2);
155
156            next.foot_r.position = Vec3::new(
157                s_a.foot.0 + footstrafer * sideabs * 3.0 + tilt * -2.0,
158                s_a.foot.1
159                    + (1.0 - sideabs) * (-1.0 * speednorm + footrotr * -2.5 * speednorm)
160                    + (direction * 5.0).max(0.0),
161                s_a.foot.2
162                    + (1.0 - sideabs)
163                        * (2.0 * speednorm + ((footvertr * -1.1 * speednorm).max(-1.0)))
164                    + side * ((footvertsr * -1.5).max(-1.0)),
165            );
166            next.foot_r.orientation = Quaternion::rotation_x(
167                (1.0 - sideabs) * (-0.2 * speednorm + foothorir * -0.9 * speednorm)
168                    + sideabs * -0.5,
169            ) * Quaternion::rotation_y(
170                tilt * 2.0 + side * 0.3 + side * (foothorir * 0.3),
171            ) * Quaternion::rotation_z(side * 0.2);
172        }
173        next.head.scale = Vec3::one();
174
175        next.tail.position = Vec3::new(0.0, s_a.tail.0, s_a.tail.1);
176
177        next
178    }
179}