This commit is contained in:
Ludvig Böklin 2021-05-16 19:00:54 +02:00
parent 6d4bf431a3
commit 20249c029e

View File

@ -53,12 +53,12 @@ fn collision(
// deformation // deformation
let v = (m1 * u1 + m2 * u2) / (m1 + m2); let v = (m1 * u1 + m2 * u2) / (m1 + m2);
(Vel(v1 - u1 + v), Vel(v2 - u2 + v)) (Vel(v1 - u1 + v), Vel(v2 - u2 + v))
// (Vel(v + vp1), Vel(v + vp2))
}, },
CollisionKind::Elastic => { CollisionKind::Elastic => {
// combined mass // combined mass
let m = m1 + m2; let m = m1 + m2;
// preserves total kinetic energy
( (
Vel(v1 - u1 + ((m1 - m2) * u1 + 2.0 * m2 * u2) / m), Vel(v1 - u1 + ((m1 - m2) * u1 + 2.0 * m2 * u2) / m),
Vel(v2 - u2 + ((m2 - m1) * u2 + 2.0 * m1 * u1) / m), Vel(v2 - u2 + ((m2 - m1) * u2 + 2.0 * m1 * u1) / m),
@ -523,41 +523,32 @@ impl<'a> PhysicsData<'a> {
let dir_from_other = let dir_from_other =
Dir::from_unnormalized(pos - pos_other) Dir::from_unnormalized(pos - pos_other)
.or(rel_mv_dir); .or(rel_mv_dir);
let d = rel_mv_dir
// only collide if moving towards the other
if rel_mv_dir
.and_then(|rel_vel| { .and_then(|rel_vel| {
Some(rel_vel.dot(*dir_from_other?)) Some(rel_vel.dot(*dir_from_other?))
}) })
.unwrap_or_default(); .map_or(false, |d| d.is_sign_negative())
{
if d < inline_tweak::tweak!(-0.01) { // temp hack to avoid more refactoring
// temp hack to avoid refactor
let vel_other = Vel(previous_cache_other let vel_other = Vel(previous_cache_other
.velocity_dt .velocity_dt
/ read.dt.0.max(0.001)); / read.dt.0.max(0.001));
// Change velocity // Set new velocity
vel.0 = collision( vel.0 = collision(
(mass, *vel), (mass, *vel),
(mass_other, vel_other), (mass_other, vel_other),
CollisionKind::Inelastic, if inline_tweak::tweak!(true) {
CollisionKind::Inelastic
} else {
CollisionKind::Elastic
},
dir_from_other.unwrap_or_default(), dir_from_other.unwrap_or_default(),
) )
.0 .0 // yes, a waste
.0; .0;
} }
// Pushback
if inline_tweak::tweak!(false) {
let speed: f32 = inline_tweak::tweak!(10.0);
let overlap = collision_dist - diff.magnitude();
// let a: f32 = inline_tweak::tweak!(-1.0);
if overlap.is_sign_positive() {
vel.0 += *dir_from_other.unwrap_or_default()
* speed
* mass_other.0
/ (mass.0 + mass_other.0)
* step_delta;
}
}
} }
collided = true; collided = true;