Skip to content

Building simulations from code

edrumwri edited this page Apr 22, 2015 · 9 revisions

Documentation

  • Compile the doxygen documentation for Moby to get its API documentation
Moby/docs $ doxygen Moby

Building the Simulation

  • Create the simulation (Simulator if no contacts or joint limits, EventDrivenSimulator otherwise)
  • If there are no contacts or joint limits, use Simulator
  boost::shared_ptr<Moby::Simulator> sim(new Moby::Simulator());
  • Otherwise use a TimeSteppingSimulator
  boost::shared_ptr<Moby::TimeSteppingSimulator> sim(new TimeSteppingSimulator());
  Moby::PrimitivePtr box_prim1(new Moby::BoxPrimitive(1, 1, 1));
  box_prim1->set_mass(10);

  Moby::RigidBodyPtr box(new Moby::RigidBody());
  box->set_visualization_data(box_prim1->create_visualization());
  box->set_inertia(box_prim1->get_inertia());
  box->set_enabled(true);

  sim->add_dynamic_body(box);
  • Set contact parameters for any bodies that may come into contact (unnecessary for Simulator) and add them to your simulator's contact_params
  boost::shared_ptr<Moby::ContactParameters> contact_params(new Moby::ContactParameters(ground, box));
  contact_params->epsilon = 0.0001;
  contact_params->mu_coulomb = 0.1;
  contact_params->mu_viscous = 0.0;
  contact_params->NK = 4;
  • Set unchecked pairs, denoting bodies not checked for contact (unnecessary for Simulator) by adding pairs of CollisionGeometry objects to your simulator's unchecked_pairs
  Moby::CollisionGeometryPtr box1CG(new Moby::CollisionGeometry());
  box1CG->set_single_body(box);
  box1CG->set_geometry(box_prim1);

  Moby::CollisionGeometryPtr box2CG(new Moby::CollisionGeometry());
  box2CG->set_single_body(ground);
  box2CG->set_geometry(box_prim2);


  • Create controllers for any models that you want to control programmatically. Attempting to control models between simulation steps is not recommended, because the simulator can take multiple intermediate steps between a single nominal step.
  • Run the simulation in a loop:
    1. Select a step size (typically from 0.0001 to 0.1)
    2. Step the simulation
    3. Update any graphics (you can call the the update_visualization() method if you're using OpenSceneGraph)
  double step_size = 0.01;
  while(true) {
    sim->step(step_size);
    sim->update_visualization();
  }

Wiki home

Clone this wiki locally