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