diff options
Diffstat (limited to 'game_server/src/collide.rs')
-rw-r--r-- | game_server/src/collide.rs | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs index ac046dd..21ec99c 100644 --- a/game_server/src/collide.rs +++ b/game_server/src/collide.rs @@ -1,4 +1,4 @@ -use crate::maths::{Vec2, Box, RBox}; +use crate::maths::{Vec2, AABox, RBox}; pub trait Collide<Rhs> { fn collides(&self, other: &Rhs) -> bool; @@ -10,13 +10,13 @@ impl Collide<Vec2> for Vec2 { } } -impl Collide<Vec2> for Box { +impl Collide<Vec2> for AABox { fn collides(&self, other: &Vec2) -> bool { self.pos < *other && other < &(self.pos + self.size) } } -impl Collide<Box> for Box { +impl Collide<AABox> for AABox { fn collides(&self, other: &Self) -> bool { self.collides(&other.pos) || self.collides(&(other.pos + Vec2{x: other.size.x, y: 0.0})) @@ -32,18 +32,14 @@ impl Collide<Box> for Box { impl Collide<Vec2> for RBox { fn collides(&self, other: &Vec2) -> bool { - let mut dx = self.size.x; - let mut dy = self.size.y; - let len = self.size.distance(); - dx /= len; - dy /= len; + let da = self.size.norm(); let dax = other.x - self.pos.x; let day = other.y - self.pos.y; - let dot = dax * dx + day * dy; + let dot = dax * da.x + day * da.y; let px = self.pos.x + dx * dot; - let py = self.pos.y + dy * dot; + let py = self.pos.y + da.y * dot; let p = Vec2{x: px, y: py}; if !(self.pos < p && p < self.pos + self.size) { @@ -58,7 +54,7 @@ impl Collide<Vec2> for RBox { } } -impl Collide<Box> for RBox { +impl Collide<AABox> for RBox { fn collides(&self, other: &Box) -> bool { self.collides(&other.pos) || self.collides(&(other.pos + Vec2{x: other.size.x, y: 0.0})) @@ -72,3 +68,9 @@ impl Collide<Box> for RBox { } } + +impl<S, T: Collide<S>> Collide<S> for Vec<T> { + fn collides(&self, other: &S) -> bool { + self.iter().any(|x| x.collides(other)) + } +} |