Skip to content

Optimize and fix sphere-OBB intersection logic#23865

Open
CrazyRoka wants to merge 1 commit intobevyengine:mainfrom
CrazyRoka:optimize-camera-primitives
Open

Optimize and fix sphere-OBB intersection logic#23865
CrazyRoka wants to merge 1 commit intobevyengine:mainfrom
CrazyRoka:optimize-camera-primitives

Conversation

@CrazyRoka
Copy link
Copy Markdown
Contributor

Objective

  • Optimize the Sphere and OOB intersection logic.
  • Fix a bug when the sphere and OBB shared the exact same center.

Solution

  • The previous implementation calculated v / d (a normalized vector). If the distance d was zero (in a situation when centers are identical), this resulted in a division by zero. The new approach avoids normalization by refactoring the comparison.
  • Original: $d < r_1 + r_2$ where $r_2 =$ relative_radius$\Bigl(\frac{\mathbf{v}}{d}\Bigr)$
  • New: $d^2 \leq r \cdot d + r_{\rm unscaled}$ where $r_{\rm unscaled} =$ relative_radius$(\mathbf{v})$

Testing

  • Added new test cases covering identical centers, edge contacts, and zero-extent volumes. These tests fail on main but pass with this PR.
  • Verified performance via the newly added camera benchmarks in Add camera primitives benchmark #23863
intersects_obb/sphere_intersects_obb
                        time:   [5.3677 ns 5.3932 ns 5.4227 ns]
                        change: [−19.462% −18.945% −18.426%] (p = 0.00 < 0.05)
                        Performance has improved.

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior C-Performance A change motivated by improving speed, memory usage or compile times A-Math Fundamental domain-agnostic mathematical operations D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward A-Camera User-facing camera APIs and controllers. X-Uncontroversial This work is generally agreed upon labels Apr 17, 2026
@alice-i-cecile alice-i-cecile requested a review from atlv24 April 17, 2026 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Camera User-facing camera APIs and controllers. A-Math Fundamental domain-agnostic mathematical operations C-Bug An unexpected or incorrect behavior C-Performance A change motivated by improving speed, memory usage or compile times D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants