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", 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(2.0, -3.0, -3.0);
97        next.main.orientation = Quaternion::rotation_y(-0.5) * Quaternion::rotation_z(PI / 2.0);
98
99        next.pants.position = Vec3::new(0.0, s_a.pants.0, s_a.pants.1);
100        next.pants.orientation = Quaternion::rotation_x(0.1 * speednorm)
101            * Quaternion::rotation_z(short * 0.25 + tilt * -1.5)
102            * Quaternion::rotation_y(tilt * 0.7);
103
104        if s_a.wing_for_foot {
105            next.hand_l.position = Vec3::new(-s_a.hand.0, s_a.hand.1 - 2.0, s_a.hand.2);
106            next.hand_l.orientation = Quaternion::rotation_x(0.4);
107
108            next.hand_r.position = Vec3::new(s_a.hand.0, s_a.hand.1 - 2.0, s_a.hand.2);
109            next.hand_r.orientation = Quaternion::rotation_x(0.4);
110
111            next.foot_l.position = Vec3::new(-s_a.foot.0, s_a.foot.1, s_a.foot.2);
112            next.foot_l.orientation =
113                Quaternion::rotation_x(-0.6 - fast * 0.6) * Quaternion::rotation_z(fast * 0.4);
114
115            next.foot_r.position = Vec3::new(s_a.foot.0, s_a.foot.1, s_a.foot.2);
116            next.foot_r.orientation =
117                Quaternion::rotation_x(-0.6 - fast * 0.6) * Quaternion::rotation_z(fast * -0.4);
118        } else {
119            next.hand_l.position = Vec3::new(
120                -s_a.hand.0 + footrotr * -1.3 * speednorm,
121                1.0 * speednorm + s_a.hand.1 + footrotr * -2.5 * speednorm,
122                s_a.hand.2 - footrotr * 1.5 * speednorm,
123            );
124            next.hand_l.orientation =
125                Quaternion::rotation_x(0.4 * speednorm + (footrotr * -1.2) * speednorm)
126                    * Quaternion::rotation_y(footrotr * 0.4 * speednorm);
127
128            next.hand_r.position = Vec3::new(
129                s_a.hand.0 + footrotl * 1.3 * speednorm,
130                1.0 * speednorm + s_a.hand.1 + footrotl * -2.5 * speednorm,
131                s_a.hand.2 - footrotl * 1.5 * speednorm,
132            );
133            next.hand_r.orientation =
134                Quaternion::rotation_x(0.4 * speednorm + (footrotl * -1.2) * speednorm)
135                    * Quaternion::rotation_y(footrotl * -0.4 * speednorm);
136
137            next.foot_l.position = Vec3::new(
138                -s_a.foot.0 + footstrafel * sideabs * 3.0 + tilt * -2.0,
139                s_a.foot.1
140                    + (1.0 - sideabs) * (-1.0 * speednorm + footrotl * -2.5 * speednorm)
141                    + (direction * 5.0).max(0.0),
142                s_a.foot.2
143                    + (1.0 - sideabs)
144                        * (2.0 * speednorm + ((footvertl * -1.1 * speednorm).max(-1.0)))
145                    + side * ((footvertsl * 1.5).max(-1.0)),
146            );
147            next.foot_l.orientation = Quaternion::rotation_x(
148                (1.0 - sideabs) * (-0.2 * speednorm + foothoril * -0.9 * speednorm)
149                    + sideabs * -0.5,
150            ) * Quaternion::rotation_y(
151                tilt * 2.0 + side * 0.3 + side * (foothoril * 0.3),
152            ) * Quaternion::rotation_z(side * 0.2);
153
154            next.foot_r.position = Vec3::new(
155                s_a.foot.0 + footstrafer * sideabs * 3.0 + tilt * -2.0,
156                s_a.foot.1
157                    + (1.0 - sideabs) * (-1.0 * speednorm + footrotr * -2.5 * speednorm)
158                    + (direction * 5.0).max(0.0),
159                s_a.foot.2
160                    + (1.0 - sideabs)
161                        * (2.0 * speednorm + ((footvertr * -1.1 * speednorm).max(-1.0)))
162                    + side * ((footvertsr * -1.5).max(-1.0)),
163            );
164            next.foot_r.orientation = Quaternion::rotation_x(
165                (1.0 - sideabs) * (-0.2 * speednorm + foothorir * -0.9 * speednorm)
166                    + sideabs * -0.5,
167            ) * Quaternion::rotation_y(
168                tilt * 2.0 + side * 0.3 + side * (foothorir * 0.3),
169            ) * Quaternion::rotation_z(side * 0.2);
170        }
171        next.head.scale = Vec3::one();
172
173        next.tail.position = Vec3::new(0.0, s_a.tail.0, s_a.tail.1);
174
175        next
176    }
177}