Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
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
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())
00046 {
00047 if(cfgVehicle->links[part].cs)
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
00054
00055
00056 if(!cs)
00057 cs = UWSimGeometry::loadGeometry(cfgVehicle->links[part].geom);
00058
00059 if(cfgVehicle->links[part].mass > 0)
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
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
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
00080 for (unsigned int i = 0; i < scene_builder->objects.size(); i++)
00081 {
00082
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
00092 CollisionDataType * colData = new CollisionDataType(scene_builder->objects[i]->getName(), " ", 0);
00093
00094
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
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
00119
00120
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
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