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