Question about contact normal

Discussion in 'Modeling' started by Gabriella, Oct 20, 2020.

  1. Hi all :)

    I was wondering whether there is an example of obtaining the contact normal between two bodies that are in contact?

    Thank you in advance.
     
  2. Hi,

    The contact normal between geoms of two bodies can be taken from the mjContact's frame:
    http://www.mujoco.org/book/reference.html#mjContact

    I never needed to work with it directly, but I have some sample code for printing distances of all contacts during a running simulation:

    void print_all_contacts(void)
    {
    char buf[mjMAXUINAME];
    int bufi;

    int ncon = 0;
    for (int i = 0; i < d->ncon; ++i)
    {
    // dist <=0 is contact
    if (d->contact.dist > 0)
    continue;

    // first geom
    bufi = d->contact.geom1; // first geom of the contact
    bufi = m->geom_bodyid[bufi]; // its body id
    mju_strncpy(buf, m->names+m->name_bodyadr[bufi], mjMAXUINAME); // location of the name in buffer
    std::cout << "\t\tBody1 #" << bufi << " " << buf << " ";

    // same for the second geom
    mju_strncpy(buf, m->names+m->name_bodyadr[m->geom_bodyid[d->contact.geom2]], mjMAXUINAME);
    std::cout << "Body2 #" << m->geom_bodyid[d->contact.geom2] << " " << buf << " ";

    std::cout << "dist: " << d->contact.dist << " ";

    std::cout << std::endl;
    }
    if (ncon > 0)
    std::cout << "\tContacts detected: " << ncon << std::endl;
    }

    Does this help?