summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Kobert <d-kobert@web.de>2019-05-31 17:05:22 +0200
committerDennis Kobert <d-kobert@web.de>2019-05-31 17:05:22 +0200
commit2f7b8cfb0c48b600ebd80bdfb3ef9f04410b0b91 (patch)
treee618dff4fc6d211eaea795a537097f5964bcf029
parentc1e589d622ebb2e2209e2e984d141d185d32ee87 (diff)
Use references for Colision trait implementations
-rw-r--r--game_server/src/collide.rs49
-rw-r--r--game_server/src/maths.rs12
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<Vec2> for Vec2 {
impl Collide<Vec2> 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<Box> 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<Vec2> 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<Vec2> for RBox {
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)
+ 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<Vec2> 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<Vec2> 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<Vec2> 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,
}
}