Skip to content

MJX: add differentiable distance calculation #3131

@egordon

Description

@egordon

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

dist = jp.where(has_separating_axis, 1.0, d - sphere.size[0])
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions