summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Kobert <d-kobert@web.de>2019-05-31 16:40:00 +0200
committerDennis Kobert <d-kobert@web.de>2019-05-31 16:40:00 +0200
commit4cdb0d88a065df2456b3e12389836eebc9b2fa4a (patch)
tree1633fdda3fbad79a48952e8dab50181f4c00ef3c
parent79db730d4e42ce592af753059422410979ff26b0 (diff)
Fix issues in Collide
-rw-r--r--game_server/src/collide.rs63
-rw-r--r--game_server/src/main.rs3
-rw-r--r--game_server/src/maths.rs14
3 files changed, 57 insertions, 23 deletions
diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs
index 4169787..4217e7f 100644
--- a/game_server/src/collide.rs
+++ b/game_server/src/collide.rs
@@ -1,36 +1,73 @@
+use crate::maths::{Vec2, Box, RBox};
+
pub trait Collide<Rhs> {
fn collides(&self, other: &Rhs) -> bool;
}
impl Collide<Vec2> for Vec2 {
- fn collides(self, other: Self) {
+ fn collides(&self, other: &Self) -> bool {
self == other
}
}
impl Collide<Vec2> for Box {
- fn collides(self, other: Vec2) {
- self.pos < other < self.pos + self.size
+ fn collides(&self, other: &Vec2) -> bool {
+ self.pos < other && other < self.pos + self.size
}
}
impl Collide<Box> for Box {
- fn collides(self, other: Self) {
- self.collides(other.pos)
- || other.collides(self.pos)
+ fn collides(&self, other: &Self) -> bool {
+ self.collides(other.pos)
+ || self.collides(other.pos + Vec2{x: other.x, y: 0})
+ || self.collides(other.pos + Vec2{x: 0, y: other.y})
+ || self.collides(other.pos + other.size)
+
+ || other.collides(self.pos)
+ || other.collides(self.pos + Vec2{x: self.x, y: 0})
+ || other.collides(self.pos + Vec2{x: 0, y: self.y})
+ || other.collides(self.pos + self.size)
}
}
impl Collide<Vec2> for RBox {
- fn collides(self, other: Vec2) {
-
- || other.pos < self.pos < other.pos + other.size
+ fn collides(&self, other: &Vec2) -> bool {
+ let dx = self.size.x;
+ let dy = self.size.y;
+ let len = f32::sqrt(dx*dx+dy*dy);
+ dx /= len;
+ dy /= len;
+
+ let dax = other.x - self.p1.x;
+ let day = other.y - self.p1.y;
+
+ let dot = dax * dx + day * dy;
+ let px = self.pos.x + dx * dot;
+ let py = self.pos.y + dy * dot;
+
+ if !(self.pos < px && px < self.pos + self.size) {
+ return false;
+ }
+
+ let ddx = other.x-px;
+ let ddy = other.y-py;
+ let manhattenDistance = ddx + ddy;
+
+ manhattenDistance < self.w
}
}
-impl Collide<Box> for Box {
- fn collides(self, other: Self) {
- self.pos < other.pos < self.pos + self.size
- || other.pos < self.pos < other.pos + other.size
+impl Collide<Box> for RBox {
+ fn collides(&self, other: &Box) -> bool {
+ self.collides(other.pos)
+ || self.collides(other.pos + Vec2{x: other.x, y: 0})
+ || self.collides(other.pos + Vec2{x: 0, y: other.y})
+ || self.collides(other.pos + other.size)
+
+ || other.collides(self.pos)
+ || other.collides(self.pos + Vec2{x: self.x, y: 0})
+ || other.collides(self.pos + Vec2{x: 0, y: self.y})
+ || other.collides(self.pos + self.size)
+
}
}
diff --git a/game_server/src/main.rs b/game_server/src/main.rs
index e10fe3c..cfd9787 100644
--- a/game_server/src/main.rs
+++ b/game_server/src/main.rs
@@ -1,12 +1,9 @@
mod group;
mod maths;
mod scribble_group;
-<<<<<<< Updated upstream
-=======
mod webhogg_group;
mod webhogg_game;
mod collide;
->>>>>>> Stashed changes
mod lobby;
mod server;
mod backend_connection;
diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs
index 8844c6e..a55c5ce 100644
--- a/game_server/src/maths.rs
+++ b/game_server/src/maths.rs
@@ -93,10 +93,10 @@ impl std::ops::Sub<Vec2> for Box {
pub struct RBox {
/// Point 1
- pub p1: Vec2,
+ pub pos: Vec2,
/// Point 2
- pub p2: Vec2,
- /// Width
+ pub size: Vec2,
+ /// Width Attention manhatten distance!!!
pub w: f32,
}
@@ -104,8 +104,8 @@ impl std::ops::Add<Vec2> for RBox {
type Output = Self;
fn add(self, other: Vec2) -> Self {
Self {
- p1: self.p1 + other,
- p2: self.p2 + other,
+ pos: self.p1 + other,
+ size: self.p2 + other,
w: self.w,
}
}
@@ -115,8 +115,8 @@ impl std::ops::Sub<Vec2> for RBox {
type Output = Self;
fn sub(self, other: Vec2) -> Self {
Self {
- p1: self.p1 + other,
- p2: self.p2 + other,
+ pos: self.p1 + other,
+ size: self.p2 + other,
w: self.w,
}
}