Gazebo | Ignition | Community
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Gravity does not apply on an object if it's being moved by a SetLinearVel call because setting the velocity of an object overrides any forces being applied on the object. Instead, you need to use AddForce on the links of the model. You can choose to apply the force on all the links or one designated link (or multiple links). In your case, the easiest thing might be to apply it on the very first link:

class ModelPush : public ModelPlugin
{
  public: void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/)
  {
    // Store the pointer to the model
    this->model = _parent;
    this->baseLink = _parent->GetLinks()[0];

    // Listen to the update event. This event is broadcast every
    // simulation iteration.
    this->updateConnection = event::Events::ConnectWorldUpdateBegin(
        std::bind(&ModelPush::OnUpdate, this));
  }

  // Called by the world update start event
  public: void OnUpdate()
  {
    // Apply a small linear velocity to all the base link the model.
    this->baseLink->AddForce(ignition::math::Vector3d(.3, 0, 0));
  }

  // Pointer to the model
  private: physics::ModelPtr model;
  private: physics::LinkPtr baseLink;

  // Pointer to the update event connection
  private: event::ConnectionPtr updateConnection;
};