veloren_voxygen_anim/golem/
run.rs

1use super::{
2    super::{Animation, vek::*},
3    GolemSkeleton, SkeletonAttr,
4};
5use std::f32::consts::PI;
6
7pub struct RunAnimation;
8
9impl Animation for RunAnimation {
10    type Dependency<'a> = (Vec3<f32>, Vec3<f32>, Vec3<f32>, f32, f32);
11    type Skeleton = GolemSkeleton;
12
13    #[cfg(feature = "use-dyn-lib")]
14    const UPDATE_FN: &'static [u8] = b"golem_run\0";
15
16    #[cfg_attr(feature = "be-dyn-lib", unsafe(export_name = "golem_run"))]
17    fn update_skeleton_inner(
18        skeleton: &Self::Skeleton,
19        (velocity, orientation, last_ori, _global_time, acc_vel): Self::Dependency<'_>,
20        anim_time: f32,
21        _rate: &mut f32,
22        s_a: &SkeletonAttr,
23    ) -> Self::Skeleton {
24        let mut next = (*skeleton).clone();
25        let speed = Vec2::<f32>::from(velocity).magnitude();
26        let mixed_vel = acc_vel + anim_time * 2.0; //sets run frequency using speed, with anim_time setting a floor
27
28        let lab: f32 = 0.45 * s_a.tempo;
29        let speednorm = (speed / 7.0).powf(0.6);
30        let foothoril =
31            ((1.0 / (0.4 + (0.6) * ((mixed_vel * 2.0 * lab + PI * 1.4).sin()).powi(2))).sqrt())
32                * ((mixed_vel * 2.0 * lab + PI * 1.4).sin())
33                * speednorm;
34        let foothorir =
35            ((1.0 / (0.4 + (0.6) * ((mixed_vel * 2.0 * lab + PI * 0.4).sin()).powi(2))).sqrt())
36                * ((mixed_vel * 2.0 * lab + PI * 0.4).sin())
37                * speednorm;
38        let footvertl = (mixed_vel * 2.0 * lab).sin() * speednorm;
39        let footvertr = (mixed_vel * 2.0 * lab + PI).sin() * speednorm;
40
41        let footrotl = ((1.0 / (0.5 + (0.5) * ((mixed_vel * 2.0 * lab + PI * 1.4).sin()).powi(2)))
42            .sqrt())
43            * ((mixed_vel * 2.0 * lab + PI * 1.4).sin())
44            * speednorm;
45
46        let footrotr = ((1.0 / (0.2 + (0.8) * ((mixed_vel * 2.0 * lab + PI * 0.4).sin()).powi(2)))
47            .sqrt())
48            * ((mixed_vel * 2.0 * lab + PI * 0.4).sin())
49            * speednorm;
50
51        let short = (mixed_vel * lab * 2.0).sin() * speednorm;
52        let shortalt = (mixed_vel * lab * 2.0 + PI / 2.0).sin() * speednorm;
53        let ori: Vec2<f32> = Vec2::from(orientation);
54        let last_ori = Vec2::from(last_ori);
55        let tilt = if vek::Vec2::new(ori, last_ori)
56            .map(|o| o.magnitude_squared())
57            .map(|m| m > 0.001 && m.is_finite())
58            .reduce_and()
59            && ori.angle_between(last_ori).is_finite()
60        {
61            ori.angle_between(last_ori).min(0.2)
62                * last_ori.determine_side(Vec2::zero(), ori).signum()
63        } else {
64            0.0
65        } * 1.3;
66
67        next.head.scale = Vec3::one() * 1.02;
68        next.jaw.scale = Vec3::one() * 1.02;
69        next.hand_l.scale = Vec3::one() * 1.04;
70        next.hand_r.scale = Vec3::one() * 1.04;
71        next.leg_l.scale = Vec3::one() * 1.02;
72        next.leg_r.scale = Vec3::one() * 1.02;
73
74        next.head.position = Vec3::new(0.0, s_a.head.0, s_a.head.1) * 1.02;
75        next.head.orientation =
76            Quaternion::rotation_z(short * -0.3) * Quaternion::rotation_x(-0.2 * speednorm);
77
78        next.jaw.position = Vec3::new(0.0, s_a.jaw.0, s_a.jaw.1) * 1.02;
79
80        next.upper_torso.position =
81            Vec3::new(0.0, s_a.upper_torso.0, s_a.upper_torso.1 + short * 1.0);
82        next.upper_torso.orientation = Quaternion::rotation_z(tilt * -4.0 + short * 0.40);
83
84        next.lower_torso.position = Vec3::new(0.0, s_a.lower_torso.0, s_a.lower_torso.1);
85        next.lower_torso.orientation = Quaternion::rotation_z(tilt * 4.0 + shortalt * 0.2);
86
87        next.shoulder_l.position = Vec3::new(-s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
88        next.shoulder_l.orientation = Quaternion::rotation_z(footrotl * 0.07)
89            * Quaternion::rotation_y(0.15)
90            * Quaternion::rotation_x(-0.2 + footrotl * -0.25);
91
92        next.shoulder_r.position = Vec3::new(s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
93        next.shoulder_r.orientation = Quaternion::rotation_z(footrotr * -0.07)
94            * Quaternion::rotation_y(-0.15 * speednorm)
95            * Quaternion::rotation_x(-0.2 + footrotr * -0.25);
96
97        next.hand_l.position = Vec3::new(-s_a.hand.0, s_a.hand.1, s_a.hand.2);
98        next.hand_l.orientation = Quaternion::rotation_x(0.3 + footrotl * -0.06)
99            * Quaternion::rotation_y(0.1 * speednorm)
100            * Quaternion::rotation_z(-0.35 * speednorm + footrotl * -0.1);
101
102        next.hand_r.position = Vec3::new(s_a.hand.0, s_a.hand.1, s_a.hand.2);
103        next.hand_r.orientation = Quaternion::rotation_x(0.3 + footrotr * -0.06)
104            * Quaternion::rotation_y(-0.1 * speednorm)
105            * Quaternion::rotation_z(0.35 * speednorm + footrotr * 0.1);
106
107        next.leg_l.position = Vec3::new(-s_a.leg.0, s_a.leg.1, s_a.leg.2) * 1.02;
108        next.leg_l.orientation = Quaternion::rotation_x(footrotl * 0.3)
109            * Quaternion::rotation_y(0.1 * speednorm)
110            * Quaternion::rotation_z(footrotl * -0.2);
111
112        next.leg_r.position = Vec3::new(s_a.leg.0, s_a.leg.1, s_a.leg.2) * 1.02;
113
114        next.leg_r.orientation = Quaternion::rotation_x(footrotr * 0.3)
115            * Quaternion::rotation_y(-0.1 * speednorm)
116            * Quaternion::rotation_z(footrotr * 0.2);
117
118        next.foot_l.position = Vec3::new(
119            -s_a.foot.0,
120            s_a.foot.1 + foothoril * 2.0,
121            s_a.foot.2 + (footvertl * 3.0).max(0.0),
122        );
123        next.foot_l.orientation =
124            Quaternion::rotation_x(footrotl * 0.2) * Quaternion::rotation_y(-0.08 * speednorm);
125
126        next.foot_r.position = Vec3::new(
127            s_a.foot.0,
128            s_a.foot.1 + foothorir * 2.0,
129            s_a.foot.2 + (footvertr * 3.0).max(0.0),
130        );
131        next.foot_r.orientation = Quaternion::rotation_z(0.0)
132            * Quaternion::rotation_x(footrotr * 0.2)
133            * Quaternion::rotation_y(0.08 * speednorm);
134
135        next.torso.position = Vec3::new(0.0, 0.0, 0.0);
136        next.torso.orientation = Quaternion::rotation_x(-0.2 * speednorm);
137        next
138    }
139}