summaryrefslogtreecommitdiff
path: root/2026/games/rpc2dot0/src/math.rs
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2026-08-29 04:06:51 -0400
committerLLLL Colonq <llll@colonq>2026-08-29 04:06:51 -0400
commit761a3172bf4c15aed3836e4d1cd6f815b651b0e0 (patch)
treecc90491529e83349d84d42a8e9982e5f0f38338e /2026/games/rpc2dot0/src/math.rs
parent0d2d8cd897e94bd94e0b3ae7c6ce9aa580a44a0a (diff)
Fix
Diffstat (limited to '2026/games/rpc2dot0/src/math.rs')
-rw-r--r--2026/games/rpc2dot0/src/math.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/2026/games/rpc2dot0/src/math.rs b/2026/games/rpc2dot0/src/math.rs
new file mode 100644
index 0000000..555204f
--- /dev/null
+++ b/2026/games/rpc2dot0/src/math.rs
@@ -0,0 +1,55 @@
+pub use glam::Vec2;
+
+pub trait BoundingCircle {
+ fn center(&self) -> Vec2;
+ fn radius(&self) -> f32;
+
+ // Intersection between two bounding circles.
+ // compares squared distance to squared sum of radiuSUS (*insert mrGreen.png*),
+ // tl;dr: a naive collision detection.
+ fn overlaps_with(&self, other: &impl BoundingCircle) -> bool {
+ let radius_sum = self.radius() + other.radius();
+ self.center().distance_squared(other.center()) <= radius_sum * radius_sum
+ }
+}
+
+#[derive(Clone, Copy)]
+pub struct Rectangle {
+ pub min: Vec2,
+ pub max: Vec2,
+}
+
+pub trait ContainsPoint {
+ fn contains(&self, point: Vec2) -> bool;
+}
+
+impl ContainsPoint for Rectangle {
+ fn contains(&self, point: Vec2) -> bool {
+ point.cmpge(self.min).all() && point.cmple(self.max).all()
+ }
+}
+
+impl Rectangle {
+ pub const fn from_xywh(x: f32, y: f32, width: f32, height: f32) -> Self {
+ Self {
+ min: Vec2::new(x, y),
+ max: Vec2::new(x + width, y + height),
+ }
+ }
+
+ pub const fn from_min_max(min: Vec2, max: Vec2) -> Self {
+ Self { min, max }
+ }
+
+ #[allow(dead_code)]
+ pub fn overlaps(&self, other: &Rectangle) -> bool {
+ self.min.cmple(other.max).all() && self.max.cmpge(other.min).all()
+ }
+}
+
+pub fn parse_coords(coords: &str) -> (f32, f32) {
+ let mut parts = coords.split(',');
+ let x: f32 = parts.next().unwrap_or("0").parse().unwrap_or(0.0);
+ let y: f32 = parts.next().unwrap_or("0").parse().unwrap_or(0.0);
+ (x, y)
+}