Skip to content

Fix MJScene body position pinned at reference pose on mass change#1477

Merged
juan-g-bonilla merged 3 commits into
developfrom
fix/mjscene-mass-change-and-perf
Jul 13, 2026
Merged

Fix MJScene body position pinned at reference pose on mass change#1477
juan-g-bonilla merged 3 commits into
developfrom
fix/mjscene-mass-change-and-perf

Conversation

@juan-g-bonilla

@juan-g-bonilla juan-g-bonilla commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Description

MJScene::equationsOfMotion calls mj_setConst whenever a body's mass changes to refresh
MuJoCo's cached "constant" quantities. mj_setConst overwrites data->qpos with the model
reference pose and leaves it there, so every position-dependent quantity computed afterwards in
the same call — forward kinematics, position-dependent forces, and the site state-output
messages — is evaluated at the reference pose instead of the current configuration. Since the
branch fires on every step of a burn, the reported body position stays pinned at the initial
pose for the whole maneuver, even though velocity and mass integrate correctly.

The fix restores the integrator state via updateMujocoArraysFromStates() immediately after
mj_setConst (which also re-flags the kinematics stale so they are recomputed), and clears the
model-const-stale flag now that the constants match the current mass properties.

Commits are organized so the fix is isolated from a behaviour-preserving performance
improvements in the same code path:

  1. [#1476] the mj_setConst state-restore fix plus a regression test (MJScene.cpp,
    test_mass_update.py).
  2. Cache the mjModel/mjData pointers for the duration of equationsOfMotion instead of
    re-fetching them through the recompile-checking accessor on every access.
  3. Release-notes snippet.

Verification

  • Extended test_mass_update.py::test_continuouslyChangingMass to also assert the reported
    position against the closed-form trajectory. The existing test only checked velocity, which
    integrates correctly even with the bug present, so it could never have caught this. The new
    assertion requires scene.extraEoMCall = True — with the default extraEoMCall = False,
    postIntegration rewrites the site message after the step and masks the mid-step mj_setConst
    clobber, which is why no existing test surfaced it.
  • Confirmed the test fails on the unfixed code (reported x pinned at 0.0 vs analytic 9.66) and
    passes with the fix applied.
  • All 15 mujocoDynamics general-module unit tests pass with this branch applied.

Documentation

  • Added a release-notes snippet (mjscene-mass-change-and-perf.rst).

@juan-g-bonilla juan-g-bonilla self-assigned this Jul 11, 2026
@juan-g-bonilla juan-g-bonilla requested a review from a team as a code owner July 11, 2026 21:28
@juan-g-bonilla juan-g-bonilla added the bug Something isn't working label Jul 11, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8b42c3fa59

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@juan-g-bonilla juan-g-bonilla changed the title Fix/mjscene mass change and perf Fix MJScene body position pinned at reference pose on mass change Jul 11, 2026
@AVSLab AVSLab deleted a comment from chatgpt-codex-connector Bot Jul 12, 2026
Comment thread src/simulation/mujocoDynamics/_GeneralModuleFiles/_UnitTest/test_mass_update.py Outdated
Comment thread src/simulation/mujocoDynamics/_GeneralModuleFiles/MJBody.h
@juan-g-bonilla juan-g-bonilla force-pushed the fix/mjscene-mass-change-and-perf branch from 8b42c3f to 14a273e Compare July 12, 2026 03:48
@juan-g-bonilla juan-g-bonilla requested a review from schaubh July 12, 2026 03:49

@schaubh schaubh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good to go.

Maybe update the PR description to reflect the final solution? The description still says “two behaviour-preserving performance improvements,” but only the pointer-caching improvement remains. Change “two … improvements” to “a … improvement.”

…ange

mj_setConst leaves data->qpos at the model reference pose. Restore the integrator state (and clear the const-stale flag) after it, so position-dependent quantities use the current configuration.
Avoids re-fetching them through the recompile-checking accessor on every access. No behavioural change.
@juan-g-bonilla juan-g-bonilla force-pushed the fix/mjscene-mass-change-and-perf branch from 14a273e to d472c07 Compare July 13, 2026 04:29
@juan-g-bonilla juan-g-bonilla merged commit 686d3c2 into develop Jul 13, 2026
7 checks passed
@juan-g-bonilla juan-g-bonilla deleted the fix/mjscene-mass-change-and-perf branch July 13, 2026 07:16
@github-project-automation github-project-automation Bot moved this to ✅ Done in Basilisk Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

MJScene reports body position pinned at the reference pose when a body's mass changes

2 participants