Skip to main content

veloren_voxygen_anim/character/
swim.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 SwimAnimation;
9
10type SwimAnimationDependency = (
11    Option<ToolKind>,
12    Option<ToolKind>,
13    (Option<Hands>, Option<Hands>),
14    Vec3<f32>,
15    Vec3<f32>,
16    Vec3<f32>,
17    f32,
18    Vec3<f32>,
19);
20
21impl Animation for SwimAnimation {
22    type Dependency<'a> = SwimAnimationDependency;
23    type Skeleton = CharacterSkeleton;
24
25    #[cfg(feature = "use-dyn-lib")]
26    const UPDATE_FN: &'static [u8] = b"character_swim\0";
27
28    #[cfg_attr(feature = "be-dyn-lib", unsafe(export_name = "character_swim"))]
29    fn update_skeleton_inner(
30        skeleton: &Self::Skeleton,
31        (
32            active_tool_kind,
33            second_tool_kind,
34            hands,
35            velocity,
36            orientation,
37            last_ori,
38            global_time,
39            avg_vel,
40        ): Self::Dependency<'_>,
41        anim_time: f32,
42        rate: &mut f32,
43        s_a: &SkeletonAttr,
44    ) -> Self::Skeleton {
45        let mut next = (*skeleton).clone();
46        let avgspeed = Vec2::<f32>::from(avg_vel).magnitude();
47
48        let avgtotal = avg_vel.magnitude();
49
50        let speed = velocity.magnitude();
51        *rate = 1.0 + speed * 0.3;
52        let tempo = 0.4 / s_a.scaler;
53        let intensity = 1.0;
54
55        let lab: f32 = 1.0 * tempo;
56
57        let short = (anim_time * lab * 6.0 + PI * 0.9).sin();
58
59        let foot = (anim_time * lab * 4.0 + PI * -0.1).sin();
60        let foot2 = (anim_time * lab * 4.0 + PI * -1.6).sin();
61
62        let footrotl = ((1.0 / (0.2 + (0.8) * ((anim_time * 6.0 * lab + PI * 1.4).sin()).powi(2)))
63            .sqrt())
64            * ((anim_time * 6.0 * lab + PI * 1.4).sin());
65
66        let footrotr = ((1.0 / (0.2 + (0.8) * ((anim_time * 6.0 * lab + PI * 0.4).sin()).powi(2)))
67            .sqrt())
68            * ((anim_time * 6.0 * lab + PI * 0.4).sin());
69
70        let foothoril = (anim_time * 6.0 * lab + PI * 1.4).sin();
71        let foothorir = (anim_time * 6.0 * lab + PI * (0.4)).sin();
72        let head_look = Vec2::new(
73            (global_time + anim_time / 4.0 * (1.0 / tempo))
74                .floor()
75                .mul(7331.0)
76                .sin()
77                * 0.2,
78            (global_time + anim_time / 4.0 * (1.0 / tempo))
79                .floor()
80                .mul(1337.0)
81                .sin()
82                * 0.1,
83        );
84        let ori: Vec2<f32> = Vec2::from(orientation);
85        let last_ori = Vec2::from(last_ori);
86        let tilt = if vek::Vec2::new(ori, last_ori)
87            .map(|o| o.magnitude_squared())
88            .map(|m| m > 0.001 && m.is_finite())
89            .reduce_and()
90            && ori.angle_between(last_ori).is_finite()
91        {
92            ori.angle_between(last_ori).min(0.8)
93                * last_ori.determine_side(Vec2::zero(), ori).signum()
94        } else {
95            0.0
96        } * 1.3;
97        let abstilt = tilt.abs();
98
99        let switch = if avg_vel.z > 0.0 && avgspeed < 0.5 {
100            avgtotal.min(0.5)
101        } else {
102            avgtotal
103        };
104        let dir_ori = (((1.0 / switch) * PI / 2.0 + avg_vel.z * 0.12).min(PI / 2.0) - PI / 2.0)
105            + avgspeed * avg_vel.z * -0.003;
106
107        let squash = if abstilt > 0.2 { 0.35 } else { 1.0 }; //condenses the body at strong turns
108        next.head.position = Vec3::new(
109            0.0,
110            s_a.head.0 + foot * 0.8,
111            s_a.head.1 + short * 0.2 - foot * 0.2,
112        );
113        next.head.orientation =
114            Quaternion::rotation_z(head_look.x * 0.3 + short * -0.2 * intensity - tilt * 2.0)
115                * Quaternion::rotation_x(
116                    (0.4 * head_look.y * (1.0 / intensity)).abs()
117                        + 0.25 * intensity
118                        + velocity.z * 0.03
119                        - (abstilt * 1.8).min(0.0)
120                        - foot * 0.2,
121                );
122        next.head.scale = Vec3::one() * s_a.head_scale;
123
124        next.chest.position = Vec3::new(
125            0.0,
126            s_a.chest.0,
127            -10.0 + s_a.chest.1 + short * 0.3 * intensity,
128        );
129        next.chest.orientation =
130            Quaternion::rotation_x(foot * 0.25) * Quaternion::rotation_z(short * 0.1 * intensity);
131
132        next.belt.position = Vec3::new(0.0, s_a.belt.0, s_a.belt.1);
133        next.belt.orientation = Quaternion::rotation_x(velocity.z.abs() * -0.005 + abstilt * 1.0)
134            * Quaternion::rotation_z(short * -0.2 * intensity);
135
136        next.back.position = Vec3::new(0.0, s_a.back.0, s_a.back.1);
137        next.back.scale = Vec3::one() * 1.02;
138
139        next.shorts.position = Vec3::new(0.0, s_a.shorts.0, s_a.shorts.1);
140        next.shorts.orientation = Quaternion::rotation_x(velocity.z.abs() * -0.005 + abstilt * 1.0)
141            * Quaternion::rotation_z(footrotr * 0.3 * intensity);
142
143        next.hand_l.position = Vec3::new(
144            -0.5 - s_a.hand.0 + foot2.min(0.0) * 4.0 + foot * 1.5,
145            1.5 + s_a.hand.1 + foot * 2.0 * intensity * squash + foot * 2.0,
146            intensity * 5.0 + s_a.hand.2 + foot * 4.0 * intensity * squash,
147        );
148        next.hand_l.orientation = Quaternion::rotation_x(-4.0 * intensity * squash)
149            * Quaternion::rotation_y(foot2 * -0.55 + foot * -1.5 + 1.3)
150            * Quaternion::rotation_z(foot2 * 1.25 - 1.6);
151        next.hand_l.scale = Vec3::one() * 1.04;
152
153        next.hand_r.position = Vec3::new(
154            0.5 + s_a.hand.0 - foot2.min(0.0) * 4.0 - foot * 1.5,
155            1.5 + s_a.hand.1 + foot * 2.0 * intensity * squash + foot * 2.0,
156            intensity * 5.0 + s_a.hand.2 + foot * 4.0 * intensity * squash,
157        );
158        next.hand_r.orientation = Quaternion::rotation_x(-4.0 * intensity * squash)
159            * Quaternion::rotation_y(foot2 * 0.55 + foot * 1.5 - 1.3)
160            * Quaternion::rotation_z(foot2 * -1.25 + 1.6);
161        next.hand_r.scale = Vec3::one() * 1.04;
162
163        next.foot_l.position = Vec3::new(
164            -s_a.foot.0,
165            s_a.foot.1 + foothoril * 3.5 * intensity * squash,
166            -10.0 + s_a.foot.2 + footrotl * 1.0 * intensity * squash,
167        );
168        next.foot_l.orientation =
169            Quaternion::rotation_x(-0.8 * squash + footrotl * 0.6 * intensity * squash);
170
171        next.foot_r.position = Vec3::new(
172            s_a.foot.0,
173            s_a.foot.1 + foothorir * 3.5 * intensity * squash,
174            -10.0 + s_a.foot.2 + footrotr * 1.0 * intensity * squash,
175        );
176        next.foot_r.orientation =
177            Quaternion::rotation_x(-0.8 * squash + footrotr * 0.6 * intensity * squash);
178
179        next.shoulder_l.position = Vec3::new(
180            -s_a.shoulder.0,
181            s_a.shoulder.1 + foot * 2.0 - 2.0,
182            s_a.shoulder.2 + foot + 2.0,
183        );
184        next.shoulder_l.orientation =
185            Quaternion::rotation_x(short * 0.15 * intensity + foot * 0.5 + 0.7);
186        next.shoulder_l.scale = Vec3::one() * 1.1;
187
188        next.shoulder_r.position = Vec3::new(
189            s_a.shoulder.0,
190            s_a.shoulder.1 + foot * 2.0 - 2.0,
191            s_a.shoulder.2 + foot + 2.0,
192        );
193        next.shoulder_r.orientation =
194            Quaternion::rotation_x(short * -0.15 * intensity + foot * 0.5 + 0.7);
195        next.shoulder_r.scale = Vec3::one() * 1.1;
196
197        next.glider.position = Vec3::new(0.0, 0.0, 10.0);
198        next.glider.scale = Vec3::one() * 0.0;
199
200        next.do_tools_on_back(hands, active_tool_kind, second_tool_kind);
201
202        next.lantern.position = Vec3::new(s_a.lantern.0, s_a.lantern.1, s_a.lantern.2);
203        next.lantern.scale = Vec3::one() * 0.65;
204        next.hold.scale = Vec3::one() * 0.0;
205
206        next.torso.position = Vec3::new(0.0, 0.0, 11.0 - avgspeed * 0.55);
207        next.torso.orientation = Quaternion::rotation_x(dir_ori)
208            * Quaternion::rotation_y(tilt * 2.0)
209            * Quaternion::rotation_z(tilt * 3.0);
210
211        next
212    }
213}