google-deepmind / google-deepmind/mujoco
MJX: add differentiable distance calculation
- Dominant language
- C++
- Stars
- 15.2k
- Forks
- 1.8k
- Avg merge
- 10d 16h
- Merged PRs (30d)
- 25
Description
### The feature, motivation and pitch
I am working on a problem where I need to differentiably calculate the distance between two convex objects, including when they are _not_ colliding. For many of the collision algorithms, the distance is hard-set to 1 when there is no contact, breaking the gradient chain, and the function `mj_geomDistance` does not appear to exist in MJX (unless I am missing something).
Either porting `mj_geomDistance`, or setting a flag that lets the collision algorithm leave the positive distance in place rather than hard-setting it to 1, would be appreciated.
### Alternatives
Currently, since one of the objects is a sphere, I manually edit https://github.com/google-deepmind/mujoco/blob/82e92cbcaae55b381a34de58be84b5a3e8c18093/mjx/mujoco/mjx/_src/collision_convex.py#L217 to `dist = d - sphere.size[0]`.
I have no idea how this might break the simulation (other collision algorithms currently set distance to 1 whenever there is no contact). It has not caused me trouble so far.
Note I also need the "contact" normal, but the frame is set appropriately even when there is positive signed distance.
### Additional context
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.