Skip to content

Commit

Permalink
merge Axis projection trait into the SatShape trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Aug 11, 2023
1 parent f810ac7 commit 71ac667
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
8 changes: 2 additions & 6 deletions src/v3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ use math::Vec2;
use range::Range;
use shapes::Point;

trait SatShape: AxisProjection {
trait SatShape {
type AxisIter: Iterator<Item = Vec2>;
/// Axes should all be unit vectors
fn axes(&self) -> Self::AxisIter;
}

trait AxisProjection {
fn axes(&self) -> Self::AxisIter;
fn project_on(&self, axis: Vec2) -> Range;
}

#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
struct Contact {
point: Point,
}
Expand Down
31 changes: 19 additions & 12 deletions src/v3/shapes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod point {
use core::ops::Add;

use crate::v3::{math::Vec2, AxisProjection, Range};
use crate::v3::{math::Vec2, Range, SatShape};

#[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) struct Point(Vec2);
Expand All @@ -18,7 +18,11 @@ mod point {
}
}

impl AxisProjection for Point {
impl SatShape for Point {
type AxisIter = core::iter::Empty<Vec2>;
fn axes(&self) -> Self::AxisIter {
core::iter::empty()
}
fn project_on(&self, axis: Vec2) -> crate::v3::Range {
let p = self.0.dot(axis);
Range::from_min_max(p, p)
Expand Down Expand Up @@ -69,11 +73,16 @@ mod point {
let expected = Range::from_min_max(expected, expected);
assert_abs_diff_eq!(point.into().project_on(axis), expected);
}

#[test]
fn test_sat_axes() {
assert_eq!(Point::from(Vec2::ZERO).axes().next(), None);
}
}
}

mod aabb {
use crate::v3::{math::Vec2, AxisProjection, Range, SatShape};
use crate::v3::{math::Vec2, Range, SatShape};

pub(crate) struct Aabb {
center: Vec2,
Expand All @@ -94,7 +103,13 @@ mod aabb {
}
}

impl AxisProjection for Aabb {
impl SatShape for Aabb {
type AxisIter = core::array::IntoIter<Vec2, 2>;

fn axes(&self) -> Self::AxisIter {
[Vec2::X, Vec2::Y].into_iter()
}

fn project_on(&self, axis: Vec2) -> Range {
let r1 = self.half_size.dot(axis).abs();
let r2 = Vec2::new(-self.half_size.x, self.half_size.y)
Expand All @@ -106,14 +121,6 @@ mod aabb {
}
}

impl SatShape for Aabb {
type AxisIter = core::array::IntoIter<Vec2, 2>;

fn axes(&self) -> Self::AxisIter {
[Vec2::X, Vec2::Y].into_iter()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 71ac667

Please sign in to comment.