Skip to content

mujoco: Add support for GetContactsFromLastStep feature#889

Draft
iche033 wants to merge 4 commits intomujoco_prototypefrom
iche033/mujoco_get_contacts
Draft

mujoco: Add support for GetContactsFromLastStep feature#889
iche033 wants to merge 4 commits intomujoco_prototypefrom
iche033/mujoco_get_contacts

Conversation

@iche033
Copy link
Contributor

@iche033 iche033 commented Mar 6, 2026

🎉 New feature

Addresses #299

Builds on top of #811

Summary

Added the GetContactsFromLastStep feature for mujoco plugin.

  • Added a geomIdToShapeInfo map to associate internal MuJoCo geom indices with ShapeInfo entities
  • Explicitly name the root "world" body in mjSpec, otherwise collisions test fails for the case when a fixed joint is created between a link and world
  • Refactored SDFFeatures to use mjsBody* pointers (instead of names) for parent-child resolution. Names are not always correct since mujoco uses scoped names.

Other changes:

  • Disabled LTO for mujoco and its dependencies in CMake otherwise I run into undefined symbol issue
  • Added a linker flag to prevent the bundled tinyxml2 from leaking symbols which conflicts with system version

Test it

The tests below should pass.

ctest -R simulation_features_mujoco
ctest -R collisions_mujoco

Checklist

  • Signed all commits for DCO
  • Added a screen capture or video to the PR description that demonstrates the feature
  • Added tests
  • Added example and/or tutorial
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • Updated Bazel files (if adding new files). Created an issue otherwise.
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers
  • Was GenAI used to generate this PR? If so, make sure to add "Generated-by" to your commits. (See this policy for more info.)

Generated-by: Gemini 3

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by and Generated-by messages.

Backports: If this is a backport, please use Rebase and Merge instead.

@iche033 iche033 requested review from azeey and scpeters as code owners March 6, 2026 21:52
@iche033 iche033 marked this pull request as draft March 6, 2026 21:52
@iche033 iche033 changed the title Add support for GetContactsFromLastStep feature in mujoco implementation mujoco: Add support for GetContactsFromLastStep feature Mar 6, 2026
@iche033 iche033 force-pushed the iche033/mujoco_get_contacts branch from 5ab3e5a to ab4ab42 Compare March 6, 2026 22:05
CMakeLists.txt Outdated
Comment on lines +56 to +89
# Ensure LTO is disabled for the mujoco target and its dependencies so it
# builds a standard shared library
if(TARGET mujoco)
set_target_properties(mujoco PROPERTIES INTERPROCEDURAL_OPTIMIZATION OFF)

# Workaround for https://github.com/google-deepmind/mujoco/pull/3069
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "GNU")
target_compile_options(mujoco PRIVATE -Wno-error=stringop-truncation)
endif()

# MuJoCo's build system forces CMAKE_INTERPROCEDURAL_OPTIMIZATION ON for its
# dependencies (ccd, lodepng, etc.). We need to disable it for them too,
# otherwise they are built as LTO bitcode which cannot be properly bundled
# into a non-LTO shared library using --whole-archive.
# See, issue: https://github.com/google-deepmind/mujoco/issues/2904
set(MUJOCO_DEPS ccd lodepng qhullstatic_r tinyobjloader tinyxml2)
foreach(dep ${MUJOCO_DEPS})
if(TARGET ${dep})
set_target_properties(${dep} PROPERTIES INTERPROCEDURAL_OPTIMIZATION OFF)
endif()
endforeach()

if(NOT APPLE AND NOT MSVC)
# Prevent bundled static libraries (like tinyxml2) from exporting symbols,
# avoiding conflicts with system versions.
target_link_options(mujoco PRIVATE "-Wl,--exclude-libs,ALL")
endif()
endif()

# We use target_link_libraries to use the proper mujoco::mujoco alias
add_library(mujoco_fetched INTERFACE IMPORTED)
target_link_libraries(mujoco_fetched INTERFACE mujoco::mujoco)
target_link_options(mujoco_fetched INTERFACE
"-Wl,-rpath,$<TARGET_FILE_DIR:mujoco>")
Copy link
Contributor

Choose a reason for hiding this comment

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

I found that the main problem was the use of FetchContent which caused targets and cmake flags from mujoco and its dependencies to leak into gz-physics targets. For example, the tinyxml2 static library was being linked against the dartsim plugin. I've changed it to use ExternalProject_Add which is similar to compiling mujoco externally (d7e5de5). I now have all (unskipped) tests passing on Linux and macOS. Can you give it a look?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the cmake changes in this PR as I confirmed that d7e5de5 fixes the tinyxml2 symbol issue.

I also removed the LTO changes in CMake. Turns out it's because on my machine I also have lld installed and it's picking that up as the linker instead of default gnu bfd. After removing lld, libmujoco.so is linked properly with correct symbols.

Signed-off-by: Ian Chen <iche@intrinsic.ai>
@iche033 iche033 added the mujoco label Mar 10, 2026
Ian Chen added 2 commits March 10, 2026 23:24
Signed-off-by: Ian Chen <iche@intrinsic.ai>
Signed-off-by: Ian Chen <iche@intrinsic.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Inbox

Development

Successfully merging this pull request may close these issues.

2 participants