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

Conversation

Jondolf
Copy link
Contributor

@Jondolf Jondolf commented Mar 22, 2024

Objective

Many shapes like cylinders, cones, capsules, convex polyhedra, and convex polygons use support mapping and a variant of GJK for ray casting.

If the ray origin is inside of the shape and the shape is marked as non-solid (i.e. hollow), the ray casting method shifts the ray origin just outside of the shape and reverses the ray's direction before running GJK a second time. This acts as a way to find the hit point on the boundary as if the ray was to keep travelling inside the shape.

However, the actual final ray cast is still done from the outside of the shape, and the normal ends up pointing outward. This is not the expected result, as the specialized ray casting implementations for shapes like circles, rectangles, triangles, and so on return the normal pointing in the interior of the shape. This makes the ray casting behavior highly inconsistent between shapes, which could lead to critical and hard-to-debug bugs.

The normal should always be pointing in the interior of the shape for non-solid shapes when the ray origin is inside of the shape, even for GJK-based ray casts.

Solution

Simply flip the normal for the non-solid interior hits. I also made the expected behavior clearer in the RayIntersection docs.

Before: (see how the capsule behavior is different)

2024-03-22.19-14-20.mp4

After:

2024-03-22.19-13-04.mp4

There is also some visible instability with the GJK-based ray casting due to GJK not converging on a solution, but attempting to fix that is out of scope for this PR.

@sebcrozet sebcrozet force-pushed the fix-gjk-ray-casting-normal-direction branch from abebf88 to 0832c78 Compare March 24, 2024 10:18
@sebcrozet
Copy link
Member

Good catch, thanks! I rebased the PR on main to fix the CI.

@sebcrozet sebcrozet merged commit 49d2d07 into dimforge:master Mar 24, 2024
5 checks passed
@Jondolf Jondolf deleted the fix-gjk-ray-casting-normal-direction branch March 24, 2024 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants