From 76737c968b4210190a9a511bcadc64a14f7ea267 Mon Sep 17 00:00:00 2001 From: Thilo Behnke Date: Sun, 5 Jun 2022 18:25:41 +0200 Subject: [PATCH] wip - acceleration needed for ball? --- pong/src/game_field.rs | 2 +- pong/src/geom.rs | 20 ++++++++++++++++++++ pong/src/pong.rs | 17 ++++++++++++++--- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pong/src/game_field.rs b/pong/src/game_field.rs index d3744ee..6aa58ff 100644 --- a/pong/src/game_field.rs +++ b/pong/src/game_field.rs @@ -123,7 +123,7 @@ impl Field { continue; } if *obj_mut.vel() == Vector::zero() { - obj_mut.vel_mut().add(&Vector::new(-200., 0.)) + obj_mut.vel_mut().add(&Vector::new(-300., 0.)) } } diff --git a/pong/src/geom.rs b/pong/src/geom.rs index 711def8..97bba43 100644 --- a/pong/src/geom.rs +++ b/pong/src/geom.rs @@ -128,6 +128,26 @@ pub mod geom { let distance = self.x.powi(2) + self.y.powi(2); return (distance as f64).sqrt(); } + + pub fn multiply(&mut self, other: &Vector) { + self.x = self.x * other.x; + self.y = self.y * other.y; + } + + pub fn max(&mut self, other: &Vector) { + self.x = self.x.max(other.x); + self.y = self.y.max(other.y); + } + + pub fn min(&mut self, other: &Vector) { + self.x = self.x.min(other.x); + self.y = self.y.min(other.y); + } + + pub fn abs(&mut self) { + self.x = self.x.abs(); + self.y = self.y.abs(); + } } impl PartialEq for Vector { diff --git a/pong/src/pong.rs b/pong/src/pong.rs index 0a7c837..c273934 100644 --- a/pong/src/pong.rs +++ b/pong/src/pong.rs @@ -16,9 +16,20 @@ pub mod pong_collisions { ball.vel_mut().reflect(&player.orientation()); // use vel of player obj if *player.vel() != Vector::zero() { - let mut adjusted = player.vel().clone(); - adjusted.scalar_multiplication(0.3); - ball.vel_mut().add(&adjusted); + let mut collision_effect = player.vel().clone(); + collision_effect.scalar_multiplication(0.3); + + let mut ball_vel = ball.vel().clone(); + ball_vel.add(&collision_effect); + + let mut vel_ball_orientation = ball_vel.clone(); + vel_ball_orientation.normalize(); + + ball_vel.abs(); + ball_vel.min(&Vector::new(1000., 1000.)); + ball_vel.multiply(&vel_ball_orientation); + + *ball.vel_mut() = ball_vel } // move out of collision let mut b_to_a = ball.pos().clone();