Function project_origin

Source
pub fn project_origin<G: ?Sized + SupportMap>(
    m: &Isometry<f32>,
    g: &G,
    simplex: &mut VoronoiSimplex,
) -> Option<Point<f32>>
Expand description

Projects the origin onto the boundary of the given shape.

This function finds the point on the shape’s surface that is closest to the origin (0, 0) in 2D or (0, 0, 0) in 3D. This is useful for distance queries and collision detection when you need to know the closest point on a shape.

§Important: Origin Must Be Outside

The origin is assumed to be outside of the shape. If the origin is inside the shape, this function returns None. For penetrating cases, use the EPA (Expanding Polytope Algorithm) instead.

§Parameters

  • m: The position and orientation (isometry) of the shape in world space
  • g: The shape to project onto (must implement SupportMap)
  • simplex: A reusable simplex structure for the GJK algorithm. Initialize with VoronoiSimplex::new() before first use.

§Returns

  • Some(Point): The closest point on the shape’s boundary, in the shape’s local space
  • None: If the origin is inside the shape

§Example

use parry3d::shape::Ball;
use parry3d::query::gjk::{project_origin, VoronoiSimplex};
use parry3d::math::Isometry;

// Create a ball at position (5, 0, 0)
let ball = Ball::new(1.0);
let position = Isometry::translation(5.0, 0.0, 0.0);

// Project the origin onto the ball
let mut simplex = VoronoiSimplex::new();
if let Some(closest_point) = project_origin(&position, &ball, &mut simplex) {
    println!("Closest point on ball: {:?}", closest_point);
    // The point will be approximately (-1, 0, 0) in local space
    // which is the left side of the ball facing the origin
}

§Performance Note

The simplex parameter can be reused across multiple calls to avoid allocations. This is particularly beneficial when performing many projection queries.