Skip to content

Commit

Permalink
Smooth the effects of collision push-out on the camera using eye disp…
Browse files Browse the repository at this point in the history
…lacement.

Could probably use more tuning but it works okay.
  • Loading branch information
kpreid committed Oct 12, 2023
1 parent 7df2723 commit f2ebc91
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions all-is-cubes/src/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,20 @@ impl Character {

let colliding_cubes = &mut self.colliding_cubes;
colliding_cubes.clear();
Some(self.body.step_with_rerun(
let info = self.body.step_with_rerun(
tick,
Some(&*space),
|cube| {
colliding_cubes.insert(cube);
},
#[cfg(feature = "rerun")]
&self.rerun_destination,
))
);
if let Some(push_out_displacement) = info.push_out {
// Smooth out camera effect of push-outs
self.eye_displacement_pos -= push_out_displacement;
}
Some(info)
} else {
// TODO: set a warning flag
None
Expand Down Expand Up @@ -380,9 +385,13 @@ impl Character {
// First, update velocity.
let body_delta_v_this_frame = self.body.velocity - initial_body_velocity;
self.eye_displacement_vel -= body_delta_v_this_frame.cast_unit() * 0.04;
self.eye_displacement_vel += self.eye_displacement_pos.cast_unit() * -(0.005f64.powf(dt));
self.eye_displacement_vel *= 0.005f64.powf(dt);
// Then apply position to velocity.
// Return-to-center force — linear near zero and increasing quadratically
self.eye_displacement_vel -= self.eye_displacement_pos.cast_unit()
* (self.eye_displacement_pos.length() + 1.0)
* 1e21_f64.powf(dt);
// Damping.
self.eye_displacement_vel *= 1e-9_f64.powf(dt);
// Finally, apply velocity to position.
self.eye_displacement_pos += self.eye_displacement_vel.cast_unit() * dt;
// TODO: Clamp eye_displacement_pos to be within the body AAB.

Expand Down

0 comments on commit f2ebc91

Please sign in to comment.