diff options
Diffstat (limited to 'webhogg/game_server/src')
-rw-r--r-- | webhogg/game_server/src/collide.rs | 28 | ||||
-rw-r--r-- | webhogg/game_server/src/main.rs | 2 | ||||
-rw-r--r-- | webhogg/game_server/src/math.rs (renamed from webhogg/game_server/src/maths.rs) | 39 | ||||
-rw-r--r-- | webhogg/game_server/src/webhogg_game.rs | 2 |
4 files changed, 35 insertions, 36 deletions
diff --git a/webhogg/game_server/src/collide.rs b/webhogg/game_server/src/collide.rs index 16b5357..940cffd 100644 --- a/webhogg/game_server/src/collide.rs +++ b/webhogg/game_server/src/collide.rs @@ -1,4 +1,4 @@ -use crate::maths::{Vec2, AABox, RBox}; +use crate::math::{Vec2, AABox, RBox}; pub trait Collide<Rhs> { fn collides(&self, other: &Rhs) -> bool; @@ -12,7 +12,7 @@ impl Collide<Vec2> for Vec2 { impl Collide<Vec2> for AABox { fn collides(&self, other: &Vec2) -> bool { - self.pos < *other && other < &(self.pos + self.size) + self.pos < *other && other < &(self.pos + self.size) } } @@ -25,8 +25,8 @@ impl Collide<AABox> for AABox { impl Collide<Vec2> for RBox { fn collides(&self, other: &Vec2) -> bool { - let v1_diff = *other + self.v1 * (-self.v1.scalar(&(*other - self.pos)) / self.v1.distance2()); - let v2_diff = *other + self.v2 * (-self.v2.scalar(&(*other - self.pos)) / self.v2.distance2()); + let v1_diff = *other + self.v1 * (-self.v1.dot(&(*other - self.pos)) / self.v1.norm2()); + let v2_diff = *other + self.v2 * (-self.v2.dot(&(*other - self.pos)) / self.v2.norm2()); let v1_dist = ((v1_diff - self.pos) / self.v2).x; let v2_dist = ((v2_diff - self.pos) / self.v1).x; @@ -42,16 +42,16 @@ impl Collide<AABox> for RBox { let other_size = other.pos + other.size; // project points onto a orthogonal line - let v1_diff = other.pos + self.v1 * (-self.v1.scalar(&(other.pos - self.pos)) / self.v1.distance2()); - let v2_diff = other.pos + self.v2 * (-self.v2.scalar(&other.pos) / self.v2.distance2()); - let v1_diff_size = other_size + self.v1 * (-self.v1.scalar(&(other_size - self.pos)) / self.v1.distance2()); - let v2_diff_size = other_size + self.v2 * (-self.v2.scalar(&(other_size - self.pos)) / self.v2.distance2()); + let v1_diff = other.pos + self.v1 * (-self.v1.dot(&(other.pos - self.pos)) / self.v1.norm2()); + let v2_diff = other.pos + self.v2 * (-self.v2.dot(&other.pos) / self.v2.norm2()); + let v1_diff_size = other_size + self.v1 * (-self.v1.dot(&(other_size - self.pos)) / self.v1.norm2()); + let v2_diff_size = other_size + self.v2 * (-self.v2.dot(&(other_size - self.pos)) / self.v2.norm2()); - // calculate the distance - let v1_dist = ((v1_diff - self.pos) / self.v2); - let v2_dist = ((v2_diff - self.pos) / self.v1); - let v1_dist_size = ((v1_diff_size - self.pos) / self.v2); - let v2_dist_size = ((v2_diff_size - self.pos) / self.v1); + // calculate the norm + let v1_dist = (v1_diff - self.pos) / self.v2; + let v2_dist = (v2_diff - self.pos) / self.v1; + let v1_dist_size = (v1_diff_size - self.pos) / self.v2; + let v2_dist_size = (v2_diff_size - self.pos) / self.v1; let v1_dist = if v1_dist.x.is_finite() {v1_dist.x} else {v1_dist.y}; let v2_dist = if v2_dist.x.is_finite() {v2_dist.x} else {v2_dist.y}; @@ -150,7 +150,7 @@ impl<S, T: Collide<S>> Collide<S> for Vec<T> { } #[test] - fn test_collide_Rbox_dot() { + fn test_collide_rbox_dot() { let a = Vec2{x: 1.0, y: 1.0}; let b = Vec2{x: 1.0, y: 1.0}; let c = Vec2{x: 1.0, y: -1.0}; diff --git a/webhogg/game_server/src/main.rs b/webhogg/game_server/src/main.rs index cfd9787..2db757b 100644 --- a/webhogg/game_server/src/main.rs +++ b/webhogg/game_server/src/main.rs @@ -1,5 +1,5 @@ mod group; -mod maths; +mod math; mod scribble_group; mod webhogg_group; mod webhogg_game; diff --git a/webhogg/game_server/src/maths.rs b/webhogg/game_server/src/math.rs index b9303af..6d9f1e1 100644 --- a/webhogg/game_server/src/maths.rs +++ b/webhogg/game_server/src/math.rs @@ -101,20 +101,20 @@ impl std::cmp::PartialEq for Vec2 { impl std::cmp::Eq for Vec2 {} impl Vec2 { - pub fn distance(&self) -> f32 { - f32::sqrt(self.distance2()) + pub fn norm(&self) -> f32 { + f32::hypot(self.x, self.y) } - pub fn distance2(&self) -> f32 { - self.scalar(self) + pub fn norm2(&self) -> f32 { + self.dot(self) } - pub fn scalar(&self, other: &Vec2) -> f32 { + pub fn dot(&self, other: &Vec2) -> f32 { self.x * other.x + self.y * other.y } - pub fn norm(&self) -> Vec2 { - let len = self.distance(); + pub fn normalized(&self) -> Vec2 { + let len = self.norm(); Vec2 { x: self.x / len, y: self.y / len, @@ -149,7 +149,7 @@ impl std::ops::Sub<Vec2> for AABox { type Output = Self; fn sub(self, other: Vec2) -> Self { Self { - pos: self.pos + other, + pos: self.pos - other, size: self.size } } @@ -174,7 +174,7 @@ impl std::cmp::Eq for AABox {} pub struct RBox { /// origin pub pos: Vec2, - /// Vwctor1 + /// Vector1 pub v1: Vec2, /// Vector2 pub v2: Vec2, @@ -182,7 +182,7 @@ pub struct RBox { impl RBox { pub fn new(pos: Vec2, orientation: Vec2, width: f32) -> Self { - let scale = width / orientation.distance(); + let scale = width / orientation.norm(); let orth = Vec2 {x: orientation.x / scale, y: -orientation.y / scale}; Self { pos: pos, @@ -213,8 +213,8 @@ impl std::ops::Sub<Vec2> for RBox { type Output = Self; fn sub(self, other: Vec2) -> Self { Self { - pos: self.pos + other, - v1: self.v1 + other, + pos: self.pos - other, + v1: self.v1, v2: self.v2, } } @@ -230,7 +230,7 @@ impl std::cmp::PartialEq for RBox { fn eq(&self, other: &Self) -> bool { self.pos == other.pos && self.v1 == other.v1 - && self.v1 == self.v2 + && self.v2 == other.v2 } } @@ -273,7 +273,6 @@ mod tests { assert!(!(a > b)); } - #[test] fn test_add_vec2() { let a = Vec2{x: 1.0, y: 7.5}; @@ -301,25 +300,25 @@ mod tests { } #[test] - fn test_distance_vec2() { + fn test_norm_vec2() { let a = Vec2{x: 2.0, y: 2.0}; - assert!(f32::abs(a.distance() - 2.0) < 1e8); + assert!(f32::abs(a.norm() - 2.0) < 1e8); } #[test] - fn test_distance2_vec2() { + fn test_norm2_vec2() { let a = Vec2{x: 1.0, y: 2.0}; - assert!(f32::abs(a.distance2() - 5.0) < 1e8); + assert!(f32::abs(a.norm2() - 5.0) < 1e8); } #[test] - fn test_norm_vec2() { + fn test_normalized_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); + assert_eq!(a.normalized(), b); } #[test] diff --git a/webhogg/game_server/src/webhogg_game.rs b/webhogg/game_server/src/webhogg_game.rs index 7b94fcb..b3f35cc 100644 --- a/webhogg/game_server/src/webhogg_game.rs +++ b/webhogg/game_server/src/webhogg_game.rs @@ -1,4 +1,4 @@ -use crate::maths::Vec2; +use crate::math::Vec2; pub struct WebhoggPlayer { pos: Vec2, |