From 4be19aa5bb2f3b935cbe477e6c40261edc6a96eb Mon Sep 17 00:00:00 2001 From: Thilo Behnke Date: Sat, 23 Apr 2022 00:32:57 +0200 Subject: [PATCH] wip collision handling --- pong/src/game_field.rs | 30 ++++++++++++++++++++---------- pong/src/game_object.rs | 4 ++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/pong/src/game_field.rs b/pong/src/game_field.rs index 87c0c5c..3c3af13 100644 --- a/pong/src/game_field.rs +++ b/pong/src/game_field.rs @@ -131,17 +131,20 @@ impl Field { } collision => objs.iter().find(|o| o.id == collision.0).unwrap(), }; - ball.obj.vel.add(&other.vel); - ball.obj.vel.invert(); - // let dot = ball.obj.vel.dot(&other.vel); - // if dot == 0. { - // ball.obj.vel.invert(); - // } else { - // let angle = ball.obj.vel.angle(&other.vel); - // ball.obj.vel.rotate(FRAC_PI_2 - angle); - // ball.obj.vel.invert(); - // } + if other.vel == Vector::zero() { + let dot = ball.obj.vel.dot(&other.orientation); + if dot >= - 0.000001 && dot <= 0.000001 { + ball.obj.vel.invert(); + } else { + let angle = ball.obj.vel.angle(&other.orientation); + ball.obj.vel.rotate(FRAC_PI_2 - angle); + ball.obj.vel.invert(); + } + } else { + ball.obj.vel.add(&other.vel); + ball.obj.vel.invert(); + } } } @@ -165,6 +168,7 @@ impl Player { obj: GameObject { id, pos: Vector {x: x as f64, y: y as f64}, + orientation: Vector::new(0., 1.), shape: Shape::Rect, shape_params: vec![field.width / 25, field.height / 5], vel: Vector::zero(), @@ -185,6 +189,7 @@ impl Ball { obj: GameObject { id, pos: Vector {x: x as f64, y: y as f64}, + orientation: Vector::zero(), shape: Shape::Circle, shape_params: vec![field.width / 80], vel: Vector::zero(), @@ -203,9 +208,11 @@ impl Bounds { pub fn new(width: u16, height: u16) -> Bounds { Bounds { objs: vec![ + // top GameObject { id: 90, pos: Vector {x: (width / 2) as f64, y: 0 as f64}, + orientation: Vector::new(1., 0.), shape: Shape::Rect, shape_params: vec![width, 2], is_static: true, @@ -215,6 +222,7 @@ impl Bounds { GameObject { id: 91, pos: Vector {x: (width / 2) as f64, y: height as f64}, + orientation: Vector::new(-1., 0.), shape: Shape::Rect, shape_params: vec![width, 2], is_static: true, @@ -224,6 +232,7 @@ impl Bounds { GameObject { id: 92, pos: Vector {x: 0 as f64, y: (height / 2) as f64}, + orientation: Vector::new(0., 1.), shape: Shape::Rect, shape_params: vec![2, height], is_static: true, @@ -233,6 +242,7 @@ impl Bounds { GameObject { id: 93, pos: Vector {x: width as f64, y: (height / 2) as f64}, + orientation: Vector::new(0., -1.), shape: Shape::Rect, shape_params: vec![2, height], is_static: true, diff --git a/pong/src/game_object.rs b/pong/src/game_object.rs index 59e2290..d6fb6ac 100644 --- a/pong/src/game_object.rs +++ b/pong/src/game_object.rs @@ -11,6 +11,7 @@ pub mod game_object { pub struct GameObject { pub id: u16, pub pos: Vector, + pub orientation: Vector, pub shape: Shape, pub shape_params: Vec, pub vel: Vector, @@ -21,6 +22,9 @@ pub mod game_object { pub fn update_pos(&mut self) { self.pos.add(&self.vel); + let mut orientation = self.pos.clone(); + orientation.invert(); + self.orientation = orientation; } pub fn set_vel_x(&mut self, x: f64) {