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