From 0f4b8fcb25c054f89620ffaad067538b49a78bb8 Mon Sep 17 00:00:00 2001 From: Paris DOUADY Date: Wed, 5 Jun 2024 21:05:07 +0200 Subject: [PATCH] add moments of inertia --- geom/src/aabb.rs | 7 +++++++ geom/src/circle.rs | 6 ++++++ geom/src/obb.rs | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/geom/src/aabb.rs b/geom/src/aabb.rs index a89454b3..c4e4a37d 100644 --- a/geom/src/aabb.rs +++ b/geom/src/aabb.rs @@ -88,6 +88,13 @@ impl AABB { } } + #[inline] + /// Calculates the moment of inertia of the AABB around its center. + pub fn moment_of_inertia(&self, mass: f32) -> f32 { + let Vec2 { x: w, y: h } = self.size(); + mass * (w * w + h * h) / 12.0 + } + #[inline] pub fn size(&self) -> Vec2 { self.ur - self.ll diff --git a/geom/src/circle.rs b/geom/src/circle.rs index c1344cc3..12c2e5a8 100644 --- a/geom/src/circle.rs +++ b/geom/src/circle.rs @@ -8,9 +8,15 @@ pub struct Circle { } impl Circle { + #[inline] pub fn contains(&self, p: Vec2) -> bool { self.center.is_close(p, self.radius) } + + #[inline] + pub fn moment_of_inertia(&self, mass: f32) -> f32 { + mass * self.radius * self.radius * 0.5 + } } impl Circle { diff --git a/geom/src/obb.rs b/geom/src/obb.rs index b0fdee3c..10bd87a2 100644 --- a/geom/src/obb.rs +++ b/geom/src/obb.rs @@ -55,6 +55,15 @@ impl OBB { ] } + #[inline] + /// Calculates the moment of inertia of the OBB around its center. + pub fn moment_of_inertia(&self, mass: f32) -> f32 { + let [a, b] = self.axis(); + let a = a.mag2(); + let b = b.mag2(); + mass * (a + b) / 12.0 + } + #[inline] pub fn center(&self) -> Vec2 { (self.corners[2] + self.corners[0]) * 0.5