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;
52        let tempo = if speed > 0.5 { 1.5 } else { 0.7 };
53        let intensity = if speed > 0.5 { 1.0 } else { 0.3 };
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 * 6.0 + PI * -0.1).sin();
60
61        let footrotl = ((1.0 / (0.2 + (0.8) * ((anim_time * 6.0 * lab + PI * 1.4).sin()).powi(2)))
62            .sqrt())
63            * ((anim_time * 6.0 * lab + PI * 1.4).sin());
64
65        let footrotr = ((1.0 / (0.2 + (0.8) * ((anim_time * 6.0 * lab + PI * 0.4).sin()).powi(2)))
66            .sqrt())
67            * ((anim_time * 6.0 * lab + PI * 0.4).sin());
68
69        let foothoril = (anim_time * 6.0 * lab + PI * 1.4).sin();
70        let foothorir = (anim_time * 6.0 * lab + PI * (0.4)).sin();
71        let head_look = Vec2::new(
72            (global_time + anim_time / 4.0 * (1.0 / tempo))
73                .floor()
74                .mul(7331.0)
75                .sin()
76                * 0.2,
77            (global_time + anim_time / 4.0 * (1.0 / tempo))
78                .floor()
79                .mul(1337.0)
80                .sin()
81                * 0.1,
82        );
83        let ori: Vec2<f32> = Vec2::from(orientation);
84        let last_ori = Vec2::from(last_ori);
85        let tilt = if vek::Vec2::new(ori, last_ori)
86            .map(|o| o.magnitude_squared())
87            .map(|m| m > 0.001 && m.is_finite())
88            .reduce_and()
89            && ori.angle_between(last_ori).is_finite()
90        {
91            ori.angle_between(last_ori).min(0.8)
92                * last_ori.determine_side(Vec2::zero(), ori).signum()
93        } else {
94            0.0
95        } * 1.3;
96        let abstilt = tilt.abs();
97
98        let squash = if abstilt > 0.2 { 0.35 } else { 1.0 }; //condenses the body at strong turns
99        next.head.position = Vec3::new(0.0, s_a.head.0, s_a.head.1 - 1.0 + short * 0.3);
100        next.head.orientation =
101            Quaternion::rotation_z(head_look.x * 0.3 + short * -0.2 * intensity + tilt * 3.0)
102                * Quaternion::rotation_x(
103                    (0.4 * head_look.y * (1.0 / intensity)).abs()
104                        + 0.45 * intensity
105                        + velocity.z * 0.03
106                        - (abstilt * 1.8).min(0.0),
107                );
108        next.head.scale = Vec3::one() * s_a.head_scale;
109
110        next.chest.position = Vec3::new(
111            0.0,
112            s_a.chest.0,
113            -10.0 + s_a.chest.1 + short * 0.3 * intensity,
114        );
115        next.chest.orientation = Quaternion::rotation_z(short * 0.1 * intensity);
116
117        next.belt.position = Vec3::new(0.0, s_a.belt.0, s_a.belt.1);
118        next.belt.orientation = Quaternion::rotation_x(velocity.z.abs() * -0.005 + abstilt * 1.0)
119            * Quaternion::rotation_z(short * -0.2 * intensity);
120
121        next.back.position = Vec3::new(0.0, s_a.back.0, s_a.back.1);
122        next.back.scale = Vec3::one() * 1.02;
123
124        next.shorts.position = Vec3::new(0.0, s_a.shorts.0, s_a.shorts.1);
125        next.shorts.orientation = Quaternion::rotation_x(velocity.z.abs() * -0.005 + abstilt * 1.0)
126            * Quaternion::rotation_z(short * -0.3 * intensity);
127
128        next.hand_l.position = Vec3::new(
129            -1.0 - s_a.hand.0,
130            1.5 + s_a.hand.1 - foot * 2.0 * intensity * squash,
131            intensity * 5.0 + s_a.hand.2 + foot * -5.0 * intensity * squash,
132        );
133        next.hand_l.orientation = Quaternion::rotation_x(1.5 + foot * -1.2 * intensity * squash)
134            * Quaternion::rotation_y(0.4 + foot * -0.35);
135        next.hand_l.scale = Vec3::one() * 1.04;
136
137        next.hand_r.position = Vec3::new(
138            1.0 + s_a.hand.0,
139            1.5 + s_a.hand.1 + foot * 2.0 * intensity * squash,
140            intensity * 5.0 + s_a.hand.2 + foot * 5.0 * intensity * squash,
141        );
142        next.hand_r.orientation = Quaternion::rotation_x(1.5 + foot * 1.2 * intensity * squash)
143            * Quaternion::rotation_y(-0.4 + foot * -0.35);
144        next.hand_r.scale = Vec3::one() * 1.04;
145
146        next.foot_l.position = Vec3::new(
147            -s_a.foot.0,
148            s_a.foot.1 + foothoril * 1.5 * intensity * squash,
149            -10.0 + s_a.foot.2 + footrotl * 3.0 * intensity * squash,
150        );
151        next.foot_l.orientation =
152            Quaternion::rotation_x(-0.8 * squash + footrotl * 0.4 * intensity * squash);
153
154        next.foot_r.position = Vec3::new(
155            s_a.foot.0,
156            s_a.foot.1 + foothorir * 1.5 * intensity * squash,
157            -10.0 + s_a.foot.2 + footrotr * 3.0 * intensity * squash,
158        );
159        next.foot_r.orientation =
160            Quaternion::rotation_x(-0.8 * squash + footrotr * 0.4 * intensity * squash);
161
162        next.shoulder_l.position = Vec3::new(-s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
163        next.shoulder_l.orientation = Quaternion::rotation_x(short * 0.15 * intensity);
164        next.shoulder_l.scale = Vec3::one() * 1.1;
165
166        next.shoulder_r.position = Vec3::new(s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
167        next.shoulder_r.orientation = Quaternion::rotation_x(short * -0.15 * intensity);
168        next.shoulder_r.scale = Vec3::one() * 1.1;
169
170        next.glider.position = Vec3::new(0.0, 0.0, 10.0);
171        next.glider.scale = Vec3::one() * 0.0;
172
173        next.do_tools_on_back(hands, active_tool_kind, second_tool_kind);
174
175        next.lantern.position = Vec3::new(s_a.lantern.0, s_a.lantern.1, s_a.lantern.2);
176        next.lantern.scale = Vec3::one() * 0.65;
177        next.hold.scale = Vec3::one() * 0.0;
178
179        let switch = if avg_vel.z > 0.0 && avgspeed < 0.5 {
180            avgtotal.min(0.5)
181        } else {
182            avgtotal
183        };
184        next.torso.position = Vec3::new(0.0, 0.0, 11.0 - avgspeed * 0.55);
185        next.torso.orientation = Quaternion::rotation_x(
186            (((1.0 / switch) * PI / 2.0 + avg_vel.z * 0.12).min(PI / 2.0) - PI / 2.0)
187                + avgspeed * avg_vel.z * -0.003,
188        ) * Quaternion::rotation_y(tilt * 2.0)
189            * Quaternion::rotation_z(tilt * 3.0);
190
191        next
192    }
193}