veloren_voxygen_anim/theropod/
run.rs1use super::{
2 super::{Animation, vek::*},
3 SkeletonAttr, TheropodSkeleton,
4};
5use core::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 = TheropodSkeleton;
12
13 #[cfg(feature = "use-dyn-lib")]
14 const UPDATE_FN: &'static [u8] = b"theropod_run\0";
15
16 #[cfg_attr(feature = "be-dyn-lib", export_name = "theropod_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).powf(0.25);
30 let mixed_vel = acc_vel + anim_time * 6.0; let speedmult = 1.0;
33 let lab: f32 = 0.45; let short = ((1.0
36 / (0.72
37 + 0.28 * ((mixed_vel * 1.0 * lab * speedmult + PI * -0.15 - 0.5).sin()).powi(2)))
38 .sqrt())
39 * ((mixed_vel * 1.0 * lab * speedmult + PI * -0.15 - 0.5).sin())
40 * speednorm;
41
42 let shortalt = (mixed_vel * 1.0 * lab * speedmult + PI * 3.0 / 8.0 - 0.5).sin() * speednorm;
44
45 let foot1a = (mixed_vel * 1.0 * lab * speedmult + 0.0 + PI).sin() * speednorm; let foot1b = (mixed_vel * 1.0 * lab * speedmult + PI / 2.0 + PI).sin() * speednorm; let foot2a = (mixed_vel * 1.0 * lab * speedmult).sin() * speednorm; let foot2b = (mixed_vel * 1.0 * lab * speedmult + PI / 2.0).sin() * speednorm; let ori: Vec2<f32> = Vec2::from(orientation);
52 let last_ori = Vec2::from(last_ori);
53 let tilt = if vek::Vec2::new(ori, last_ori)
54 .map(|o| o.magnitude_squared())
55 .map(|m| m > 0.001 && m.is_finite())
56 .reduce_and()
57 && ori.angle_between(last_ori).is_finite()
58 {
59 ori.angle_between(last_ori).min(0.2)
60 * last_ori.determine_side(Vec2::zero(), ori).signum()
61 } else {
62 0.0
63 } * 1.3;
64 let x_tilt = avg_vel.z.atan2(avg_vel.xy().magnitude()) * speednorm;
65
66 next.head.scale = Vec3::one() * 1.02;
67 next.neck.scale = Vec3::one() * 0.98;
68 next.jaw.scale = Vec3::one() * 0.98;
69 next.foot_l.scale = Vec3::one() * 0.96;
70 next.foot_r.scale = Vec3::one() * 0.96;
71
72 next.head.position = Vec3::new(0.0, s_a.head.0, s_a.head.1);
73 next.head.orientation = Quaternion::rotation_x(-0.1 * speednorm + short * -0.05)
74 * Quaternion::rotation_y(tilt * 0.8)
75 * Quaternion::rotation_z(shortalt * -0.2 - tilt * 2.5);
76
77 next.jaw.position = Vec3::new(0.0, s_a.jaw.0, s_a.jaw.1);
78 next.jaw.orientation = Quaternion::rotation_x(short * -0.03);
79
80 next.neck.position = Vec3::new(0.0, s_a.neck.0, s_a.neck.1);
81 next.neck.orientation = Quaternion::rotation_x(-0.1 * speednorm + short * -0.04)
82 * Quaternion::rotation_y(tilt * 0.3)
83 * Quaternion::rotation_z(shortalt * -0.1 - tilt * 3.2);
84
85 next.chest_front.position = Vec3::new(
86 0.0,
87 s_a.chest_front.0,
88 s_a.chest_front.1 + short * 0.5 + x_tilt * 10.0 + 0.5 * speednorm,
89 );
90 next.chest_front.orientation = Quaternion::rotation_x(short * 0.07 + x_tilt)
91 * Quaternion::rotation_y(tilt * 0.8)
92 * Quaternion::rotation_z(shortalt * 0.15 + tilt * -1.5);
93
94 next.chest_back.position = Vec3::new(0.0, s_a.chest_back.0, s_a.chest_back.1);
95 next.chest_back.orientation = Quaternion::rotation_x(short * -0.04)
96 * Quaternion::rotation_y(tilt * 0.6)
97 * Quaternion::rotation_z(shortalt * -0.15 + tilt * 0.6);
98
99 next.tail_front.position = Vec3::new(0.0, s_a.tail_front.0, s_a.tail_front.1);
100 next.tail_front.orientation = Quaternion::rotation_x(0.1 + short * -0.02)
101 * Quaternion::rotation_z(shortalt * -0.1 + tilt * 1.0);
102
103 next.tail_back.position = Vec3::new(0.0, s_a.tail_back.0, s_a.tail_back.1);
104 next.tail_back.orientation = Quaternion::rotation_x(0.2 + short * -0.1)
105 * Quaternion::rotation_z(shortalt * -0.2 + tilt * 1.4);
106 if s_a.steady_wings {
107 next.hand_l.position = Vec3::new(-s_a.hand.0 - 8.0, s_a.hand.1, s_a.hand.2);
108 next.hand_l.orientation =
109 Quaternion::rotation_x(-0.2 * speednorm) * Quaternion::rotation_z(-0.6);
110
111 next.hand_r.position = Vec3::new(s_a.hand.0 + 8.0, s_a.hand.1, s_a.hand.2);
112 next.hand_r.orientation =
113 Quaternion::rotation_x(-0.2 * speednorm) * Quaternion::rotation_z(0.6);
114 } else {
115 next.hand_l.position = Vec3::new(-s_a.hand.0, s_a.hand.1, s_a.hand.2);
116 next.hand_l.orientation = Quaternion::rotation_x(-0.2 * speednorm + foot2a * 0.3);
117
118 next.hand_r.position = Vec3::new(s_a.hand.0, s_a.hand.1, s_a.hand.2);
119 next.hand_r.orientation = Quaternion::rotation_x(-0.2 * speednorm + foot1a * 0.3);
120 };
121 next.leg_l.position = Vec3::new(
122 -s_a.leg.0 + speednorm * 1.5,
123 s_a.leg.1 + foot1b * -2.3,
124 s_a.leg.2 * 1.5 + foot1a.max(0.0) * 3.0,
125 );
126 next.leg_l.orientation = Quaternion::rotation_x(-0.2 * speednorm + foot1a * 0.15)
127 * Quaternion::rotation_y(tilt * 0.5)
128 * Quaternion::rotation_z(foot1a * -0.3 + tilt * -0.5);
129
130 next.leg_r.position = Vec3::new(
131 s_a.leg.0 + speednorm * -1.5,
132 s_a.leg.1 + foot2b * -2.3,
133 s_a.leg.2 * 1.5 + foot2a.max(0.0) * 3.0,
134 );
135 next.leg_r.orientation = Quaternion::rotation_x(-0.2 * speednorm + foot2a * 0.15)
136 * Quaternion::rotation_y(tilt * 0.5)
137 * Quaternion::rotation_z(foot2a * 0.3 + tilt * -0.5);
138
139 next.foot_l.position = Vec3::new(
140 -s_a.foot.0,
141 s_a.foot.1 + foot1b * -2.0,
142 s_a.foot.2 + speednorm * 0.5 + (foot1a * 1.5).max(0.0),
143 );
144 next.foot_l.orientation =
145 Quaternion::rotation_x(0.2 + foot2b * 0.6) * Quaternion::rotation_y(tilt * -1.0);
146
147 next.foot_r.position = Vec3::new(
148 s_a.foot.0,
149 s_a.foot.1 + foot2b * -2.0,
150 s_a.foot.2 + speednorm * 0.5 + (foot2a * 1.5).max(0.0),
151 );
152 next.foot_r.orientation =
153 Quaternion::rotation_x(0.2 + foot1b * 0.6) * Quaternion::rotation_y(tilt * -1.0);
154
155 next
156 }
157}