Skip to content

Commit 9120771

Browse files
committed
Try to fix rotation... Again
1 parent ac52005 commit 9120771

File tree

1 file changed

+53
-46
lines changed

1 file changed

+53
-46
lines changed

src/cube_rotation.rs

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,59 @@ pub(crate) fn rotate(
3232
let time = &*time;
3333
let rotation_data = &mut *rotation_data;
3434
let rotation_duration = 1.;
35-
let mut input_handling = |keycode: KeyCode, axis: CartesianDirection, camera_rotation: i32| {
36-
if input.just_pressed(keycode) {
37-
let axis = direction_after_camera_turn(
38-
axis.abs(),
39-
rotation_data.current_rotation,
40-
rotation_data.current_camera_up,
41-
0,
42-
)
43-
.expect("Current rotation does not have anything other than quarter turns");
44-
let axis_num = axis.axis_num() as usize;
45-
if rotation_data.time_started_rotations[axis_num].is_zero()
46-
&& (rotation_data.time_started_rotations[3].is_zero() || camera_rotation == 0)
47-
{
48-
rotation_data.reversed_axes[axis_num] = axis.is_negative();
49-
rotation_data.time_started_rotations[axis_num] = time.elapsed();
50-
// if $camera_rotation != 0 {
51-
// rotation_data.time_started_rotations[3] = time.elapsed();
52-
// rotation_data.reversed_axes[3] = $camera_rotation == -1;
53-
// rotation_data.camera_rotated_times += $camera_rotation;
54-
// }
55-
}
35+
36+
dbg!(
37+
&rotation_data,
38+
rotation_data.current_rotation.mul_vec3(Vec3::splat(1.))
39+
);
40+
41+
let mut input_handling =
42+
|keycode: KeyCode, axis: CartesianDirection, camera_rotation: i32, reversed: bool| {
43+
if input.just_pressed(keycode) {
44+
let axis_rotated = direction_after_camera_turn(
45+
axis.abs(),
46+
rotation_data.current_rotation,
47+
rotation_data.current_camera_up,
48+
0,
49+
)
50+
.expect("Current rotation does not have anything other than quarter turns");
51+
let axis_num = axis_rotated.axis_num() as usize;
52+
if rotation_data.time_started_rotations[axis_num].is_zero()
53+
&& (rotation_data.time_started_rotations[3].is_zero() || camera_rotation == 0)
54+
{
55+
rotation_data.reversed_axes[axis_num] = reversed;
56+
rotation_data.time_started_rotations[axis_num] = time.elapsed();
57+
if camera_rotation != 0 {
58+
rotation_data.time_started_rotations[3] = time.elapsed();
59+
rotation_data.reversed_axes[3] = camera_rotation == -1;
60+
}
61+
}
62+
};
5663
};
57-
};
5864

5965
// Input
60-
input_handling(KeyCode::Left, CartesianDirection::Y, 0);
61-
input_handling(KeyCode::Right, CartesianDirection::NegY, 0);
62-
input_handling(KeyCode::Down, CartesianDirection::NegZ, 1);
63-
input_handling(KeyCode::Up, CartesianDirection::Z, -1);
66+
input_handling(KeyCode::Left, CartesianDirection::Y, 0, true);
67+
input_handling(KeyCode::Right, CartesianDirection::Y, 0, false);
68+
input_handling(KeyCode::Down, CartesianDirection::Z, 1, false);
69+
input_handling(KeyCode::Up, CartesianDirection::Z, -1, true);
6470
if input.just_pressed(KeyCode::Space) {
6571
rotation_data.time_started_rotations[3] = time.elapsed();
6672
rotation_data.reversed_axes[3] = input.pressed(KeyCode::A);
6773
}
6874

6975
let mut rotation_needed = rotation_data.current_rotation;
7076
let mut camera_rotation_up_needed = rotation_data.current_camera_up.as_vec3();
77+
// Has to happen before world axes so that the rotation is the same on even the last one
78+
animate_camera_rotation(
79+
time,
80+
&mut rotation_data.time_started_rotations[3],
81+
&mut rotation_data.current_camera_up,
82+
rotation_duration,
83+
&mut camera_rotation_up_needed,
84+
rotation_data.reversed_axes[3],
85+
rotation_data.current_rotation,
86+
);
87+
7188
// Animate world axes
7289
// x-axis
7390
animate_axis(
@@ -100,23 +117,14 @@ pub(crate) fn rotate(
100117
rotation_data.reversed_axes[2],
101118
);
102119

103-
animate_camera_rotation(
104-
time,
105-
&mut rotation_data.time_started_rotations[3],
106-
&mut rotation_data.current_camera_up,
107-
rotation_duration,
108-
&mut camera_rotation_up_needed,
109-
rotation_data.reversed_axes[3],
110-
rotation_data.current_rotation,
111-
);
112-
120+
dbg!(rotation_needed.mul_vec3(Vec3::splat(1.)));
113121
// Apply the rotation
114122
for mut camera in &mut query {
115123
let mut transform = camera.0;
116124
transform.translation = camera.1.start_coords;
117125
// The camera rotates in the opposite direction from how the cube would have rotated to get
118126
// to the same place
119-
transform.translate_around(Vec3::new(0., 0., 0.), rotation_needed.inverse());
127+
transform.translate_around(Vec3::new(0., 0., 0.), rotation_needed);
120128

121129
transform.look_at(Vec3::new(0., 0., 0.), camera_rotation_up_needed);
122130

@@ -149,7 +157,8 @@ fn animate_camera_rotation(
149157
if reversed { 2 } else { 1 },
150158
)
151159
.unwrap();
152-
let target = direction_after_rotation(target, rotation).unwrap();
160+
dbg!(target);
161+
// let target = direction_after_rotation(target, rotation).unwrap();
153162

154163
let quat_path = Quat::from_rotation_arc(current_camera_up.as_vec3(), target.as_vec3());
155164
let rotation_amount = rotation_curve(time_elapsed.as_secs_f32() / rotation_duration)
@@ -216,7 +225,8 @@ fn to_the_side_from_camera_perspective(
216225
let axis = CartesianDirection::from_vec3_round(side).unwrap();
217226
if current_camera_up.axis_num() == i as u32 {
218227
if current_camera_up != axis {
219-
// The side camera up is not visible
228+
error!("The side camera up is not visible");
229+
dbg!(camera_loc, axis);
220230
return None;
221231
}
222232
continue;
@@ -239,11 +249,12 @@ fn rotation_curve(time: f32) -> f32 {
239249
if time <= 0. {
240250
return 0.;
241251
}
252+
time
242253

243-
let c1 = 1.70158;
244-
let c3 = c1 + 1.;
254+
// let c1 = 1.70158;
255+
// let c3 = c1 + 1.;
245256

246-
1. + c3 * (time - 1.).powi(3) + c1 * (time - 1.).powi(2)
257+
// 1. + c3 * (time - 1.).powi(3) + c1 * (time - 1.).powi(2)
247258
}
248259

249260
fn direction_after_rotation(
@@ -313,10 +324,6 @@ fn animate_axis(
313324
}
314325
}
315326

316-
fn reset_everything() {
317-
todo!()
318-
}
319-
320327
mod tests {
321328
#[test]
322329
fn new_axis_on_side_after_rotation_test() {

0 commit comments

Comments
 (0)