Skip to content

Commit f026333

Browse files
committed
It now works flawlessly. I just want to make it handle as many rotations as you throw at it, and then it's done
1 parent fbb8fc3 commit f026333

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/cube_rotation.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cmp::Ordering;
12
use std::time::{Duration, Instant};
23

34
use bevy::prelude::*;
@@ -20,7 +21,7 @@ pub(crate) struct RotationData {
2021
pub(crate) struct RotationState {
2122
#[derivative(Default(value = "CartesianDirection::Y"))]
2223
top: CartesianDirection,
23-
#[derivative(Default(value = "CartesianDirection::X"))]
24+
#[derivative(Default(value = "CartesianDirection::Z"))]
2425
side: CartesianDirection,
2526
}
2627

@@ -73,9 +74,8 @@ impl RotationAnimationData {
7374
let animation_progress =
7475
(Instant::now() - self.animation_started).as_secs_f64() / rotation_time.as_secs_f64();
7576
let rotation_amount = rotation_curve(animation_progress as f32);
76-
self.from.as_vec3() * (1.-rotation_amount) + self.target.as_vec3() * (rotation_amount)
77+
self.from.as_vec3() * (1. - rotation_amount) + self.target.as_vec3() * (rotation_amount)
7778
}
78-
7979
}
8080

8181
pub(crate) fn iterate(
@@ -96,22 +96,32 @@ pub(crate) fn iterate(
9696
for mut camera in &mut query {
9797
let mut transform = camera.0;
9898
transform.translation = rotation_data.rotation_state.camera_location() * 2.;
99-
// The camera rotates in the opposite direction from how the cube would have rotated to get
100-
// to the same place
99+
100+
// Needed to prevent dropping this value
101+
let animations = [
102+
rotation_data.top_rotation_animation,
103+
rotation_data.side_rotation_animation,
104+
];
105+
let mut animations = animations
106+
.iter()
107+
.flatten()
108+
.collect::<Vec<&RotationAnimationData>>();
109+
// Sort by when the animation started. If this is not done, the animations may be added in the wrong order resulting in wrong rotation
110+
animations.sort_by(|c1, c2| {
111+
if c1.animation_started < c2.animation_started {
112+
Ordering::Greater
113+
} else {
114+
Ordering::Less
115+
}
116+
});
101117
transform.translate_around(
102118
Vec3::ZERO,
103-
total_animation_rotation(
104-
&[
105-
rotation_data.top_rotation_animation,
106-
rotation_data.side_rotation_animation,
107-
],
108-
rotation_duration,
109-
),
119+
total_animation_rotation(&animations, rotation_duration),
110120
);
111121

112122
transform.look_at(
113123
Vec3::new(0., 0., 0.),
114-
camera_up_vector(rotation_data, rotation_duration)
124+
camera_up_vector(rotation_data, rotation_duration),
115125
);
116126

117127
camera.0 = transform;
@@ -192,12 +202,13 @@ fn start_rotation(rotation_data: &mut RotationData, rotation: CartesianDirection
192202
}
193203

194204
fn total_animation_rotation(
195-
animations: &[Option<RotationAnimationData>],
205+
animations: &[&RotationAnimationData],
196206
rotation_time: Duration,
197207
) -> Quat {
198208
let mut output = Quat::IDENTITY;
209+
// let mut output = animations.iter().flatten().last().map_or(Quat::IDENTITY, |p|p.partial_camera_translation(rotation_time));
199210
// Iterate without Nones
200-
for animation in animations.iter().flatten() {
211+
for animation in animations {
201212
output *= animation.partial_camera_translation(rotation_time);
202213
}
203214
output
@@ -209,8 +220,8 @@ fn camera_up_vector(rotation_data: &RotationData, rotation_time: Duration) -> Ve
209220
rotation_data.top_rotation_animation,
210221
rotation_data.side_rotation_animation,
211222
]
212-
.iter()
213-
.flatten()
223+
.iter()
224+
.flatten()
214225
{
215226
let top = rotation_data.rotation_state.top;
216227
if (animation.target.is_parallel_to(top) || animation.from.is_parallel_to(top))
@@ -230,10 +241,10 @@ fn rotation_curve(time: f32) -> f32 {
230241
// return 0.;
231242
// }
232243
// time
233-
//
244+
234245
let c1 = 1.70158;
235246
let c3 = c1 + 1.;
236-
247+
237248
1. + c3 * (time - 1.).powi(3) + c1 * (time - 1.).powi(2)
238249
}
239250

0 commit comments

Comments
 (0)