summaryrefslogtreecommitdiff
path: root/game_server/src/maths.rs
diff options
context:
space:
mode:
authornatrixaeria <janng@gmx.de>2019-06-01 02:47:21 +0200
committernatrixaeria <janng@gmx.de>2019-06-01 02:47:21 +0200
commit0f7da3a189085cd16f90b210d0bb06798f715a57 (patch)
tree26f5e80f642df040bf3f33d4c6508c64730a8db9 /game_server/src/maths.rs
parentde3d86164acaa9aeb78f28e3201ff1a40b212f04 (diff)
parent04ed6c8ec40307b72151daff54079bbc185a2cec (diff)
Merge branch 'webhogg' of https://github.com/TrueDoctor/DiscoBot into webhogg
Diffstat (limited to 'game_server/src/maths.rs')
-rw-r--r--game_server/src/maths.rs130
1 files changed, 130 insertions, 0 deletions
diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs
new file mode 100644
index 0000000..7b844dc
--- /dev/null
+++ b/game_server/src/maths.rs
@@ -0,0 +1,130 @@
+#[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<std::cmp::Ordering> {
+ 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 fn norm(&self) -> Vec2 {
+ let len = self.size.distance();
+ Vec2 {
+ x: self.x / len,
+ y: self.y / len,
+ }
+ }
+}
+
+pub struct AABox {
+ pub pos: Vec2,
+ /// the size may not be smaller than zero
+ pub size: Vec2,
+}
+
+impl std::ops::Add<Vec2> for AABox {
+ type Output = Self;
+ fn add(self, other: Vec2) -> Self {
+ Self {
+ pos: self.pos + other,
+ }
+ }
+}
+
+impl std::ops::Sub<Vec2> for AABox {
+ type Output = Self;
+ fn sub(self, other: Vec2) -> Self {
+ Self {
+ pos: self.pos + other,
+ size: self.size
+ }
+ }
+}
+
+pub struct RBox {
+ /// Point 1
+ pub pos: Vec2,
+ /// Point 2
+ pub size: Vec2,
+ /// Width Attention manhatten distance!!!
+ pub w: f32,
+}
+
+impl std::ops::Add<Vec2> for RBox {
+ type Output = Self;
+ fn add(self, other: Vec2) -> Self {
+ Self {
+ pos: self.pos + other,
+ size: self.size,
+ w: self.w,
+ }
+ }
+}
+
+impl std::ops::Sub<Vec2> for RBox {
+ type Output = Self;
+ fn sub(self, other: Vec2) -> Self {
+ Self {
+ pos: self.pos + other,
+ size: self.size + other,
+ w: self.w,
+ }
+ }
+}