Skip to content

Commit

Permalink
Add Ray2d and Ray3d primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
Jondolf committed Dec 2, 2023
1 parent 24c6a7d commit 4e7da2c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
35 changes: 34 additions & 1 deletion crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{Primitive2d, WindingOrder};
use crate::Vec2;

/// A normalized vector pointing in a direction in 2D space
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Direction2d(Vec2);

impl From<Vec2> for Direction2d {
Expand Down Expand Up @@ -63,6 +63,32 @@ pub struct Plane2d {
}
impl Primitive2d for Plane2d {}

/// An infinite half-line starting at `origin` and going in `direction` in 2D space.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Ray2d {
/// The origin of the ray.
pub origin: Vec2,
/// The direction of the ray.
pub direction: Direction2d,
}

impl Ray2d {
/// Create a new `Ray2d` from a given origin and direction
#[inline]
pub fn new(origin: Vec2, direction: Vec2) -> Self {
Self {
origin,
direction: direction.into(),
}
}

/// Get a point at a given distance along the ray
#[inline]
pub fn get_point(&self, distance: f32) -> Vec2 {
self.origin + *self.direction * distance
}
}

/// An infinite line along a direction in 2D space.
///
/// For a finite line: [`Segment2d`]
Expand Down Expand Up @@ -323,6 +349,13 @@ impl RegularPolygon {
mod tests {
use super::*;

#[test]
fn ray_math() {
let ray = Ray2d::new(Vec2::ZERO, Vec2::Y * 10.0);
assert_eq!(*ray.direction, Vec2::Y, "ray direction is not normalized");
assert_eq!(ray.get_point(2.0), Vec2::Y * 2.0);
}

#[test]
fn triangle_winding_order() {
let mut cw_triangle = Triangle2d::new(
Expand Down
40 changes: 39 additions & 1 deletion crates/bevy_math/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::Primitive3d;
use crate::Vec3;

/// A normalized vector pointing in a direction in 3D space
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Direction3d(Vec3);

impl From<Vec3> for Direction3d {
Expand Down Expand Up @@ -43,6 +43,32 @@ pub struct Plane3d {
}
impl Primitive3d for Plane3d {}

/// An infinite half-line starting at `origin` and going in `direction` in 3D space.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Ray3d {
/// The origin of the ray.
pub origin: Vec3,
/// The direction of the ray.
pub direction: Direction3d,
}

impl Ray3d {
/// Create a new `Ray3d` from a given origin and direction
#[inline]
pub fn new(origin: Vec3, direction: Vec3) -> Self {
Self {
origin,
direction: direction.into(),
}
}

/// Get a point at a given distance along the ray
#[inline]
pub fn get_point(&self, distance: f32) -> Vec3 {
self.origin + *self.direction * distance
}
}

/// An infinite line along a direction in 3D space.
///
/// For a finite line: [`Segment3d`]
Expand Down Expand Up @@ -331,3 +357,15 @@ impl Torus {
}
}
}

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

#[test]
fn ray_math() {
let ray = Ray3d::new(Vec3::ZERO, Vec3::Z * 10.0);
assert_eq!(*ray.direction, Vec3::Z, "ray direction is not normalized");
assert_eq!(ray.get_point(2.0), Vec3::Z * 2.0);
}
}

0 comments on commit 4e7da2c

Please sign in to comment.