-
Notifications
You must be signed in to change notification settings - Fork 14
Building simulations from code
edrumwri edited this page Apr 22, 2015
·
9 revisions
- Compile the doxygen documentation for Moby to get its API documentation
Moby/docs $ doxygen Moby
- Create the simulation (
Simulatorif no contacts or joint limits,EventDrivenSimulatorotherwise) - 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());
- Create one or more model(s) and add them to the simulation, see Constructing models
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:
- Select a step size (typically from 0.0001 to 0.1)
- Step the simulation
- 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();
}