From 8276c94169c7f6027691f124c63b61c815ab15a5 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Mon, 3 Jun 2019 20:30:59 +0200 Subject: Add further collision test cases --- game_server/src/collide.rs | 45 ++++++++++++++++++++++++++++++++++----------- game_server/src/maths.rs | 12 ++++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs index 9aab5c6..16b5357 100644 --- a/game_server/src/collide.rs +++ b/game_server/src/collide.rs @@ -65,8 +65,8 @@ impl Collide for RBox { 0.0 <= v1_dist_size && v1_dist <= 1.0 && 0.0 <= v2_dist_size && v2_dist <= 1.0 - && other.pos.x < maxx && minx < other.pos.x + other.size.x - && other.pos.y < maxy && miny < other.pos.y + other.size.y + && other.pos.x <= maxx && minx <= other.pos.x + other.size.x + && other.pos.y <= maxy && miny <= other.pos.y + other.size.y } } @@ -162,7 +162,7 @@ impl> Collide for Vec { } #[test] - fn test_not_collide_Rbox_dot() { + fn test_not_collide_rbox_dot() { let a = Vec2{x: 1.0, y: 1.0}; let b = Vec2{x: 1.0, y: 1.0}; let c = Vec2{x: 1.0, y: -1.0}; @@ -174,7 +174,7 @@ impl> Collide for Vec { } #[test] - fn test_collide_rbox_rbox_intersecting() { + fn test_collide_rbox_aabox_intersecting() { let a = Vec2{x: 1.0, y: 2.5}; let b = Vec2{x: 0.0, y: 2.5}; let c = Vec2{x: 3.0, y: 0.5}; @@ -187,20 +187,43 @@ impl> Collide for Vec { } #[test] - fn test_collide_rbox_rbox_crossed() { + fn test_collide_rbox_aabox_edges_touch() { + let a = Vec2{x: 4.0, y: 5.5}; + let b = Vec2{x: 1.0, y: 7.5}; + let aa_box = RBox::new(a, b, 3.9); + let a = Vec2{x: 0.0, y: 0.5}; + let b = Vec2{x: 4.0, y: 5.0}; + let bb_box = AABox{pos: a, size: b}; + + assert!(aa_box.collides(&bb_box)); + } + + #[test] + fn test_collide_rbox_aabox_crossed() { let a = Vec2{x: 2.0, y: 0.5}; - let b = Vec2{x: 1.0, y: 0.0}; - let c = Vec2{x: 0.0, y: 7.5}; - let aa_box = RBox{pos: a, v1: b, v2: c}; - let a = Vec2{x: 1.0, y: 3.5}; - let b = Vec2{x: 5.0, y: 4.5}; + let b = Vec2{x: 1.0, y: 7.5}; + let aa_box = RBox::new(a, b, 3.9); + let a = Vec2{x: 0.0, y: 4.5}; + let b = Vec2{x: 15.0, y: 1.5}; let bb_box = AABox{pos: a, size: b}; assert!(aa_box.collides(&bb_box)); } #[test] - fn test_not_collide_rbox_rbox() { + fn test_not_collide_rbox_aabox_next_to() { + let a = Vec2{x: 2.0, y: 0.5}; + let b = Vec2{x: 1.0, y: 7.5}; + let aa_box = RBox::new(a, b, 3.9); + let a = Vec2{x: 5.0, y: 40.5}; + let b = Vec2{x: 15.0, y: 1.5}; + let bb_box = AABox{pos: a, size: b}; + + assert!(!aa_box.collides(&bb_box)); + } + + #[test] + fn test_not_collide_rbox_aabox() { let a = Vec2{x: 1.0, y: 1.0}; let b = Vec2{x: 0.0, y: 1.0}; let c = Vec2{x: 1.0, y: 0.0}; diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs index 7c4ee2f..b9303af 100644 --- a/game_server/src/maths.rs +++ b/game_server/src/maths.rs @@ -180,6 +180,18 @@ pub struct RBox { pub v2: Vec2, } +impl RBox { + pub fn new(pos: Vec2, orientation: Vec2, width: f32) -> Self { + let scale = width / orientation.distance(); + let orth = Vec2 {x: orientation.x / scale, y: -orientation.y / scale}; + Self { + pos: pos, + v1: orientation, + v2: orth, + } + } +} + impl std::ops::Add for RBox { type Output = Self; fn add(self, other: Vec2) -> Self { -- cgit v1.2.3-54-g00ecf