From 2f7b8cfb0c48b600ebd80bdfb3ef9f04410b0b91 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 17:05:22 +0200 Subject: Use references for Colision trait implementations --- game_server/src/collide.rs | 49 +++++++++++++++++++++++----------------------- game_server/src/maths.rs | 12 ++++++------ 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs index 4217e7f..ac046dd 100644 --- a/game_server/src/collide.rs +++ b/game_server/src/collide.rs @@ -12,40 +12,41 @@ impl Collide for Vec2 { impl Collide for Box { fn collides(&self, other: &Vec2) -> bool { - self.pos < other && other < self.pos + self.size + self.pos < *other && other < &(self.pos + self.size) } } impl Collide for Box { 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) + self.collides(&other.pos) + || self.collides(&(other.pos + Vec2{x: other.size.x, y: 0.0})) + || self.collides(&(other.pos + Vec2{x: 0.0, y: other.size.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) + || other.collides(&(self.pos)) + || other.collides(&(self.pos + Vec2{x: self.size.x, y: 0.0})) + || other.collides(&(self.pos + Vec2{x: 0.0, y: self.size.y})) + || other.collides(&(self.pos + self.size)) } } impl Collide for RBox { fn collides(&self, other: &Vec2) -> bool { - let dx = self.size.x; - let dy = self.size.y; - let len = f32::sqrt(dx*dx+dy*dy); + let mut dx = self.size.x; + let mut dy = self.size.y; + let len = self.size.distance(); dx /= len; dy /= len; - let dax = other.x - self.p1.x; - let day = other.y - self.p1.y; + let dax = other.x - self.pos.x; + let day = other.y - self.pos.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) { + let p = Vec2{x: px, y: py}; + if !(self.pos < p && p < self.pos + self.size) { return false; } @@ -59,15 +60,15 @@ impl Collide for RBox { impl Collide 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) + self.collides(&other.pos) + || self.collides(&(other.pos + Vec2{x: other.size.x, y: 0.0})) + || self.collides(&(other.pos + Vec2{x: 0.0, y: other.size.y})) + || self.collides(&(other.pos + other.size)) + + || other.collides(&(self.pos)) + || other.collides(&(self.pos + Vec2{x: self.size.x, y: 0.0})) + || other.collides(&(self.pos + Vec2{x: 0.0, y: self.size.y})) + || other.collides(&(self.pos + self.size)) } } diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs index a55c5ce..f5ceab4 100644 --- a/game_server/src/maths.rs +++ b/game_server/src/maths.rs @@ -75,8 +75,8 @@ impl std::ops::Add for Box { type Output = Self; fn add(self, other: Vec2) -> Self { Self { - p1: self.p1 + other, - p2: self.p2 + other, + pos: self.pos + other, + size: self.size + other, } } } @@ -104,8 +104,8 @@ impl std::ops::Add for RBox { type Output = Self; fn add(self, other: Vec2) -> Self { Self { - pos: self.p1 + other, - size: self.p2 + other, + pos: self.pos + other, + size: self.size + other, w: self.w, } } @@ -115,8 +115,8 @@ impl std::ops::Sub for RBox { type Output = Self; fn sub(self, other: Vec2) -> Self { Self { - pos: self.p1 + other, - size: self.p2 + other, + pos: self.pos + other, + size: self.size + other, w: self.w, } } -- cgit v1.2.3-54-g00ecf