Skip to content

Commit

Permalink
add moments of inertia
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Jun 5, 2024
1 parent 0352d5a commit 0f4b8fc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions geom/src/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions geom/src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions geom/src/obb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0f4b8fc

Please sign in to comment.