enhancement/improve-ball-bound-collisions

This commit is contained in:
Thilo Behnke
2022-07-03 17:38:00 +02:00
parent f6fe13e476
commit bf0b2173b6
2 changed files with 13 additions and 1 deletions

View File

@@ -154,6 +154,12 @@ pub mod vector {
self.x = self.x.abs();
self.y = self.y.abs();
}
pub fn switch(&mut self) {
let tmp = self.x;
self.x = self.y;
self.y = tmp;
}
}
impl PartialEq for Vector {

View File

@@ -56,8 +56,14 @@ pub mod pong_collisions {
ball.vel_mut().reflect(&bound.orientation());
// move out of collision
let mut bound_orientation = bound.orientation().clone();
bound_orientation.switch();
bound_orientation.abs();
let mut bound_pos = bound.pos().clone();
bound_pos.multiply(&bound_orientation);
let mut b_to_a = ball.pos().clone();
b_to_a.sub(&bound.pos());
b_to_a.sub(&bound_pos);
b_to_a.normalize();
b_to_a.scalar_multiplication(5.);
ball.pos_mut().add(&b_to_a);