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", export_name = "character_swim")]
29
30    fn update_skeleton_inner(
31        skeleton: &Self::Skeleton,
32        (
33            active_tool_kind,
34            second_tool_kind,
35            hands,
36            velocity,
37            orientation,
38            last_ori,
39            global_time,
40            avg_vel,
41        ): Self::Dependency<'_>,
42        anim_time: f32,
43        rate: &mut f32,
44        s_a: &SkeletonAttr,
45    ) -> Self::Skeleton {
46        let mut next = (*skeleton).clone();
47        let avgspeed = Vec2::<f32>::from(avg_vel).magnitude();
48
49        let avgtotal = avg_vel.magnitude();
50
51        let speed = velocity.magnitude();
52        *rate = 1.0;
53        let tempo = if speed > 0.5 { 1.5 } else { 0.7 };
54        let intensity = if speed > 0.5 { 1.0 } else { 0.3 };
55
56        let lab: f32 = 1.0 * tempo;
57
58        let short = (anim_time * lab * 6.0 + PI * 0.9).sin();
59
60        let foot = (anim_time * lab * 6.0 + PI * -0.1).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 squash = if abstilt > 0.2 { 0.35 } else { 1.0 }; //condenses the body at strong turns
100        next.head.position = Vec3::new(0.0, s_a.head.0, s_a.head.1 - 1.0 + short * 0.3);
101        next.head.orientation =
102            Quaternion::rotation_z(head_look.x * 0.3 + short * -0.2 * intensity + tilt * 3.0)
103                * Quaternion::rotation_x(
104                    (0.4 * head_look.y * (1.0 / intensity)).abs()
105                        + 0.45 * intensity
106                        + velocity.z * 0.03
107                        - (abstilt * 1.8).min(0.0),
108                );
109        next.head.scale = Vec3::one() * s_a.head_scale;
110
111        next.chest.position = Vec3::new(
112            0.0,
113            s_a.chest.0,
114            -10.0 + s_a.chest.1 + short * 0.3 * intensity,
115        );
116        next.chest.orientation = Quaternion::rotation_z(short * 0.1 * intensity);
117
118        next.belt.position = Vec3::new(0.0, s_a.belt.0, s_a.belt.1);
119        next.belt.orientation = Quaternion::rotation_x(velocity.z.abs() * -0.005 + abstilt * 1.0)
120            * Quaternion::rotation_z(short * -0.2 * intensity);
121
122        next.back.position = Vec3::new(0.0, s_a.back.0, s_a.back.1);
123        next.back.scale = Vec3::one() * 1.02;
124
125        next.shorts.position = Vec3::new(0.0, s_a.shorts.0, s_a.shorts.1);
126        next.shorts.orientation = Quaternion::rotation_x(velocity.z.abs() * -0.005 + abstilt * 1.0)
127            * Quaternion::rotation_z(short * -0.3 * intensity);
128
129        next.hand_l.position = Vec3::new(
130            -1.0 - s_a.hand.0,
131            1.5 + s_a.hand.1 - foot * 2.0 * intensity * squash,
132            intensity * 5.0 + s_a.hand.2 + foot * -5.0 * intensity * squash,
133        );
134        next.hand_l.orientation = Quaternion::rotation_x(1.5 + foot * -1.2 * intensity * squash)
135            * Quaternion::rotation_y(0.4 + foot * -0.35);
136        next.hand_l.scale = Vec3::one() * 1.04;
137
138        next.hand_r.position = Vec3::new(
139            1.0 + s_a.hand.0,
140            1.5 + s_a.hand.1 + foot * 2.0 * intensity * squash,
141            intensity * 5.0 + s_a.hand.2 + foot * 5.0 * intensity * squash,
142        );
143        next.hand_r.orientation = Quaternion::rotation_x(1.5 + foot * 1.2 * intensity * squash)
144            * Quaternion::rotation_y(-0.4 + foot * -0.35);
145        next.hand_r.scale = Vec3::one() * 1.04;
146
147        next.foot_l.position = Vec3::new(
148            -s_a.foot.0,
149            s_a.foot.1 + foothoril * 1.5 * intensity * squash,
150            -10.0 + s_a.foot.2 + footrotl * 3.0 * intensity * squash,
151        );
152        next.foot_l.orientation =
153            Quaternion::rotation_x(-0.8 * squash + footrotl * 0.4 * intensity * squash);
154
155        next.foot_r.position = Vec3::new(
156            s_a.foot.0,
157            s_a.foot.1 + foothorir * 1.5 * intensity * squash,
158            -10.0 + s_a.foot.2 + footrotr * 3.0 * intensity * squash,
159        );
160        next.foot_r.orientation =
161            Quaternion::rotation_x(-0.8 * squash + footrotr * 0.4 * intensity * squash);
162
163        next.shoulder_l.position = Vec3::new(-s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
164        next.shoulder_l.orientation = Quaternion::rotation_x(short * 0.15 * intensity);
165        next.shoulder_l.scale = Vec3::one() * 1.1;
166
167        next.shoulder_r.position = Vec3::new(s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
168        next.shoulder_r.orientation = Quaternion::rotation_x(short * -0.15 * intensity);
169        next.shoulder_r.scale = Vec3::one() * 1.1;
170
171        next.glider.position = Vec3::new(0.0, 0.0, 10.0);
172        next.glider.scale = Vec3::one() * 0.0;
173
174        next.do_tools_on_back(hands, active_tool_kind, second_tool_kind);
175
176        next.lantern.position = Vec3::new(s_a.lantern.0, s_a.lantern.1, s_a.lantern.2);
177        next.lantern.scale = Vec3::one() * 0.65;
178        next.hold.scale = Vec3::one() * 0.0;
179
180        let switch = if avg_vel.z > 0.0 && avgspeed < 0.5 {
181            avgtotal.min(0.5)
182        } else {
183            avgtotal
184        };
185        next.torso.position = Vec3::new(0.0, 0.0, 11.0 - avgspeed * 0.55);
186        next.torso.orientation = Quaternion::rotation_x(
187            (((1.0 / switch) * PI / 2.0 + avg_vel.z * 0.12).min(PI / 2.0) - PI / 2.0)
188                + avgspeed * avg_vel.z * -0.003,
189        ) * Quaternion::rotation_y(tilt * 2.0)
190            * Quaternion::rotation_z(tilt * 3.0);
191
192        next
193    }
194}