Skip to content

Add Ray2d and Ray3d primitives #10843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}