veloren_voxygen_anim/crustacean/
swim.rs

1use super::{
2    super::{Animation, vek::*},
3    CrustaceanSkeleton, SkeletonAttr,
4};
5use std::f32::consts::PI;
6
7pub struct SwimAnimation;
8
9impl Animation for SwimAnimation {
10    type Dependency<'a> = (Vec3<f32>, Vec3<f32>, Vec3<f32>, f32, Vec3<f32>, f32);
11    type Skeleton = CrustaceanSkeleton;
12
13    #[cfg(feature = "use-dyn-lib")]
14    const UPDATE_FN: &'static [u8] = b"crustacean_swim\0";
15
16    #[cfg_attr(feature = "be-dyn-lib", export_name = "crustacean_swim")]
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 direction = velocity.y * 0.098 * orientation.y + velocity.x * 0.098 * orientation.x;
31
32        let side =
33            (velocity.x * -0.098 * orientation.y + velocity.y * 0.098 * orientation.x) * -1.0;
34        let sideabs = side.abs();
35
36        let mixed_vel = (acc_vel + anim_time * 6.0) * 0.8; //sets run frequency using speed, with anim_time setting a floor
37
38        //create a mix between a sine and a square wave
39        //(controllable with ratio variable)
40        let ratio = 0.1;
41        let wave1 = (mixed_vel).sin();
42        let wave2 = (mixed_vel - PI / 2.0).sin();
43        let wave3 = (mixed_vel + PI / 2.0).sin();
44        let wave4 = (mixed_vel + PI).sin();
45        let slow_wave = (mixed_vel / 10.0).sin();
46        let foot1 = wave1.abs().powf(ratio) * wave1.signum();
47        let foot2 = wave2.abs().powf(ratio) * wave2.signum();
48        let foot3 = wave3.abs().powf(ratio) * wave3.signum();
49        let foot4 = wave4.abs().powf(ratio) * wave4.signum();
50
51        let x_tilt = avg_vel.z.atan2(avg_vel.xy().magnitude()) * speednorm;
52
53        next.chest.scale = Vec3::one() * s_a.scaler;
54
55        let up_rot = 0.3;
56        let turnaround = PI;
57        let swim = -0.3 * foot2;
58
59        next.chest.position = Vec3::new(0.0, s_a.chest.0, s_a.chest.1 + x_tilt);
60        if s_a.move_sideways {
61            next.chest.orientation =
62                Quaternion::rotation_x((mixed_vel).sin().max(0.0) * 0.06 + x_tilt)
63                    * Quaternion::rotation_z(turnaround + ((mixed_vel + PI / 2.0).sin() * 0.06));
64
65            next.arm_l.orientation = Quaternion::rotation_x(0.1 * foot3)
66                * Quaternion::rotation_y(foot4.max(sideabs * -1.0) * up_rot)
67                * Quaternion::rotation_z((PI / -5.0) + 0.3 - foot2 * 0.1 * direction);
68            next.arm_r.orientation = Quaternion::rotation_x(0.1 * foot3)
69                * Quaternion::rotation_y(-foot1.max(sideabs * -1.0) * up_rot)
70                * Quaternion::rotation_z((PI / 5.0) + -0.2 - foot3 * 0.1 * direction);
71            next.arm_l.position = Vec3::new(0.0, -1.0, 0.0);
72            next.arm_r.position = Vec3::new(0.0, -1.0, 0.0);
73        } else {
74            next.arm_l.orientation = Quaternion::rotation_z(0.7 * -slow_wave);
75            next.arm_r.orientation = Quaternion::rotation_z(0.7 * slow_wave);
76        }
77
78        next.leg_fl.position = Vec3::new(-s_a.leg_f.0, s_a.leg_f.1, s_a.leg_f.2 + 1.0);
79        next.leg_fr.position = Vec3::new(s_a.leg_f.0, s_a.leg_f.1, s_a.leg_f.2 + 1.0);
80        next.leg_fl.orientation = Quaternion::rotation_y(foot4.max(sideabs * -0.5) * up_rot)
81            * Quaternion::rotation_z(swim + s_a.leg_ori.0 + foot2 * 0.1 * -direction);
82        next.leg_fr.orientation = Quaternion::rotation_y(-foot1.max(sideabs * -0.5) * up_rot)
83            * Quaternion::rotation_z(-swim + -s_a.leg_ori.0 - foot3 * 0.1 * -direction);
84
85        next.leg_cl.position = Vec3::new(-s_a.leg_c.0, s_a.leg_c.1, s_a.leg_c.2);
86        next.leg_cr.position = Vec3::new(s_a.leg_c.0, s_a.leg_c.1, s_a.leg_c.2);
87        next.leg_cl.orientation = Quaternion::rotation_x(foot4 * 0.1 * -direction)
88            * Quaternion::rotation_y(foot1.max(sideabs * -0.5) * up_rot)
89            * Quaternion::rotation_z(swim + s_a.leg_ori.1 + foot3 * 0.2 * -direction);
90        next.leg_cr.orientation = Quaternion::rotation_x(foot1 * 0.1 * -direction)
91            * Quaternion::rotation_y(foot1.min(sideabs * -0.5) * up_rot)
92            * Quaternion::rotation_z(-swim + -s_a.leg_ori.1 - foot2 * 0.2 * -direction);
93
94        next.leg_bl.position = Vec3::new(-s_a.leg_b.0, s_a.leg_b.1, s_a.leg_b.2);
95        next.leg_br.position = Vec3::new(s_a.leg_b.0, s_a.leg_b.1, s_a.leg_b.2);
96        next.leg_bl.orientation = Quaternion::rotation_x(foot4 * 0.2)
97            * Quaternion::rotation_y(foot4.max(sideabs * -0.5) * up_rot)
98            * Quaternion::rotation_z(swim + s_a.leg_ori.2 + foot3 * 0.2 * direction);
99        next.leg_br.orientation = Quaternion::rotation_x(foot1 * 0.2 * -direction)
100            * Quaternion::rotation_y(foot4.min(sideabs * -0.5) * up_rot)
101            * Quaternion::rotation_z(-swim + -s_a.leg_ori.2 - foot2 * 0.2 * direction);
102
103        next
104    }
105}