From 79db730d4e42ce592af753059422410979ff26b0 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 14:53:37 +0200 Subject: tart implementing game logic --- game_server/src/maths.rs | 123 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 game_server/src/maths.rs (limited to 'game_server/src/maths.rs') diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs new file mode 100644 index 0000000..8844c6e --- /dev/null +++ b/game_server/src/maths.rs @@ -0,0 +1,123 @@ +#[derive(Clone, Copy)] +pub struct Vec2 { + pub x: f32, + pub y: f32, +} + +impl std::ops::Add for Vec2 { + type Output = Self; + fn add(self, other: Self) -> Self { + Self { + x: self.x + other.x, + y: self.y + other.y + } + } +} + +impl std::ops::Sub for Vec2 { + type Output = Self; + fn sub(self, other: Self) -> Self { + Self { + x: self.x - other.x, + y: self.y - other.y + } + } +} + +impl std::ops::Neg for Vec2 { + type Output = Self; + fn neg(self) -> Self { + Self { + x: -self.x, + y: -self.y + } + } +} + +impl std::cmp::PartialOrd for Vec2 { + fn partial_cmp(&self, other: &Self) -> Option { + Some(if self.x < other.x && self.y < other.y { + std::cmp::Ordering::Less + } else if self.x > other.x && self.y > other.y { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + }) + } +} + +impl std::cmp::PartialEq for Vec2 { + fn eq(&self, other: &Self) -> bool { + f32::abs(self.x - other.x) < 1e-8 + && f32::abs(self.y - other.y) < 1e-8 + } +} + +impl std::cmp::Eq for Vec2 {} + +impl Vec2 { + pub fn distance(&self) -> f32 { + f32::sqrt(self.distance2()) + } + + pub fn distance2(&self) -> f32 { + self.x * self.x + self.y * self.y + } +} + +pub struct Box { + pub pos: Vec2, + /// the size may not be smaller than zero + pub size: Vec2, +} + +impl std::ops::Add for Box { + type Output = Self; + fn add(self, other: Vec2) -> Self { + Self { + p1: self.p1 + other, + p2: self.p2 + other, + } + } +} + +impl std::ops::Sub for Box { + type Output = Self; + fn sub(self, other: Vec2) -> Self { + Self { + pos: self.pos + other, + size: self.size + other + } + } +} + +pub struct RBox { + /// Point 1 + pub p1: Vec2, + /// Point 2 + pub p2: Vec2, + /// Width + pub w: f32, +} + +impl std::ops::Add for RBox { + type Output = Self; + fn add(self, other: Vec2) -> Self { + Self { + p1: self.p1 + other, + p2: self.p2 + other, + w: self.w, + } + } +} + +impl std::ops::Sub for RBox { + type Output = Self; + fn sub(self, other: Vec2) -> Self { + Self { + p1: self.p1 + other, + p2: self.p2 + other, + w: self.w, + } + } +} -- cgit v1.2.3-54-g00ecf From 4cdb0d88a065df2456b3e12389836eebc9b2fa4a Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 16:40:00 +0200 Subject: Fix issues in Collide --- game_server/src/collide.rs | 63 ++++++++++++++++++++++++++++++++++++---------- game_server/src/main.rs | 3 --- game_server/src/maths.rs | 14 +++++------ 3 files changed, 57 insertions(+), 23 deletions(-) (limited to 'game_server/src/maths.rs') diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs index 4169787..4217e7f 100644 --- a/game_server/src/collide.rs +++ b/game_server/src/collide.rs @@ -1,36 +1,73 @@ +use crate::maths::{Vec2, Box, RBox}; + pub trait Collide { fn collides(&self, other: &Rhs) -> bool; } impl Collide for Vec2 { - fn collides(self, other: Self) { + fn collides(&self, other: &Self) -> bool { self == other } } impl Collide for Box { - fn collides(self, other: Vec2) { - self.pos < other < self.pos + self.size + fn collides(&self, other: &Vec2) -> bool { + self.pos < other && other < self.pos + self.size } } impl Collide for Box { - fn collides(self, other: Self) { - self.collides(other.pos) - || other.collides(self.pos) + 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) + + || 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) } } impl Collide for RBox { - fn collides(self, other: Vec2) { - - || other.pos < self.pos < other.pos + other.size + fn collides(&self, other: &Vec2) -> bool { + let dx = self.size.x; + let dy = self.size.y; + let len = f32::sqrt(dx*dx+dy*dy); + dx /= len; + dy /= len; + + let dax = other.x - self.p1.x; + let day = other.y - self.p1.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) { + return false; + } + + let ddx = other.x-px; + let ddy = other.y-py; + let manhattenDistance = ddx + ddy; + + manhattenDistance < self.w } } -impl Collide for Box { - fn collides(self, other: Self) { - self.pos < other.pos < self.pos + self.size - || other.pos < self.pos < other.pos + other.size +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) + } } diff --git a/game_server/src/main.rs b/game_server/src/main.rs index e10fe3c..cfd9787 100644 --- a/game_server/src/main.rs +++ b/game_server/src/main.rs @@ -1,12 +1,9 @@ mod group; mod maths; mod scribble_group; -<<<<<<< Updated upstream -======= mod webhogg_group; mod webhogg_game; mod collide; ->>>>>>> Stashed changes mod lobby; mod server; mod backend_connection; diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs index 8844c6e..a55c5ce 100644 --- a/game_server/src/maths.rs +++ b/game_server/src/maths.rs @@ -93,10 +93,10 @@ impl std::ops::Sub for Box { pub struct RBox { /// Point 1 - pub p1: Vec2, + pub pos: Vec2, /// Point 2 - pub p2: Vec2, - /// Width + pub size: Vec2, + /// Width Attention manhatten distance!!! pub w: f32, } @@ -104,8 +104,8 @@ impl std::ops::Add for RBox { type Output = Self; fn add(self, other: Vec2) -> Self { Self { - p1: self.p1 + other, - p2: self.p2 + other, + pos: self.p1 + other, + size: self.p2 + other, w: self.w, } } @@ -115,8 +115,8 @@ impl std::ops::Sub for RBox { type Output = Self; fn sub(self, other: Vec2) -> Self { Self { - p1: self.p1 + other, - p2: self.p2 + other, + pos: self.p1 + other, + size: self.p2 + other, w: self.w, } } -- cgit v1.2.3-54-g00ecf 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(-) (limited to 'game_server/src/maths.rs') 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 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/maths.rs') 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-54-g00ecf