Applying External Force at a Position

Discussion in 'Simulation' started by Se-Joon Chung, Nov 3, 2015.

  1. How can I apply external force at a position in Cartesian space? For example, if I want to apply force of (0,1,0) N at the position (2,3,4), how can I achieve this in MuJoCo? In particular, how can I determine if the position is within the bounds of the object geometry to see if the force should be applied at all?
     
    Last edited: Nov 3, 2015
  2. Emo Todorov

    Emo Todorov Administrator Staff Member

    These are two unrelated questions. To determine if a given point is inside an object, you need collision detection in general. There is a way to get MuJoCo to generate contacts for information purposes, without applying contact forces. This is done by adjusting the "margin" and "gap" attributes of geoms or contact pairs; see MJCF documentation. The contact is detected and included in the global array d->contact when the distance is below margin. Contact force is applied when the distance is below margin-gap. So the gap is an area where contacts are detected and included in the global array for the benefit of the user. That being said, I am not sure why you want to apply a custom force only if the point is inside the body... it sounds like you may be trying to replicate the job of the contact solver.

    As for applying the force, this is done with d->qfrc_applied or d->xfrc_applied. xfrc_applied contains a 3D force and 3D torque for each body, and is applied at the center of mass of the body. If the point of application is elsewhere, you have to compute the corresponding force in generalized coordinates and include it in qfrc_applied. The function mj_applyFT does that: it computes the generalized force and adds it to a vector of generalized forces that you specified (so you would normally pass d->qfrc_applied as the last argument to this function). There is also a manual way of doing it, which is to get the Jacobian of the point with mj_jac, multiply the force by the Jacobian transposed, and add the result to qfrc_applied. This is what mj_applyFT does internally.