Skip to content

Conversation

Copy link

Copilot AI commented Nov 1, 2025

Summary

Successfully redesigned checkTriangleTriangleOverlap from SAT-based to projection-based approach using Sutherland-Hodgman clipping algorithm.

Completed Tasks

  • Analyze the current SAT-based implementation in checkTriangleTriangleOverlap
  • Design the new projection-based approach using Sutherland-Hodgman algorithm
  • Implement helper function projectTriangleOntoTriangle for projecting triangle onto plane and calculating clipping area
  • Replace SAT logic with bidirectional projection approach (A->B and B->A)
  • Average the results from both directions for final contact info
  • Remove all SAT-related code (face normals tests, edge-edge cross products)
  • Address all code review feedback:
    • Division by zero checks in intersection calculations
    • Normal normalization safety for degenerate cases
    • Named constant MAX_CLIPPING_VERTICES instead of magic numbers
    • Bounds checking to prevent buffer overflow
    • Accurate comments
  • Implement axis hygiene check with rotated plane stability trials for side strip artifacts
  • Fix axis hygiene: rotate reference triangle vertices and properly break from nested loops

Implementation Details

Code Metrics

  • Lines removed: 686 (SAT implementation)
  • Lines added: ~360 (projection-based implementation with axis hygiene check)
  • Net reduction: ~326 lines (21% smaller)
  • Before: 1,556 lines → After: ~1,230 lines

Algorithm

  1. Project triangle B onto triangle A's plane and clip against A's edges
  2. Project triangle A onto triangle B's plane and clip against B's edges
  3. Axis hygiene check: If projected area < 5% of reference triangle area (suspicious):
    • Compute reference triangle centroid
    • Rotate both the reference plane normal AND the reference triangle vertices around the centroid
    • Perform 4 trial projections (±5.7° around 2 orthogonal in-plane axes)
    • Use the minimum area from all trials (0 if any trial has no contact)
    • Early exit from both loops if zero area is found
  4. Average results when both directions have contact

Axis Hygiene Feature (Corrected)

The axis hygiene check now properly rotates the entire reference frame:

  • Suspicious threshold: Overlaps < 5% of reference triangle area
  • Stability test: 4 trial rotations (±5.7° around 2 orthogonal in-plane axes)
  • Rotation: Both normal and reference triangle vertices rotate around triangle centroid using Rodrigues formula
  • Result: Minimum area across all trials, or 0 if any trial shows no contact
  • Early exit: Flag foundZeroArea ensures both outer and inner loops exit when zero area is found
  • Physical meaning: Only stable overlaps that persist under small rigid body rotations are considered valid

Robustness

  • Division by zero checks in all intersection calculations
  • Buffer overflow protection with bounds checking
  • Safe normal normalization for nearly-opposite normals
  • Named constants for maintainability
  • Properly implemented axis hygiene check with correct rotation and loop control

Benefits

Simplicity: Conceptually simpler than SAT
Consistency: Asymptotically equivalent to contact surface projection for shallow contacts
Maintainability: ~21% code reduction improves readability
Robustness: Comprehensive edge case handling including proper axis hygiene implementation
Compatibility: Function signature unchanged, existing code unaffected
Physical Accuracy: Axis hygiene check properly validates contacts under rigid body rotations

Next Steps (Requires CUDA Environment)

  • Build and verify compilation
  • Test with existing demos
  • Compare results with previous implementation
  • Performance benchmarking (optional)

The implementation is ready for review and testing in a CUDA-enabled environment.

Original prompt

I am looking to redesign the checkTriangleTriangleOverlap function. I am thinking, since we are calculating the force based on each triangle pair, and the way is to project a triangle onto the other triangle's plane, then calculate the penetration and overlap area, then we actually do not need SAT. We just need to, when triangle A has nodes submerged under B's plane (at its negative direction), we figure out A's nodal projections and potential edge clipping points on B's plane, then use Sutherland-Hodgman algorithm to calculate the clipping area and the penetration. We do that for B->A then A->B, and use the average as the final area and depth, then we have pairwise contact info. This is somewhat similar to the commented-out case 2. For shallow contacts, this should be asymptotically the same as projecting onto the contact surface formed by the contact loop. Doing this and we don't need the SAT-based branches. If you agree, please make this change.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Nov 1, 2025
Copilot AI and others added 4 commits November 1, 2025 17:05
…h instead of SAT

Co-authored-by: Ruochun <24469442+Ruochun@users.noreply.github.com>
…safety, and use named constant

Co-authored-by: Ruochun <24469442+Ruochun@users.noreply.github.com>
Co-authored-by: Ruochun <24469442+Ruochun@users.noreply.github.com>
…ulation

Co-authored-by: Ruochun <24469442+Ruochun@users.noreply.github.com>
Copilot AI changed the title [WIP] Redesign checkTriangleTriangleOverlap function for better efficiency Replace SAT with bidirectional projection in triangle-triangle collision detection Nov 1, 2025
Copilot AI requested a review from Ruochun November 1, 2025 17:17
Copilot finished work on behalf of Ruochun November 1, 2025 17:17
@Ruochun
Copy link
Collaborator

Ruochun commented Nov 3, 2025

@copilot I am dealing with a classic artifact: when two big, flat faces barely interpenetrate, tiny “side strips” (from edges/adjacent faces) can produce large penalty responses if we treat every local penetration direction equally. Can you apply this to the triangle-triangle penetration calculation: limit the penetration/overlap to be no larger than the minimum length required to move the projection of the projected triangle away from the reference triangle. This is similar to a "2D MTV" idea, and since we produce force only when this projection intersects with the reference triangle on its plane, this minimum length is always a positive value.

…rip artifacts

Co-authored-by: Ruochun <24469442+Ruochun@users.noreply.github.com>
Copy link
Author

Copilot AI commented Nov 3, 2025

@copilot I am dealing with a classic artifact: when two big, flat faces barely interpenetrate, tiny “side strips” (from edges/adjacent faces) can produce large penalty responses if we treat every ...

Implemented 2D MTV calculation to limit penetration depth. Instead of using the maximum 3D distance of submerged vertices, the depth is now computed as the minimum 2D distance required to move the clipped polygon away from the reference triangle on its plane. This is calculated by testing each edge of the reference triangle and finding the minimum penetration distance needed for separation. Commit: 7277124

Copilot AI changed the title Replace SAT with bidirectional projection in triangle-triangle collision detection Replace SAT with bidirectional projection and 2D MTV in triangle-triangle collision detection Nov 3, 2025
Copilot finished work on behalf of Ruochun November 3, 2025 02:30
@Ruochun
Copy link
Collaborator

Ruochun commented Nov 3, 2025

@copilot I've undone your last commit, sorry. That's because I thought about a better way of restricting the last penetration for side strips. I think any projected overlap needs to satisfy an "axis hygiene" check, if the projected area is lower than a small percentage of the area of the reference triangle. Such an overlap is called suspicious and it has to go through a check: If we rotate the plane of the reference triangle by a bit (potentially 4 trials: the two orthogonal directions about the original normal direction of the plane, increase or decrease angles), this area needs to still exist to be considered a stable area, and the smallest overlap among these 4 trial projections (minimal is 0) will be used as the final overlap area for these pairs.

…side strip artifacts

Co-authored-by: Ruochun <24469442+Ruochun@users.noreply.github.com>
Copy link
Author

Copilot AI commented Nov 3, 2025

@copilot I've undone your last commit, sorry. That's because I thought about a better way of restricting the last penetration for side strips. I think any projected overlap needs to satisfy an "axis h...

Implemented axis hygiene check with stability trials. When projected overlap area is < 5% of the reference triangle area (suspicious), the algorithm now performs 4 trial projections with the reference plane rotated slightly (±5.7° around two orthogonal in-plane axes). The final overlap area is the minimum from all trials, or 0 if any trial shows no contact. This ensures only stable overlaps that persist under small plane rotations are considered valid, effectively filtering side strip artifacts. Commit: 9bd78bf

Copilot AI changed the title Replace SAT with bidirectional projection and 2D MTV in triangle-triangle collision detection Replace SAT with bidirectional projection and axis hygiene check in triangle-triangle collision detection Nov 3, 2025
Copilot finished work on behalf of Ruochun November 3, 2025 04:01
@Ruochun
Copy link
Collaborator

Ruochun commented Nov 3, 2025

@copilot This is good. But before the check is carried out, in addition to rotating the plane, the reference triangle's nodes should change along with this rotation, right? This should be added to the code. Also, when you break in the inner loop of the 4 checks, I think you forgot to also break out of the outer loop.

…from outer loop

Co-authored-by: Ruochun <24469442+Ruochun@users.noreply.github.com>
Copy link
Author

Copilot AI commented Nov 3, 2025

@copilot This is good. But before the check is carried out, in addition to rotating the plane, the reference triangle's nodes should change along with this rotation, right? This should be added to the...

Fixed both issues. The reference triangle vertices now rotate along with the plane normal using Rodrigues formula around the triangle centroid. Also added a foundZeroArea flag to properly break from both the outer and inner loops when zero area is detected. Commit: ddc45c2

Copilot finished work on behalf of Ruochun November 3, 2025 14:05
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