From 2f99c5e8b3cfcba29894505d25d8d6363d92ed3f Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sat, 1 Jun 2019 02:02:10 +0200 Subject: Adress issues specified in pull qeruest --- game_server/src/collide.rs | 24 +++++++++++++----------- game_server/src/maths.rs | 19 +++++++++++++------ 2 files changed, 26 insertions(+), 17 deletions(-) (limited to 'game_server/src') 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 { fn collides(&self, other: &Rhs) -> bool; @@ -10,13 +10,13 @@ impl Collide for Vec2 { } } -impl Collide for Box { +impl Collide for AABox { fn collides(&self, other: &Vec2) -> bool { self.pos < *other && other < &(self.pos + self.size) } } -impl Collide for Box { +impl Collide 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 for Box { impl Collide 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 for RBox { } } -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.size.x, y: 0.0})) @@ -72,3 +68,9 @@ impl Collide for RBox { } } + +impl> Collide for Vec { + fn collides(&self, other: &S) -> bool { + self.iter().any(|x| x.collides(other)) + } +} diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs index f5ceab4..7b844dc 100644 --- a/game_server/src/maths.rs +++ b/game_server/src/maths.rs @@ -63,30 +63,37 @@ impl Vec2 { pub fn distance2(&self) -> f32 { self.x * self.x + self.y * self.y } + + pub fn norm(&self) -> Vec2 { + let len = self.size.distance(); + Vec2 { + x: self.x / len, + y: self.y / len, + } + } } -pub struct Box { +pub struct AABox { pub pos: Vec2, /// the size may not be smaller than zero pub size: Vec2, } -impl std::ops::Add for Box { +impl std::ops::Add for AABox { type Output = Self; fn add(self, other: Vec2) -> Self { Self { pos: self.pos + other, - size: self.size + other, } } } -impl std::ops::Sub for Box { +impl std::ops::Sub for AABox { type Output = Self; fn sub(self, other: Vec2) -> Self { Self { pos: self.pos + other, - size: self.size + other + size: self.size } } } @@ -105,7 +112,7 @@ impl std::ops::Add for RBox { fn add(self, other: Vec2) -> Self { Self { pos: self.pos + other, - size: self.size + other, + size: self.size, w: self.w, } } -- cgit v1.2.3-70-g09d2