diff options
Diffstat (limited to 'game_server')
-rw-r--r-- | game_server/src/collide.rs | 4 | ||||
-rw-r--r-- | game_server/src/maths.rs | 171 |
2 files changed, 166 insertions, 9 deletions
diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs index 21ec99c..7439101 100644 --- a/game_server/src/collide.rs +++ b/game_server/src/collide.rs @@ -38,7 +38,7 @@ impl Collide<Vec2> for RBox { let day = other.y - self.pos.y; let dot = dax * da.x + day * da.y; - let px = self.pos.x + dx * dot; + let px = self.pos.x + da.x * dot; let py = self.pos.y + da.y * dot; let p = Vec2{x: px, y: py}; @@ -55,7 +55,7 @@ impl Collide<Vec2> for RBox { } impl Collide<AABox> for RBox { - fn collides(&self, other: &Box) -> bool { + fn collides(&self, other: &AABox) -> bool { 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})) diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs index 7b844dc..66c5bca 100644 --- a/game_server/src/maths.rs +++ b/game_server/src/maths.rs @@ -1,4 +1,4 @@ -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] pub struct Vec2 { pub x: f32, pub y: f32, @@ -14,6 +14,13 @@ impl std::ops::Add for Vec2 { } } +impl std::ops::AddAssign<Vec2> for Vec2 { + fn add_assign(&mut self, other: Vec2) { + self.x += other.x; + self.y += other.y; + } +} + impl std::ops::Sub for Vec2 { type Output = Self; fn sub(self, other: Self) -> Self { @@ -24,6 +31,13 @@ impl std::ops::Sub for Vec2 { } } +impl std::ops::SubAssign<Vec2> for Vec2 { + fn sub_assign(&mut self, other: Vec2) { + self.x -= other.x; + self.y -= other.y; + } +} + impl std::ops::Neg for Vec2 { type Output = Self; fn neg(self) -> Self { @@ -36,13 +50,13 @@ impl std::ops::Neg for Vec2 { impl std::cmp::PartialOrd for Vec2 { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { - Some(if self.x < other.x && self.y < other.y { - std::cmp::Ordering::Less + if self.x < other.x && self.y < other.y { + Some(std::cmp::Ordering::Less) } else if self.x > other.x && self.y > other.y { - std::cmp::Ordering::Greater + Some(std::cmp::Ordering::Greater) } else { - std::cmp::Ordering::Equal - }) + None + } } } @@ -65,7 +79,7 @@ impl Vec2 { } pub fn norm(&self) -> Vec2 { - let len = self.size.distance(); + let len = self.distance(); Vec2 { x: self.x / len, y: self.y / len, @@ -73,6 +87,7 @@ impl Vec2 { } } +#[derive(Clone, Copy, Debug)] pub struct AABox { pub pos: Vec2, /// the size may not be smaller than zero @@ -84,10 +99,17 @@ impl std::ops::Add<Vec2> for AABox { fn add(self, other: Vec2) -> Self { Self { pos: self.pos + other, + size: self.size, } } } +impl std::ops::AddAssign<Vec2> for AABox { + fn add_assign(&mut self, other: Vec2) { + self.pos += other + } +} + impl std::ops::Sub<Vec2> for AABox { type Output = Self; fn sub(self, other: Vec2) -> Self { @@ -98,6 +120,22 @@ impl std::ops::Sub<Vec2> for AABox { } } +impl std::ops::SubAssign<Vec2> for AABox { + fn sub_assign(&mut self, other: Vec2) { + self.pos -= other + } +} + +impl std::cmp::PartialEq for AABox { + fn eq(&self, other: &Self) -> bool { + self.pos == other.pos + && self.size == other.size + } +} + +impl std::cmp::Eq for AABox {} + +#[derive(Clone, Copy, Debug)] pub struct RBox { /// Point 1 pub pos: Vec2, @@ -118,6 +156,12 @@ impl std::ops::Add<Vec2> for RBox { } } +impl std::ops::AddAssign<Vec2> for RBox { + fn add_assign(&mut self, other: Vec2) { + self.pos += other + } +} + impl std::ops::Sub<Vec2> for RBox { type Output = Self; fn sub(self, other: Vec2) -> Self { @@ -128,3 +172,116 @@ impl std::ops::Sub<Vec2> for RBox { } } } + +impl std::ops::SubAssign<Vec2> for RBox { + fn sub_assign(&mut self, other: Vec2) { + self.pos -= other + } +} + +impl std::cmp::PartialEq for RBox { + fn eq(&self, other: &Self) -> bool { + self.pos == other.pos + && self.size == other.size + && f32::abs(self.w - other.w) < 1e-8 + } +} + +impl std::cmp::Eq for RBox {} + +#[cfg(test)] +mod tests { + // Note this useful idiom: importing names from outer (for mod tests) scope. + use super::*; + + #[test] + fn test_add_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let c = Vec2{x: -2.0, y: 10.0}; + + assert_eq!(a + b, c); + } + + #[test] + fn test_neg_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -1.0, y: -7.5}; + + assert_eq!(-a, b); + } + + #[test] + fn test_sub_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let c = Vec2{x: 4.0, y: 5.0}; + + assert_eq!(a - b, c); + } + + #[test] + fn test_distance_vec2() { + let a = Vec2{x: 2.0, y: 2.0}; + + assert!(f32::abs(a.distance() - 2.0) < 1e8); + } + + #[test] + fn test_distance2_vec2() { + let a = Vec2{x: 1.0, y: 2.0}; + + assert!(f32::abs(a.distance2() - 5.0) < 1e8); + } + #[test] + fn test_norm_vec2() { + let a = Vec2{x: 2.0, y: -2.0}; + let b = Vec2{x: std::f32::consts::FRAC_1_SQRT_2, y: -std::f32::consts::FRAC_1_SQRT_2}; + + assert_eq!(a.norm(), b); + } + + #[test] + fn test_add_aabox_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let mut aa_box = AABox{pos: a, size: b}; + let bb_box = AABox{pos: a + b,size: b}; + aa_box += b; + + assert_eq!(aa_box, bb_box); + } + + #[test] + fn test_sub_aabox_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let mut aa_box = AABox{pos: a, size: b}; + let bb_box = AABox{pos: a - b,size: b}; + aa_box -= b; + + assert_eq!(aa_box, bb_box); + } + + #[test] + fn test_add_rbox_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let mut aa_box = RBox{pos: a, size: b, w: 8.0}; + let bb_box = RBox{pos: a + b,size: b, w: 8.0}; + aa_box += b; + + assert_eq!(aa_box, bb_box); + } + + #[test] + fn test_sub_rbox_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let mut aa_box = RBox{pos: a, size: b, w: 8.0}; + let bb_box = RBox{pos: a - b,size: b, w: 8.0}; + aa_box -= b; + + assert_eq!(aa_box, bb_box); + } +}
\ No newline at end of file |