Skip to content

Fix bug with nonuniform scaling in mesh vs convex collisions#2745

Merged
eric-heiden merged 2 commits into
newton-physics:mainfrom
nvtw:dev/tw3/fix_nonuniform_scaling_bug
May 7, 2026
Merged

Fix bug with nonuniform scaling in mesh vs convex collisions#2745
eric-heiden merged 2 commits into
newton-physics:mainfrom
nvtw:dev/tw3/fix_nonuniform_scaling_bug

Conversation

@nvtw

@nvtw nvtw commented May 7, 2026

Copy link
Copy Markdown
Member

Description

Fix triangle-mesh-vs-convex collisions silently dropping all contacts under non-uniform (and even large uniform) mesh scale. The mesh BVH is built over unscaled mesh.points, but the AABB query in mesh_vs_convex_midphase was performed in scaled mesh-local space. Any convex shape whose unscaled-space AABB lay outside the BVH bounds received 0 triangles and 0 contacts. The query AABB and per-axis contact gap are now converted from scaled to unscaled (BVH) space before the BVH overlap test.

Also adds two small helper funcs in collision_core.py:

  • aabb_to_unscaled — convert an AABB from a component-wise scaled frame to the unscaled frame, swapping per-axis min/max under negative scale.
  • transform_normal_with_scale — correct normal transform under non-uniform component-wise scale (R · (n / scale), normalized).

Checklist

  • New or existing tests cover these changes
  • The documentation is up to date with these changes
  • CHANGELOG.md has been updated (if user-facing change)

Test plan

New regression class TestMeshNonUniformScaling in newton/tests/test_narrow_phase.py drops a sphere onto a unit-quad mesh with various scales (uniform, large uniform, pancake (10,10,1), thin (1,1,0.1), extreme (50,0.5,1)) and asserts contacts are produced with sane unit-length, roughly +Z normals; a far-separated case asserts 0 contacts (no false positives).

uv run --extra dev -m newton.tests -k test_narrow_phase.TestMeshNonUniformScaling

Bug fix

Triangle-mesh-vs-convex collisions silently produce zero contacts whenever the mesh shape uses a non-uniform scale (e.g. (10, 10, 1)), and even for sufficiently large uniform scales. Mesh-averaging of the scale caused the AABB query against the BVH (which lives in unscaled mesh-local space) to miss all triangles.

Steps to reproduce:

  1. Build a triangle mesh and add it as a shape with a non-uniform scale=(sx, sy, sz) (e.g. (10, 10, 1)).
  2. Place a convex shape (sphere/box/capsule/...) so it overlaps the scaled mesh in world space.
  3. Run the narrow phase — observe contact_count == 0.

Minimal reproduction:

import numpy as np
import newton

verts = np.array(
    [[-0.5, -0.5, 0.0], [0.5, -0.5, 0.0], [0.5, 0.5, 0.0], [-0.5, 0.5, 0.0]],
    dtype=np.float32,
)
inds = np.array([0, 1, 2, 0, 2, 3], dtype=np.int32)
mesh = newton.Mesh(verts, inds)

# Non-uniform scale: 10x10 quad in world space, sphere dropped just above it.
# Without this fix: 0 contacts. With this fix: contacts as expected.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed triangle-mesh-vs-convex collisions to correctly handle non-uniform and large uniform mesh scaling, preventing silent loss of contact detection.
  • Tests

    • Added comprehensive test coverage for mesh-vs-convex collision behavior under various scaling scenarios, including non-uniform and extreme anisotropic scaling configurations.

@nvtw nvtw self-assigned this May 7, 2026
@coderabbitai

coderabbitai Bot commented May 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: e6dd11bb-6b0e-4050-a885-629c8c87a4e6

📥 Commits

Reviewing files that changed from the base of the PR and between cd98996 and 5b9f16f.

📒 Files selected for processing (1)
  • CHANGELOG.md
✅ Files skipped from review due to trivial changes (1)
  • CHANGELOG.md

📝 Walkthrough

Walkthrough

This PR fixes a bug where triangle-mesh-vs-convex collisions could drop contacts under non-uniform or large uniform mesh scaling by converting scaled AABBs into the BVH's unscaled mesh-local space and applying per-axis contact gap adjustments; helpers and tests are added.

Changes

Mesh Scaling Collision Detection

Layer / File(s) Summary
Scaling Helper Functions
newton/_src/geometry/collision_core.py
New @wp.func helpers aabb_to_unscaled and transform_normal_with_scale to convert scaled AABBs and normals into BVH/unscaled/world space with epsilon and negative-scale handling.
AABB Query and Margin Adjustment
newton/_src/geometry/collision_core.py
mesh_vs_convex_midphase computes a tight AABB in the mesh's scaled-local frame, converts it to BVH unscaled space via aabb_to_unscaled, and applies per-axis anisotropic contact margins (rigid_gap / abs(scale)) before BVH triangle queries.
Regression Tests and Documentation
newton/tests/test_narrow_phase.py, CHANGELOG.md
Adds TestMeshNonUniformScaling with multiple cases (uniform, large uniform, non-uniform XY, off-center, thin Z, extreme X, separated) validating contact presence/absence and normal validity; updates CHANGELOG fixed entry.

Sequence Diagram(s)

sequenceDiagram
  participant Convex
  participant Mesh
  participant aabb_to_unscaled
  participant BVH
  participant NarrowPhase
  Convex->>Mesh: compute tight AABB in mesh scaled-local frame
  Mesh->>aabb_to_unscaled: convert scaled AABB -> BVH unscaled space
  aabb_to_unscaled->>BVH: submit AABB query (with per-axis margins)
  BVH-->>NarrowPhase: triangle list
  NarrowPhase-->>Convex: contact buffer / normals
Loading

Possibly related PRs

  • newton-physics/newton#2619: Refactors the tiled BVH query loop in mesh_vs_convex_midphase, overlapping with this PR's changes to AABB/margin handling.
  • newton-physics/newton#2527: Restructures tiled BVH query control flow in mesh_vs_convex_midphase, potentially related to this PR's midphase updates.

Suggested reviewers

  • eric-heiden
  • adenzler-nvidia

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix bug with nonuniform scaling in mesh vs convex collisions' directly and specifically identifies the primary change—fixing a collision bug related to non-uniform scaling, which matches the changeset perfectly.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented May 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@eric-heiden eric-heiden left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

@eric-heiden eric-heiden added this pull request to the merge queue May 7, 2026
Merged via the queue into newton-physics:main with commit c4d74b1 May 7, 2026
25 checks passed
eric-heiden added a commit that referenced this pull request May 7, 2026
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