Skip to main content

cuboid_cuboid_find_local_separating_normal_oneway

Function cuboid_cuboid_find_local_separating_normal_oneway 

Source
pub fn cuboid_cuboid_find_local_separating_normal_oneway(
    cuboid1: &Cuboid,
    cuboid2: &Cuboid,
    pos12: &Pose,
) -> (f32, Vector)
Expand description

Finds the best separating axis by testing the face normals of the first cuboid.

This function tests the face normals (X, Y, Z axes in 2D/3D) of cuboid1 to find which direction gives the maximum separation between the two cuboids. This is the “one-way” test that only considers faces from one cuboid.

§Why “One-Way”?

For a complete SAT test between two cuboids, you need to test:

  1. Face normals from cuboid1 (this function)
  2. Face normals from cuboid2 (call this function again with swapped arguments)
  3. Edge-edge cross products in 3D (cuboid_cuboid_find_local_separating_edge_twoway)

By testing only one shape’s normals at a time, the implementation can be more efficient and reusable.

§Parameters

  • cuboid1: The cuboid whose face normals will be tested
  • cuboid2: The other cuboid
  • pos12: The position of cuboid2 relative to cuboid1

§Returns

A tuple containing:

  • Real: The maximum separation found among cuboid1’s face normals
    • Positive: Shapes are separated by at least this distance
    • Negative: Shapes are overlapping (penetration)
  • Vector: The face normal direction that gives this separation

§Example

use parry2d::shape::Cuboid;
use parry2d::query::sat::cuboid_cuboid_find_local_separating_normal_oneway;
use parry2d::math::{Pose, Vector};

let rect1 = Cuboid::new(Vector::new(1.0, 1.0));
let rect2 = Cuboid::new(Vector::new(0.5, 0.5));

// Position rect2 to the right of rect1
let pos12 = Pose::translation(2.5, 0.0);

// Test rect1's face normals (X and Y axes)
let (sep1, normal1) = cuboid_cuboid_find_local_separating_normal_oneway(
    &rect1, &rect2, &pos12
);

// Test rect2's face normals by swapping arguments and inverting the transform
let (sep2, normal2) = cuboid_cuboid_find_local_separating_normal_oneway(
    &rect2, &rect1, &pos12.inverse()
);

// The maximum separation indicates if shapes collide
let max_separation = sep1.max(sep2);
if max_separation > 0.0 {
    println!("Shapes are separated!");
}