mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-02-14 14:39:51 +00:00
enhancement/improve-ball-bound-collisions
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user