PhysicsBuilder.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2013 University of Jaume-I.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the GNU Public License v3.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.gnu.org/licenses/gpl.html
00007  * 
00008  * Contributors:
00009  *     Mario Prats
00010  *     Javier Perez
00011  */
00012 
00013 #include <uwsim/PhysicsBuilder.h>
00014 
00015 PhysicsBuilder::PhysicsBuilder(SceneBuilder * scene_builder, ConfigFile config)
00016 {
00017   loadPhysics(scene_builder, config);
00018 }
00019 
00020 void PhysicsBuilder::loadPhysics(SceneBuilder * scene_builder, ConfigFile config)
00021 {
00022 
00023   physics = new BulletPhysics(config.physicsConfig, scene_builder->scene->getOceanSurface());
00024   OSG_INFO << "Loading Physics" << std::endl;
00025 
00026   //Add physics to vehicles
00027   for (unsigned int i = 0; i < scene_builder->iauvFile.size(); i++)
00028   {
00029     for (unsigned int j = 0; j < scene_builder->iauvFile[i]->urdf->link.size(); j++)
00030     {
00031       osg::Node * link = scene_builder->iauvFile[i]->urdf->link[j];
00032       osg::Node * cs = NULL;
00033       btRigidBody* rigidBody;
00034       boost::shared_ptr<PhysicProperties> pp;
00035       pp.reset(new PhysicProperties);
00036       pp->init();
00037       pp->isKinematic = 1;
00038       pp->csType = "compound box";
00039       pp->mass=0;
00040       //Look for config vehicle, to search for physic properties.
00041       for (std::list<Vehicle>::iterator cfgVehicle = config.vehicles.begin(); cfgVehicle != config.vehicles.end();
00042           cfgVehicle++)
00043         if (cfgVehicle->name == scene_builder->iauvFile[i]->name)
00044           for (unsigned int part = 0; part < cfgVehicle->links.size(); part++)
00045             if (cfgVehicle->links[part].name == link->getName()) //Config vehicle found!
00046             {
00047               if(cfgVehicle->links[part].cs) //Add collisionShape if available
00048               {
00049                 cs = UWSimGeometry::loadGeometry(cfgVehicle->links[part].cs);
00050                 if (!cs)
00051                   std::cerr << "Collision shape couldn't load, using visual to create physics" << std::endl;
00052               }
00053               //When creating the collision shape from geometry it uses child nodes, so we need to load the  
00054               // geometry on a clean node to avoid multiple collision shapes
00055               //TODO: avoid to reload the same geometry reusing visual geometry
00056               if(!cs)
00057                 cs = UWSimGeometry::loadGeometry(cfgVehicle->links[part].geom);
00058 
00059               if(cfgVehicle->links[part].mass > 0)  //If mass is set in config add it.
00060                 pp->mass=cfgVehicle->links[part].mass;
00061             }
00062 
00063       CollisionDataType * colData = new CollisionDataType(link->getName(), scene_builder->iauvFile[i]->name, 1);
00064       rigidBody = physics->addObject(NULL, link, colData, pp, cs);
00065 
00066       //Update visual node data with physics properties
00067       osg::ref_ptr < NodeDataType > data = dynamic_cast<NodeDataType*>(link->getUserData());
00068       data->rigidBody = rigidBody;
00069       link->setUserData(data);
00070     }
00071     scene_builder->iauvFile[i]->urdf->physics = physics;
00072 
00073     assert(scene_builder->iauvFile[i]->devices.get() != NULL);
00074     //apply physics to devices
00075     for (unsigned int j = 0; j < scene_builder->iauvFile[i]->devices->all.size(); j++)
00076       scene_builder->iauvFile[i]->devices->all[j]->applyPhysics(physics);
00077   }
00078 
00079   //Add physics to objects  
00080   for (unsigned int i = 0; i < scene_builder->objects.size(); i++)
00081   {
00082     //create Matrix Transform to use it on physics
00083     osg::Matrix mat;
00084     mat.makeIdentity();
00085     osg::MatrixTransform * mt = new osg::MatrixTransform();
00086     osg::Group * parent = scene_builder->objects[i]->getParent(0);
00087     mt->addChild(scene_builder->objects[i]);
00088     parent->removeChild(scene_builder->objects[i]);
00089     parent->addChild(mt);
00090 
00091     //NodeDataType * data;
00092     CollisionDataType * colData = new CollisionDataType(scene_builder->objects[i]->getName(), " ", 0);
00093 
00094     //Search for object in config, and look for physic properties
00095     boost::shared_ptr < PhysicProperties > pp;
00096     for (std::list<Object>::iterator j = config.objects.begin(); j != config.objects.end(); j++)
00097     {
00098       if (j->name == scene_builder->objects[i]->getName())
00099       {
00100         pp = j->physicProperties;
00101       }
00102 
00103     }
00104 
00105     //Check if object has a collisionShape defined
00106     osg::Node * cs = NULL;
00107     if (pp && pp->cs != ""){
00108       boost::shared_ptr<Geometry> geom =(boost::shared_ptr<Geometry>) new Geometry;
00109       geom->file=pp->cs;
00110       geom->type=0;
00111       cs = UWSimGeometry::loadGeometry(geom);
00112     }
00113 
00114     btRigidBody* rigidBody;
00115 
00116     rigidBody = physics->addObject(mt, scene_builder->objects[i], colData, pp, cs);
00117     
00118     //wMb->setUserData(data); 
00119 
00120     //store physics in object's data
00121     osg::ref_ptr < NodeDataType > data = dynamic_cast<NodeDataType*>(scene_builder->objects[i]->getUserData());
00122     data->rigidBody = rigidBody;
00123     scene_builder->objects[i]->setUserData(data);
00124   }
00125 
00126   //Add ROSPhysInterfaces
00127   while (config.ROSPhysInterfaces.size() > 0)
00128   {
00129     ROSInterfaceInfo rosInterface = config.ROSPhysInterfaces.front();
00130 
00131     boost::shared_ptr < ROSInterface > iface;
00132 
00133     if (rosInterface.type == ROSInterfaceInfo::contactSensorToROS)
00134       iface = boost::shared_ptr < contactSensorToROS
00135           > (new contactSensorToROS(scene_builder->root, physics, rosInterface.targetName, rosInterface.topic,
00136                                     rosInterface.rate));
00137 
00138     scene_builder->ROSInterfaces.push_back(iface);
00139     config.ROSPhysInterfaces.pop_front();
00140 
00141   }
00142 
00143   OSG_INFO << "Physics Loaded!" << std::endl;
00144 
00145 }
00146 


uwsim
Author(s): Mario Prats , Javier Perez
autogenerated on Fri Aug 28 2015 13:28:58