Merge branch 'master' into 'master'

Implement animation smoothing

Closes #22

See merge request veloren/veloren!34

Former-commit-id: a7ae76993439e5538e9131e7c20843377af5973b
This commit is contained in:
Joshua Barretto 2019-04-16 19:10:57 +00:00
commit 9928ad6e52
5 changed files with 78 additions and 43 deletions

View File

@ -18,31 +18,34 @@ impl Animation for IdleAnimation {
type Dependency = f64; type Dependency = f64;
fn update_skeleton( fn update_skeleton(
skeleton: &mut Self::Skeleton, skeleton: &Self::Skeleton,
time: f64, time: f64,
) { ) -> Self::Skeleton {
skeleton.head.offset = Vec3::unit_z() * 13.0 / 11.0; let mut next = (*skeleton).clone();
skeleton.head.ori = Quaternion::rotation_z(0.0); next.head.offset = Vec3::unit_z() * 13.0 / 11.0;
next.head.ori = Quaternion::rotation_z(0.0);
skeleton.chest.offset = Vec3::unit_z() * 9.0 / 11.0; next.chest.offset = Vec3::unit_z() * 9.0 / 11.0;
skeleton.chest.ori = Quaternion::rotation_z(0.0); next.chest.ori = Quaternion::rotation_z(0.0);
skeleton.belt.offset = Vec3::unit_z() * 7.0 / 11.0; next.belt.offset = Vec3::unit_z() * 7.0 / 11.0;
skeleton.belt.ori = Quaternion::rotation_z(0.0); next.belt.ori = Quaternion::rotation_z(0.0);
skeleton.shorts.offset = Vec3::unit_z() * 4.0 / 11.0; next.shorts.offset = Vec3::unit_z() * 4.0 / 11.0;
skeleton.shorts.ori = Quaternion::rotation_z(0.0); next.shorts.ori = Quaternion::rotation_z(0.0);
skeleton.l_hand.offset = Vec3::new(-8.0, 0.0, 9.0) / 11.0; next.l_hand.offset = Vec3::new(-8.0, 0.0, 9.0) / 11.0;
skeleton.r_hand.offset = Vec3::new(8.0, 0.0, 9.0 ) / 11.0; next.r_hand.offset = Vec3::new(8.0, 0.0, 9.0 ) / 11.0;
skeleton.l_foot.offset = Vec3::new(-3.5, 0.0, 3.0) / 11.0; next.l_foot.offset = Vec3::new(-3.5, 0.0, 3.0) / 11.0;
skeleton.l_foot.ori = Quaternion::rotation_x(0.0); next.l_foot.ori = Quaternion::rotation_x(0.0);
skeleton.r_foot.offset = Vec3::new(3.5, 0.0, 3.0) / 11.0; next.r_foot.offset = Vec3::new(3.5, 0.0, 3.0) / 11.0;
skeleton.r_foot.ori = Quaternion::rotation_x(0.0); next.r_foot.ori = Quaternion::rotation_x(0.0);
skeleton.back.offset = Vec3::new(-9.0, 5.0, 18.0); next.back.offset = Vec3::new(-9.0, 5.0, 18.0);
skeleton.back.ori = Quaternion::rotation_y(2.5); next.back.ori = Quaternion::rotation_y(2.5);
skeleton.back.scale = Vec3::one(); next.back.scale = Vec3::one();
next
} }
} }

View File

@ -14,6 +14,7 @@ use super::{
Bone, Bone,
}; };
#[derive(Clone)]
pub struct CharacterSkeleton { pub struct CharacterSkeleton {
head: Bone, head: Bone,
chest: Bone, chest: Bone,
@ -65,4 +66,16 @@ impl Skeleton for CharacterSkeleton {
FigureBoneData::default(), FigureBoneData::default(),
] ]
} }
fn interpolate(&mut self, target: &Self) {
self.head.interpolate(&target.head);
self.chest.interpolate(&target.chest);
self.belt.interpolate(&target.belt);
self.shorts.interpolate(&target.shorts);
self.l_hand.interpolate(&target.l_hand);
self.r_hand.interpolate(&target.r_hand);
self.l_foot.interpolate(&target.l_foot);
self.r_foot.interpolate(&target.r_foot);
self.back.interpolate(&target.back);
}
} }

View File

@ -17,35 +17,39 @@ impl Animation for RunAnimation {
type Dependency = f64; type Dependency = f64;
fn update_skeleton( fn update_skeleton(
skeleton: &mut Self::Skeleton, skeleton: &Self::Skeleton,
time: f64, time: f64,
) { ) -> Self::Skeleton {
let mut next = (*skeleton).clone();
let wave = (time as f32 * 12.0).sin(); let wave = (time as f32 * 12.0).sin();
let wave_slow = (time as f32 * 6.0 + PI).sin(); let wave_slow = (time as f32 * 6.0 + PI).sin();
let wave_dip = (wave_slow.abs() - 0.5).abs(); let wave_dip = (wave_slow.abs() - 0.5).abs();
skeleton.head.offset = Vec3::unit_z() * 13.0 / 11.0; next.head.offset = Vec3::unit_z() * 13.0 / 11.0;
skeleton.head.ori = Quaternion::rotation_z(wave * 0.3); next.head.ori = Quaternion::rotation_z(wave * 0.3);
skeleton.chest.offset = Vec3::unit_z() * 9.0 / 11.0; next.chest.offset = Vec3::unit_z() * 9.0 / 11.0;
skeleton.chest.ori = Quaternion::rotation_z(wave * 0.3); next.chest.ori = Quaternion::rotation_z(wave * 0.3);
skeleton.belt.offset = Vec3::unit_z() * 7.0 / 11.0; next.belt.offset = Vec3::unit_z() * 7.0 / 11.0;
skeleton.belt.ori = Quaternion::rotation_z(wave * 0.2); next.belt.ori = Quaternion::rotation_z(wave * 0.2);
skeleton.shorts.offset = Vec3::unit_z() * 4.0 / 11.0; next.shorts.offset = Vec3::unit_z() * 4.0 / 11.0;
skeleton.shorts.ori = Quaternion::rotation_z(wave * 0.1); next.shorts.ori = Quaternion::rotation_z(wave * 0.1);
skeleton.l_hand.offset = Vec3::new(-6.0 - wave_dip * 6.0, wave * 5.0, 11.0 - wave_dip * 6.0) / 11.0; next.l_hand.offset = Vec3::new(-6.0 - wave_dip * 6.0, wave * 5.0, 11.0 - wave_dip * 6.0) / 11.0;
skeleton.r_hand.offset = Vec3::new(6.0 + wave_dip * 6.0, -wave * 5.0, 11.0 - wave_dip * 6.0) / 11.0; next.r_hand.offset = Vec3::new(6.0 + wave_dip * 6.0, -wave * 5.0, 11.0 - wave_dip * 6.0) / 11.0;
skeleton.l_foot.offset = Vec3::new(-3.5, 1.0 - wave * 8.0, 3.5 - wave_dip * 4.0) / 11.0; next.l_foot.offset = Vec3::new(-3.5, 1.0 - wave * 8.0, 3.5 - wave_dip * 4.0) / 11.0;
skeleton.l_foot.ori = Quaternion::rotation_x(-wave + 1.0); next.l_foot.ori = Quaternion::rotation_x(-wave + 1.0);
skeleton.r_foot.offset = Vec3::new(3.5, 1.0 + wave * 8.0, 3.5 - wave_dip * 4.0) / 11.0; next.r_foot.offset = Vec3::new(3.5, 1.0 + wave * 8.0, 3.5 - wave_dip * 4.0) / 11.0;
skeleton.r_foot.ori = Quaternion::rotation_x(wave + 1.0); next.r_foot.ori = Quaternion::rotation_x(wave + 1.0);
skeleton.back.offset = Vec3::new(-9.0, 5.0, 18.0); next.back.offset = Vec3::new(-9.0, 5.0, 18.0);
skeleton.back.ori = Quaternion::rotation_y(2.5); next.back.ori = Quaternion::rotation_y(2.5);
skeleton.back.scale = Vec3::one(); next.back.scale = Vec3::one();
next
} }
} }

View File

@ -25,18 +25,31 @@ impl Bone {
pub fn compute_base_matrix(&self) -> Mat4<f32> { pub fn compute_base_matrix(&self) -> Mat4<f32> {
Mat4::<f32>::translation_3d(self.offset) * Mat4::scaling_3d(self.scale) * Mat4::from(self.ori) Mat4::<f32>::translation_3d(self.offset) * Mat4::scaling_3d(self.scale) * Mat4::from(self.ori)
} }
/// Change the current bone to be more like `target`
fn interpolate(&mut self, target: &Bone) {
// TODO: Make configurable
let factor = 0.3;
self.offset += (target.offset - self.offset) * factor;
self.ori = vek::ops::Slerp::slerp(self.ori, target.ori, factor);
self.scale += (target.scale - self.scale) * factor;
}
} }
pub trait Skeleton: Send + Sync + 'static { pub trait Skeleton: Send + Sync + 'static {
fn compute_matrices(&self) -> [FigureBoneData; 16]; fn compute_matrices(&self) -> [FigureBoneData; 16];
/// Change the current skeleton to be more like `target`
fn interpolate(&mut self, target: &Self);
} }
pub trait Animation { pub trait Animation {
type Skeleton; type Skeleton;
type Dependency; type Dependency;
/// Returns a new skeleton that is generated by the animation
fn update_skeleton( fn update_skeleton(
skeleton: &mut Self::Skeleton, skeleton: &Self::Skeleton,
dependency: Self::Dependency, dependency: Self::Dependency,
); ) -> Self::Skeleton;
} }

View File

@ -94,10 +94,12 @@ impl Figures {
.entry(entity) .entry(entity)
.or_insert_with(|| FigureState::new(renderer, CharacterSkeleton::new())); .or_insert_with(|| FigureState::new(renderer, CharacterSkeleton::new()));
match animation { let target_skeleton = match animation {
comp::character::Animation::Idle => IdleAnimation::update_skeleton(&mut state.skeleton, time), comp::character::Animation::Idle => IdleAnimation::update_skeleton(&mut state.skeleton, time),
comp::character::Animation::Run => RunAnimation::update_skeleton(&mut state.skeleton, time), comp::character::Animation::Run => RunAnimation::update_skeleton(&mut state.skeleton, time),
} };
state.skeleton.interpolate(&target_skeleton);
state.update(renderer, pos.0, dir.0); state.update(renderer, pos.0, dir.0);
} }