Skip to content
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

Fix normal direction for GJK ray casting inside non-solid shapes #183

Merged
Merged
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
6 changes: 5 additions & 1 deletion src/query/ray/ray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ impl Ray {
archive(as = "Self")
)]
pub struct RayIntersection {
/// The time of impact of the ray with the object. The exact contact point can be computed
/// The time of impact of the ray with the object. The exact contact point can be computed
/// with: `ray.point_at(toi)` or equivalently `origin + dir * toi` where `origin` is the origin of the ray;
/// `dir` is its direction and `toi` is the value of this field.
pub toi: Real,

/// The normal at the intersection point.
///
/// If the origin of the ray is inside of the shape and the shape is not solid,
/// the normal will point towards the interior of the shape.
/// Otherwise, the normal points outward.
///
/// If the `toi` is exactly zero, the normal might not be reliable.
// XXX: use a Unit<Vector> instead.
pub normal: Vector<Real>,
Expand Down
8 changes: 6 additions & 2 deletions src/query/ray/ray_support_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ where
simplex.reset(CSOPoint::single_point(supp - new_ray.origin.coords));

gjk::cast_local_ray(shape, simplex, &new_ray, shift + eps).and_then(
|(toi, normal)| {
|(toi, outward_normal)| {
let toi = shift - toi;
if toi <= max_toi {
Some(RayIntersection::new(toi, normal, FeatureId::Unknown))
Some(RayIntersection::new(
toi,
-outward_normal,
FeatureId::Unknown,
))
} else {
None
}
Expand Down
Loading