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