Struct Ray

Source
#[repr(C)]
pub struct Ray { pub origin: Point<f32>, pub dir: Vector<f32>, }
Expand description

A ray for ray-casting queries.

A ray is a half-infinite line starting at an origin point and extending infinitely in a direction. Rays are fundamental for visibility queries, shooting mechanics, and collision prediction.

§Structure

  • origin: The starting point of the ray
  • dir: The direction vector (does NOT need to be normalized)

§Direction Vector

The direction can be any non-zero vector:

  • Normalized: dir with length 1.0 gives time-of-impact in world units
  • Not normalized: Time-of-impact is scaled by dir.norm()

Most applications use normalized directions for intuitive results.

§Use Cases

  • Shooting/bullets: Check what a projectile hits
  • Line of sight: Check if one object can “see” another
  • Mouse picking: Select objects by clicking
  • Laser beams: Simulate light or laser paths
  • Proximity sensing: Detect obstacles in a direction

§Example

use parry3d::query::{Ray, RayCast};
use parry3d::shape::Ball;
use nalgebra::{Point3, Vector3, Isometry3};

// Create a ray from origin pointing along +X axis
let ray = Ray::new(
    Point3::origin(),
    Vector3::new(1.0, 0.0, 0.0)  // Normalized direction
);

// Create a ball at position (5, 0, 0) with radius 1
let ball = Ball::new(1.0);
let ball_pos = Isometry3::translation(5.0, 0.0, 0.0);

// Cast the ray against the ball
if let Some(toi) = ball.cast_ray(&ball_pos, &ray, 100.0, true) {
    // Ray hits at t=4.0 (center at 5.0 minus radius 1.0)
    assert_eq!(toi, 4.0);

    // Compute the actual hit point
    let hit_point = ray.point_at(toi);
    assert_eq!(hit_point, Point3::new(4.0, 0.0, 0.0));
}

Fields§

§origin: Point<f32>

Starting point of the ray.

This is where the ray begins. Points along the ray are computed as origin + dir * t for t ≥ 0.

§dir: Vector<f32>

Direction vector of the ray.

This vector points in the direction the ray travels. It does NOT need to be normalized, but using a normalized direction makes time-of-impact values represent actual distances.

Implementations§

Source§

impl Ray

Source

pub fn new(origin: Point<f32>, dir: Vector<f32>) -> Ray

Creates a new ray from an origin point and direction vector.

§Arguments
  • origin - The starting point of the ray
  • dir - The direction vector (typically normalized but not required)
§Example
use parry3d::query::Ray;
use nalgebra::{Point3, Vector3};

// Horizontal ray pointing along +X axis
let ray = Ray::new(
    Point3::new(0.0, 5.0, 0.0),
    Vector3::new(1.0, 0.0, 0.0)
);

// Ray starts at (0, 5, 0) and points along +X
assert_eq!(ray.origin, Point3::new(0.0, 5.0, 0.0));
assert_eq!(ray.dir, Vector3::new(1.0, 0.0, 0.0));
Source

pub fn transform_by(&self, m: &Isometry<f32>) -> Self

Transforms this ray by the given isometry (translation + rotation).

Both the origin and direction are transformed.

§Example
use parry3d::query::Ray;
use nalgebra::{Isometry3, Point3, Vector3};

let ray = Ray::new(Point3::origin(), Vector3::x());

// Translate by (5, 0, 0)
let transform = Isometry3::translation(5.0, 0.0, 0.0);
let transformed = ray.transform_by(&transform);

assert_eq!(transformed.origin, Point3::new(5.0, 0.0, 0.0));
assert_eq!(transformed.dir, Vector3::x());
Source

pub fn inverse_transform_by(&self, m: &Isometry<f32>) -> Self

Transforms this ray by the inverse of the given isometry.

This is equivalent to transforming the ray to the local space of an object.

§Example
use parry3d::query::Ray;
use nalgebra::{Isometry3, Point3, Vector3};

let ray = Ray::new(Point3::new(10.0, 0.0, 0.0), Vector3::x());

let transform = Isometry3::translation(5.0, 0.0, 0.0);
let local_ray = ray.inverse_transform_by(&transform);

// Origin moved back by the translation
assert_eq!(local_ray.origin, Point3::new(5.0, 0.0, 0.0));
Source

pub fn translate_by(&self, v: Vector<f32>) -> Self

Translates this ray by the given vector.

Only the origin is moved; the direction remains unchanged.

§Example
use parry3d::query::Ray;
use nalgebra::{Point3, Vector3};

let ray = Ray::new(Point3::origin(), Vector3::x());
let translated = ray.translate_by(Vector3::new(10.0, 5.0, 0.0));

assert_eq!(translated.origin, Point3::new(10.0, 5.0, 0.0));
assert_eq!(translated.dir, Vector3::x()); // Direction unchanged
Source

pub fn point_at(&self, t: f32) -> Point<f32>

Computes a point along the ray at parameter t.

Returns origin + dir * t. For t ≥ 0, this gives points along the ray.

§Arguments
  • t - The parameter (typically the time-of-impact from ray casting)
§Example
use parry3d::query::Ray;
use nalgebra::{Point3, Vector3};

let ray = Ray::new(
    Point3::origin(),
    Vector3::new(1.0, 0.0, 0.0)
);

// Point at t=5.0
assert_eq!(ray.point_at(5.0), Point3::new(5.0, 0.0, 0.0));

// Point at t=0.0 is the origin
assert_eq!(ray.point_at(0.0), ray.origin);

Trait Implementations§

Source§

impl Clone for Ray

Source§

fn clone(&self) -> Ray

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ray

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for Ray

Auto Trait Implementations§

§

impl Freeze for Ray

§

impl RefUnwindSafe for Ray

§

impl Send for Ray

§

impl Sync for Ray

§

impl Unpin for Ray

§

impl UnwindSafe for Ray

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.