Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow moving bodies to another parent in mjSpec #2330

Open
jjyyxx opened this issue Jan 7, 2025 · 0 comments
Open

Allow moving bodies to another parent in mjSpec #2330

jjyyxx opened this issue Jan 7, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@jjyyxx
Copy link
Contributor

jjyyxx commented Jan 7, 2025

The feature, motivation and pitch

I'm experimenting with modifying the kinematic tree during simulation. I understand that, for a single mjModel, the kinematic tree and the number of DOFs have to be fixed. So I tried out the recent mjSpec API, but found that it might not be flexible enough to achieve this seemingly simple functionality (discussed in #2327 (comment)).

Except for keyframe management, I could implement this feature when building from source as follows:

class mjCBody : public mjCBody_, private mjsBody {
  // ...
  void MoveTo(mjCBody* new_parent) {
    mjCBody* old_parent = parent;
    for (int i=0; i<old_parent->bodies.size(); i++) {
      if (old_parent->bodies[i] == this) {
        old_parent->bodies.erase(old_parent->bodies.begin() + i);
        break;
      }
    }
    new_parent->bodies.push_back(this);
    parent = new_parent;
  }
  // ...
}

void mjs_moveBody(mjsBody* parent, mjsBody* child) {
  mjCBody* child_body = static_cast<mjCBody*>(child->element);
  mjCBody* new_parent = static_cast<mjCBody*>(parent->element);
  try {
    child_body->MoveTo(new_parent);
  } catch (mjCError& e) {
    new_parent->model->SetError(e);
  }
}

Alternatives

No response

Additional context

No response

@jjyyxx jjyyxx added the enhancement New feature or request label Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant