diff --git a/src/input.rs b/src/input.rs index 9a2b6ca..04ad4f1 100644 --- a/src/input.rs +++ b/src/input.rs @@ -408,22 +408,48 @@ impl InputState { let mut positions = self.pressed_touch_positions.iter(); let (id_1, pos_1) = positions.next().unwrap(); let (id_2, pos_2) = positions.next().unwrap(); - let mut vec_1 = None; - let mut vec_2 = None; - for input in &self.touch_inputs { - if input.id == *id_1 { vec_1 = Some(input.touch_location - pos_1) } - if input.id == *id_2 { vec_2 = Some(input.touch_location - pos_2) } + let mut touch_loc_1 = vec2(0., 0.); + let mut touch_loc_2 = vec2(0., 0.); + let mut touch_1 = None; + let mut touch_2 = None; + + fn filtered_vector_sub(a: Vector2, b: &Vector2) -> Option> { + Some(a - b).filter(|v| v.magnitude() > 0.01) } - match (vec_1, vec_2) { + + for input in &self.touch_inputs { + if input.id == *id_1 { + touch_1 = filtered_vector_sub(input.touch_location, &pos_1); + touch_loc_1 = input.touch_location; + } + if input.id == *id_2 { + touch_2 = filtered_vector_sub(input.touch_location, &pos_2); + touch_loc_2 = input.touch_location; + } + } + + let diff = touch_loc_2 - touch_loc_1; + let norm_n = vec2(-diff.y, diff.x).normalize(); + + match (touch_1, touch_2) { (Some(v1), Some(v2)) => { - let movement_length = (v1.magnitude() + v2.magnitude()) / 2.0; - let opposing = f64::abs(-v1.dot(v2)); - let rotating = 1.0; - (movement_length * opposing * rotating) as f32 + let v1_n = v1.normalize(); + let v2_n = v2.normalize(); + + let direction = v1_n.dot(norm_n) - v2_n.dot(norm_n); + let rotation = f64::abs(direction); + let distance = (v1.magnitude() + v2.magnitude()) / 2.; + (rotation * distance * f64::signum(direction)) as f32 }, - (Some(v1), None) => v1.magnitude() as f32, - (None, Some(v2)) => v2.magnitude() as f32, - (None, None) => 0.0 + (Some(v1), None) => { + let direction = v1.normalize().dot(norm_n); + (v1.magnitude() * f64::signum(direction)) as f32 + }, + (None, Some(v2)) => { + let direction = v2.normalize().dot(norm_n); + (v2.magnitude() * f64::signum(direction)) as f32 + }, + (None, None) => { 0.0 } } } else { 0.0