veloren_voxygen_anim/arthropod/
run.rs

1use super::{
2    super::{Animation, vek::*},
3    ArthropodSkeleton, 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, Vec3<f32>, f32);
11    type Skeleton = ArthropodSkeleton;
12
13    #[cfg(feature = "use-dyn-lib")]
14    const UPDATE_FN: &'static [u8] = b"arthropod_run\0";
15
16    #[cfg_attr(feature = "be-dyn-lib", export_name = "arthropod_run")]
17    fn update_skeleton_inner(
18        skeleton: &Self::Skeleton,
19        (velocity, _orientation, _last_ori, _global_time, avg_vel, 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()).min(22.0);
26        *rate = 1.0;
27
28        let speednorm = speed / 13.0;
29
30        let mixed_vel = (acc_vel + anim_time * 6.0) * 0.8; //sets run frequency using speed, with anim_time setting a floor
31
32        //create a mix between a sine and a square wave
33        //(controllable with ratio variable)
34        let ratio = 0.1;
35        let wave1 = (mixed_vel).sin();
36        let wave2 = (mixed_vel - PI / 2.0).sin();
37        let wave3 = (mixed_vel + PI / 2.0).sin();
38        let wave4 = (mixed_vel + PI).sin();
39        let foot1 = wave1.abs().powf(ratio) * wave1.signum();
40        let foot2 = wave2.abs().powf(ratio) * wave2.signum();
41        let foot3 = wave3.abs().powf(ratio) * wave3.signum();
42        let foot4 = wave4.abs().powf(ratio) * wave4.signum();
43
44        let x_tilt = avg_vel.z.atan2(avg_vel.xy().magnitude()) * speednorm;
45
46        next.chest.scale = Vec3::one() * s_a.scaler;
47        next.wing_bl.scale = Vec3::one() * 0.98;
48        next.wing_br.scale = Vec3::one() * 0.98;
49
50        next.head.position = Vec3::new(0.0, s_a.head.0, s_a.head.1);
51        next.head.orientation = Quaternion::rotation_x((mixed_vel).sin() * 0.1)
52            * Quaternion::rotation_y((mixed_vel).sin().min(0.0) * 0.08)
53            * Quaternion::rotation_z((mixed_vel + PI * 1.5).sin() * 0.08);
54
55        next.chest.position = Vec3::new(0.0, s_a.chest.0, s_a.chest.1 + x_tilt);
56        next.chest.orientation = Quaternion::rotation_x((mixed_vel).sin().max(0.0) * 0.06 + x_tilt)
57            * Quaternion::rotation_z((mixed_vel + PI / 2.0).sin() * 0.06);
58
59        next.mandible_l.position = Vec3::new(-s_a.mandible.0, s_a.mandible.1, s_a.mandible.2);
60        next.mandible_r.position = Vec3::new(s_a.mandible.0, s_a.mandible.1, s_a.mandible.2);
61
62        next.wing_fl.position = Vec3::new(-s_a.wing_f.0, s_a.wing_f.1, s_a.wing_f.2);
63        next.wing_fr.position = Vec3::new(s_a.wing_f.0, s_a.wing_f.1, s_a.wing_f.2);
64
65        next.wing_bl.position = Vec3::new(-s_a.wing_b.0, s_a.wing_b.1, s_a.wing_b.2);
66        next.wing_br.position = Vec3::new(s_a.wing_b.0, s_a.wing_b.1, s_a.wing_b.2);
67
68        next.leg_fl.position = Vec3::new(-s_a.leg_f.0, s_a.leg_f.1, s_a.leg_f.2);
69        next.leg_fr.position = Vec3::new(s_a.leg_f.0, s_a.leg_f.1, s_a.leg_f.2);
70        next.leg_fl.orientation = Quaternion::rotation_x(foot4.max(0.0) * 0.7)
71            * Quaternion::rotation_z(s_a.leg_ori.0 + foot2 * 0.4);
72        next.leg_fr.orientation = Quaternion::rotation_x(foot1.max(0.0) * 0.7)
73            * Quaternion::rotation_z(-s_a.leg_ori.0 - foot3 * 0.4);
74
75        next.leg_fcl.position = Vec3::new(-s_a.leg_fc.0, s_a.leg_fc.1, s_a.leg_fc.2);
76        next.leg_fcr.position = Vec3::new(s_a.leg_fc.0, s_a.leg_fc.1, s_a.leg_fc.2);
77        next.leg_fcl.orientation = Quaternion::rotation_x(foot4 * 0.2)
78            * Quaternion::rotation_y(foot1.max(0.0) * 0.7)
79            * Quaternion::rotation_z(s_a.leg_ori.1 + foot3 * 0.2);
80        next.leg_fcr.orientation = Quaternion::rotation_x(foot1 * 0.2)
81            * Quaternion::rotation_y(foot1.min(0.0) * 0.7)
82            * Quaternion::rotation_z(-s_a.leg_ori.1 - foot2 * 0.2);
83
84        next.leg_bcl.position = Vec3::new(-s_a.leg_bc.0, s_a.leg_bc.1, s_a.leg_bc.2);
85        next.leg_bcr.position = Vec3::new(s_a.leg_bc.0, s_a.leg_bc.1, s_a.leg_bc.2);
86        next.leg_bcl.orientation = Quaternion::rotation_x(foot4 * 0.2)
87            * Quaternion::rotation_y(foot4.max(0.0) * 0.7)
88            * Quaternion::rotation_z(s_a.leg_ori.2 + foot2 * 0.3);
89        next.leg_bcr.orientation = Quaternion::rotation_x(foot1 * 0.2)
90            * Quaternion::rotation_y(foot4.min(0.0) * 0.7)
91            * Quaternion::rotation_z(-s_a.leg_ori.2 - foot3 * 0.3);
92
93        next.leg_bl.position = Vec3::new(-s_a.leg_b.0, s_a.leg_b.1, s_a.leg_b.2);
94        next.leg_br.position = Vec3::new(s_a.leg_b.0, s_a.leg_b.1, s_a.leg_b.2);
95        next.leg_bl.orientation = Quaternion::rotation_x(foot4 * 0.2)
96            * Quaternion::rotation_y(foot1.max(0.0) * 0.7)
97            * Quaternion::rotation_z(s_a.leg_ori.3 + foot3 * 0.2);
98        next.leg_br.orientation = Quaternion::rotation_x(foot1 * 0.2)
99            * Quaternion::rotation_y(foot1.min(0.0) * 0.7)
100            * Quaternion::rotation_z(-s_a.leg_ori.3 - foot2 * 0.2);
101
102        next
103    }
104}