1 //// submodel.cxx - models a releasable submodel.
2 // Written by Dave Culp, started Aug 2004
3 // With major additions by Vivian Meaaza 2004 - 2007
5 // This file is in the Public Domain and comes with no warranty.
11 #include "submodel.hxx"
13 #include <simgear/structure/exception.hxx>
14 #include <simgear/misc/sg_path.hxx>
15 #include <simgear/math/sg_geodesy.hxx>
16 #include <simgear/props/props_io.hxx>
18 #include <Main/fg_props.hxx>
19 #include <Main/util.hxx>
23 #include "AIManager.hxx"
24 #include "AIBallistic.hxx"
27 const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172;
29 FGSubmodelMgr::FGSubmodelMgr()
31 x_offset = y_offset = 0.0;
36 out[0] = out[1] = out[2] = 0;
38 contrail_altitude = 30000;
43 FGSubmodelMgr::~FGSubmodelMgr()
47 void FGSubmodelMgr::init()
51 _serviceable_node = fgGetNode("/sim/submodels/serviceable", true);
52 _serviceable_node->setBoolValue(true);
54 _user_lat_node = fgGetNode("/position/latitude-deg", true);
55 _user_lon_node = fgGetNode("/position/longitude-deg", true);
56 _user_alt_node = fgGetNode("/position/altitude-ft", true);
58 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
59 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
60 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
61 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
62 _user_alpha_node = fgGetNode("/orientation/alpha-deg", true);
64 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
66 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps", true);
67 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps", true);
69 _user_speed_down_fps_node = fgGetNode("/velocities/speed-down-fps", true);
70 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps", true);
71 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps", true);
73 _contrail_altitude_node = fgGetNode("/environment/params/contrail-altitude", true);
74 contrail_altitude = _contrail_altitude_node->getDoubleValue();
75 _contrail_trigger = fgGetNode("ai/submodels/contrails", true);
76 _contrail_trigger->setBoolValue(false);
78 ai = (FGAIManager*)globals->get_subsystem("ai_model");
83 void FGSubmodelMgr::postinit() {
84 // postinit, so that the AI list is populated
90 //TODO reload submodels if an MP ac joins
93 void FGSubmodelMgr::bind()
96 void FGSubmodelMgr::unbind()
98 submodel_iterator = submodels.begin();
99 while (submodel_iterator != submodels.end()) {
100 (*submodel_iterator)->prop->untie("count");
105 void FGSubmodelMgr::update(double dt)
107 if (!_serviceable_node->getBoolValue())
114 // check if the submodel hit an object or terrain
115 sm_list = ai->get_ai_list();
116 sm_list_iterator sm_list_itr = sm_list.begin();
117 sm_list_iterator end = sm_list.end();
119 for (; sm_list_itr != end; ++sm_list_itr) {
120 _impact = (*sm_list_itr)->_getImpactData();
121 _hit = (*sm_list_itr)->_getCollisionData();
122 _expiry = (*sm_list_itr)->_getExpiryData();
123 int parent_subID = (*sm_list_itr)->_getSubID();
124 //SG_LOG(SG_GENERAL, SG_DEBUG, "Submodel: Impact " << _impact << " hit! "
125 // << _hit <<" parent_subID " << parent_subID);
126 if ( parent_subID == 0) // this entry in the list has no associated submodel
127 continue; // so we can continue
129 if (_impact || _hit || _expiry) {
130 // SG_LOG(SG_GENERAL, SG_ALERT, "Submodel: Impact " << _impact << " hit! " << _hit
131 //<< " exipiry :-( " << _expiry );
133 submodel_iterator = submodels.begin();
135 while (submodel_iterator != submodels.end()) {
136 int child_ID = (*submodel_iterator)->id;
137 //cout << "Impact: parent SubID " << parent_subID << " child_ID " << child_ID << endl;
139 if ( parent_subID == child_ID ) {
140 _parent_lat = (*sm_list_itr)->_getImpactLat();
141 _parent_lon = (*sm_list_itr)->_getImpactLon();
142 _parent_elev = (*sm_list_itr)->_getImpactElevFt();
143 _parent_hdg = (*sm_list_itr)->_getImpactHdg();
144 _parent_pitch = (*sm_list_itr)->_getImpactPitch();
145 _parent_roll = (*sm_list_itr)->_getImpactRoll();
146 _parent_speed = (*sm_list_itr)->_getImpactSpeed();
147 (*submodel_iterator)->first_time = true;
148 //cout << "Impact: parent SubID = child_ID elev " << _parent_elev << endl;
150 if (release(*submodel_iterator, dt))
151 (*sm_list_itr)->setDie(true);
160 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
163 bool in_range = true;
164 bool trigger = false;
167 submodel_iterator = submodels.begin();
168 while (submodel_iterator != submodels.end()) {
172 /*SG_LOG(SG_GENERAL, SG_DEBUG,
173 "Submodels: " << (*submodel_iterator)->id
174 << " name " << (*submodel_iterator)->name
175 << " in range " << in_range);*/
177 if ((*submodel_iterator)->trigger_node != 0) {
178 _trigger_node = (*submodel_iterator)->trigger_node;
179 trigger = _trigger_node->getBoolValue();
180 //cout << (*submodel_iterator)->name << "trigger node found " << trigger << endl;
183 //cout << (*submodel_iterator)->name << "trigger node not found " << trigger << endl;
186 if (trigger && (*submodel_iterator)->count != 0) {
188 int id = (*submodel_iterator)->id;
189 string name = (*submodel_iterator)->name;
190 /*SG_LOG(SG_GENERAL, SG_DEBUG,
191 "Submodels end: " << (*submodel_iterator)->id
192 << " name " << (*submodel_iterator)->name
193 << " count " << (*submodel_iterator)->count
194 << " in range " << in_range);*/
196 release(*submodel_iterator, dt);
198 (*submodel_iterator)->first_time = true;
204 bool FGSubmodelMgr::release(submodel *sm, double dt)
206 //cout << "release id " << sm->id << " name " << sm->name
207 //<< " first time " << sm->first_time << " repeat " << sm->repeat <<
210 // only run if first time or repeat is set to true
211 if (!sm->first_time && !sm->repeat) {
212 //cout<< "not first time " << sm->first_time<< " repeat " << sm->repeat <<endl;
218 if (sm->timer < sm->delay) {
219 //cout << "not yet: timer " << sm->timer << " delay " << sm->delay << endl;
223 //cout << "released timer: " << sm->timer << " delay " << sm->delay << endl;
227 if (sm->first_time) {
229 sm->first_time = false;
232 transform(sm); // calculate submodel's initial conditions in world-coordinates
234 FGAIBallistic* ballist = new FGAIBallistic;
235 ballist->setPath(sm->model.c_str());
236 ballist->setName(sm->name);
237 ballist->setRandom(sm->random);
238 ballist->setRandomness(sm->randomness);
239 ballist->setLatitude(offsetpos.getLatitudeDeg());
240 ballist->setLongitude(offsetpos.getLongitudeDeg());
241 ballist->setAltitude(offsetpos.getElevationFt());
242 ballist->setAzimuth(IC.azimuth);
243 ballist->setElevation(IC.elevation);
244 ballist->setRoll(IC.roll);
245 ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
246 ballist->setWind_from_east(IC.wind_from_east);
247 ballist->setWind_from_north(IC.wind_from_north);
248 ballist->setMass(IC.mass);
249 ballist->setDragArea(sm->drag_area);
250 ballist->setLife(sm->life);
251 ballist->setBuoyancy(sm->buoyancy);
252 ballist->setWind(sm->wind);
253 ballist->setCd(sm->cd);
254 ballist->setStabilisation(sm->aero_stabilised);
255 ballist->setNoRoll(sm->no_roll);
256 ballist->setCollision(sm->collision);
257 ballist->setExpiry(sm->expiry);
258 ballist->setImpact(sm->impact);
259 ballist->setImpactReportNode(sm->impact_report);
260 ballist->setFuseRange(sm->fuse_range);
261 ballist->setSubmodel(sm->submodel.c_str());
262 ballist->setSubID(sm->sub_id);
263 ballist->setForceStabilisation(sm->force_stabilised);
264 ballist->setExternalForce(sm->ext_force);
265 ballist->setForcePath(sm->force_path.c_str());
273 void FGSubmodelMgr::load()
275 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
279 string Path = path->getStringValue();
280 bool Seviceable =_serviceable_node->getBoolValue();
281 setData(id, Path, Seviceable);
285 void FGSubmodelMgr::transform(submodel *sm)
287 // set initial conditions
288 if (sm->contents_node != 0) {
289 // get the weight of the contents (lbs) and convert to mass (slugs)
290 sm->contents = sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue();
291 //cout << "transform: contents " << sm->contents << endl;
292 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
293 //cout << "mass inc contents" << IC.mass << endl;
295 // set contents to 0 in the parent
296 sm->contents_node->getChild("level-gal_us",0,1)->setDoubleValue(0);
297 /*cout << "contents " << sm->contents_node->getChild("level-gal_us")->getDoubleValue()
298 << " " << sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue()
301 IC.mass = sm->weight * lbs_to_slugs;
303 // cout << "mass " << IC.mass << endl;
305 if (sm->speed_node != 0)
306 sm->speed = sm->speed_node->getDoubleValue();
309 //int sub_id = (*submodel)->sub_id;
310 string name = sm->name;
312 //cout << " name " << name << " id " << id << " sub id" << sub_id << endl;
314 // set the Initial Conditions for the types of submodel parent
316 if (_impact || _hit || _expiry) {
317 // set the data for a submodel tied to a submodel
319 //cout << "Submodels: release sub sub " << _count<< endl;
320 //cout << " id " << sm->id
321 // << " lat " << _parent_lat
322 // << " lon " << _parent_lon
323 // << " elev " << _parent_elev
324 // << " name " << sm->name
327 IC.lat = _parent_lat;
328 IC.lon = _parent_lon;
329 IC.alt = _parent_elev;
330 IC.roll = _parent_roll; // rotation about x axis
331 IC.elevation = _parent_pitch; // rotation about y axis
332 IC.azimuth = _parent_hdg; // rotation about z axis
333 IC.speed = _parent_speed;
334 IC.speed_down_fps = 0;
335 IC.speed_east_fps = 0;
336 IC.speed_north_fps = 0;
338 } else if (id == 0) {
339 //set the data for a submodel tied to the main model
340 /*cout << "Submodels: release main sub " << endl;
341 cout << " name " << sm->name
344 IC.lat = _user_lat_node->getDoubleValue();
345 IC.lon = _user_lon_node->getDoubleValue();
346 IC.alt = _user_alt_node->getDoubleValue();
347 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
348 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
349 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
350 IC.speed = _user_speed_node->getDoubleValue();
351 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
352 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
353 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
356 // set the data for a submodel tied to an AI Object
357 sm_list_iterator sm_list_itr = sm_list.begin();
358 sm_list_iterator end = sm_list.end();
360 while (sm_list_itr != end) {
361 int parent_id = (*sm_list_itr)->getID();
363 if (id != parent_id) {
368 //cout << "found id " << id << endl;
369 IC.lat = (*sm_list_itr)->_getLatitude();
370 IC.lon = (*sm_list_itr)->_getLongitude();
371 IC.alt = (*sm_list_itr)->_getAltitude();
372 IC.roll = (*sm_list_itr)->_getRoll();
373 IC.elevation = (*sm_list_itr)->_getPitch();
374 IC.azimuth = (*sm_list_itr)->_getHeading();
375 IC.alt = (*sm_list_itr)->_getAltitude();
376 IC.speed = (*sm_list_itr)->_getSpeed() * SG_KT_TO_FPS;
377 IC.speed_down_fps = -(*sm_list_itr)->_getVS_fps();
378 IC.speed_east_fps = (*sm_list_itr)->_get_speed_east_fps();
379 IC.speed_north_fps = (*sm_list_itr)->_get_speed_north_fps();
385 /*cout << "heading " << IC.azimuth << endl ;
386 cout << "speed down " << IC.speed_down_fps << endl ;
387 cout << "speed east " << IC.speed_east_fps << endl ;
388 cout << "speed north " << IC.speed_north_fps << endl ;
389 cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/
391 // Set the Initial Conditions that are common to all types of parent
392 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
393 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
395 userpos.setLatitudeDeg(IC.lat);
396 userpos.setLongitudeDeg(IC.lon);
397 userpos.setElevationFt(IC.alt);
399 _x_offset = sm->x_offset;
400 _y_offset = sm->y_offset;
401 _z_offset = sm->z_offset;
405 //IC.elevation += sm->pitch_offset;
406 //IC.azimuth += sm->yaw_offset ;
408 // pre-process the trig functions
409 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
410 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
411 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
412 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
413 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
414 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
416 //// set up the transform matrix
417 //trans[0][0] = cosRy * cosRz;
418 //trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
419 //trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
421 //trans[1][0] = cosRy * sinRz;
422 //trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
423 //trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
425 //trans[2][0] = -1 * sinRy;
426 //trans[2][1] = sinRx * cosRy;
427 //trans[2][2] = cosRx * cosRy;
430 //// multiply the input and transform matrices
431 //out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
432 //out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
433 //out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
435 //// convert ft to degrees of latitude
436 //out[0] = out[0] / (366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
438 //// convert ft to degrees of longitude
439 //out[1] = out[1] / (365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
441 //// set submodel initial position
446 // get aircraft velocity vector angles in XZ and XY planes
447 //double alpha = _user_alpha_node->getDoubleValue();
448 //double velXZ = IC.elevation - alpha * cosRx;
449 //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
451 // Get submodel initial velocity vector angles in XZ and XY planes.
452 // This vector should be added to aircraft's vector.
453 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
454 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
456 // calculate the total speed north
457 IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
458 * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
460 // calculate the total speed east
461 IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
462 * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
464 // calculate the total speed down
465 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
468 // re-calculate speed, elevation and azimuth
469 IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
470 + IC.total_speed_east * IC.total_speed_east
471 + IC.total_speed_down * IC.total_speed_down);
473 // if speeds are low this calculation can become unreliable
475 //IC.azimuth = atan2(IC.total_speed_east, IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
476 // cout << "azimuth1 " << IC.azimuth<<endl;
478 // rationalise the output
481 else if (IC.azimuth >= 360)
483 // cout << "azimuth2 " << IC.azimuth<<endl;
485 IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
486 * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
487 * SG_RADIANS_TO_DEGREES;
491 void FGSubmodelMgr::updatelat(double lat)
493 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
494 ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
497 void FGSubmodelMgr::loadAI()
499 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
501 sm_list = ai->get_ai_list();
503 if (sm_list.empty()) {
504 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
508 sm_list_iterator sm_list_itr = sm_list.begin();
509 sm_list_iterator end = sm_list.end();
511 while (sm_list_itr != end) {
512 string path = (*sm_list_itr)->_getSMPath();
519 int id = (*sm_list_itr)->getID();
520 bool serviceable = (*sm_list_itr)->_getServiceable();
521 setData(id, path, serviceable);
528 void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
532 SGPath config(globals->get_fg_root());
534 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: path " << path);
536 SG_LOG(SG_GENERAL, SG_DEBUG,
537 "Submodels: Trying to read AI submodels file: " << config.str());
538 readProperties(config.str(), &root);
539 } catch (const sg_exception &) {
540 SG_LOG(SG_GENERAL, SG_DEBUG,
541 "Submodels: Unable to read AI submodels file: " << config.str());
545 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
546 vector<SGPropertyNode_ptr>::iterator it = children.begin();
547 vector<SGPropertyNode_ptr>::iterator end = children.end();
549 for (int i = 0; it != end; ++it, i++) {
550 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
551 submodel* sm = new submodel;
552 SGPropertyNode * entry_node = *it;
553 sm->name = entry_node->getStringValue("name", "none_defined");
554 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
555 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
556 sm->repeat = entry_node->getBoolValue("repeat", false);
557 sm->delay = entry_node->getDoubleValue("delay", 0.25);
558 sm->count = entry_node->getIntValue("count", 1);
559 sm->slaved = entry_node->getBoolValue("slaved", false);
560 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
561 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
562 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
563 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
564 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
565 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
566 sm->life = entry_node->getDoubleValue("life", 900.0);
567 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
568 sm->wind = entry_node->getBoolValue("wind", false);
569 sm->cd = entry_node->getDoubleValue("cd", 0.193);
570 sm->weight = entry_node->getDoubleValue("weight", 0.25);
571 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
572 sm->no_roll = entry_node->getBoolValue("no-roll", false);
573 sm->collision = entry_node->getBoolValue("collision", false);
574 sm->expiry = entry_node->getBoolValue("expiry", false);
575 sm->impact = entry_node->getBoolValue("impact", false);
576 sm->impact_report = entry_node->getStringValue("impact-reports");
577 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
578 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
579 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
580 sm->submodel = entry_node->getStringValue("submodel-path", "");
581 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
582 sm->ext_force = entry_node->getBoolValue("external-force", false);
583 sm->force_path = entry_node->getStringValue("force-path", "");
584 sm->random = entry_node->getBoolValue("random", false);
585 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
588 //cout << "sm->contents_node " << sm->contents_node << endl;
589 if (sm->contents_node != 0)
590 sm->contents = sm->contents_node->getDoubleValue();
592 const char *trigger_path = entry_node->getStringValue("trigger", 0);
594 sm->trigger_node = fgGetNode(trigger_path, true);
595 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
597 sm->trigger_node = 0;
600 if (sm->speed_node != 0)
601 sm->speed = sm->speed_node->getDoubleValue();
603 sm->timer = sm->delay;
605 sm->first_time = false;
606 sm->serviceable = serviceable;
609 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
610 sm->prop->tie("delay", SGRawValuePointer<double>(&(sm->delay)));
611 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
612 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
613 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
614 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
615 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
616 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
617 string name = sm->name;
618 sm->prop->setStringValue("name", name.c_str());
620 string submodel = sm->submodel;
621 sm->prop->setStringValue("submodel", submodel.c_str());
622 //cout << " set submodel path " << submodel << endl;
624 string force_path = sm->force_path;
625 sm->prop->setStringValue("force_path", force_path.c_str());
626 //cout << "set force_path Sub " << force_path << endl;
628 if (sm->contents_node != 0)
629 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
632 submodels.push_back(sm);
636 void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable)
640 SGPath config(globals->get_fg_root());
642 SG_LOG(SG_GENERAL, SG_DEBUG,
643 "Submodels: path " << path);
645 SG_LOG(SG_GENERAL, SG_DEBUG,
646 "Submodels: Trying to read AI submodels file: " << config.str());
647 readProperties(config.str(), &root);
649 } catch (const sg_exception &) {
650 SG_LOG(SG_GENERAL, SG_DEBUG,
651 "Submodels: Unable to read AI submodels file: " << config.str());
655 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
656 vector<SGPropertyNode_ptr>::iterator it = children.begin();
657 vector<SGPropertyNode_ptr>::iterator end = children.end();
659 for (int i = 0; it != end; ++it, i++) {
660 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
661 submodel* sm = new submodel;
662 SGPropertyNode * entry_node = *it;
663 sm->name = entry_node->getStringValue("name", "none_defined");
664 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
665 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
666 sm->repeat = entry_node->getBoolValue("repeat", false);
667 sm->delay = entry_node->getDoubleValue("delay", 0.25);
668 sm->count = entry_node->getIntValue("count", 1);
669 sm->slaved = entry_node->getBoolValue("slaved", false);
670 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
671 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
672 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
673 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
674 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
675 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
676 sm->life = entry_node->getDoubleValue("life", 900.0);
677 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
678 sm->wind = entry_node->getBoolValue("wind", false);
679 sm->cd = entry_node->getDoubleValue("cd", 0.193);
680 sm->weight = entry_node->getDoubleValue("weight", 0.25);
681 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
682 sm->no_roll = entry_node->getBoolValue("no-roll", false);
683 sm->collision = entry_node->getBoolValue("collision", false);
684 sm->expiry = entry_node->getBoolValue("expiry", false);
685 sm->impact = entry_node->getBoolValue("impact", false);
686 sm->impact_report = entry_node->getStringValue("impact-reports");
687 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
688 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
689 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
690 sm->submodel = entry_node->getStringValue("submodel-path", "");
691 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
692 sm->ext_force = entry_node->getBoolValue("external-force", false);
693 sm->force_path = entry_node->getStringValue("force-path", "");
694 sm->random = entry_node->getBoolValue("random", false);
695 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
697 //cout << "sm->contents_node " << sm->contents_node << endl;
698 if (sm->contents_node != 0)
699 sm->contents = sm->contents_node->getDoubleValue();
701 const char *trigger_path = entry_node->getStringValue("trigger", 0);
703 sm->trigger_node = fgGetNode(trigger_path, true);
704 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
706 sm->trigger_node = 0;
709 if (sm->speed_node != 0)
710 sm->speed = sm->speed_node->getDoubleValue();
712 sm->timer = sm->delay;
714 sm->first_time = false;
715 sm->serviceable = serviceable;
718 sm->prop = fgGetNode("/ai/submodels/subsubmodel", index, true);
719 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
720 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
721 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
722 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
723 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
724 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
725 string name = sm->name;
726 sm->prop->setStringValue("name", name.c_str());
728 string submodel = sm->submodel;
729 sm->prop->setStringValue("submodel", submodel.c_str());
730 // cout << " set submodel path AI" << submodel<< endl;
732 string force_path = sm->force_path;
733 sm->prop->setStringValue("force_path", force_path.c_str());
734 cout << "set force_path AI" << force_path << endl;
736 if (sm->contents_node != 0)
737 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
740 subsubmodels.push_back(sm);
744 void FGSubmodelMgr::loadSubmodels()
746 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading sub submodels");
750 submodel_iterator = submodels.begin();
752 while (submodel_iterator != submodels.end()) {
753 string submodel = (*submodel_iterator)->submodel;
754 if (!submodel.empty()) {
755 //int id = (*submodel_iterator)->id;
756 bool serviceable = true;
757 //SG_LOG(SG_GENERAL, SG_DEBUG, "found path sub sub "
759 // << " index " << index
760 // << "name " << (*submodel_iterator)->name);
762 if ((*submodel_iterator)->sub_id == 0){
763 (*submodel_iterator)->sub_id = index;
765 setSubData(index, submodel, serviceable);
772 subsubmodel_iterator = subsubmodels.begin();
774 while (subsubmodel_iterator != subsubmodels.end()) {
775 submodels.push_back(*subsubmodel_iterator);
776 ++subsubmodel_iterator;
779 subsubmodels.clear();
781 //submodel_iterator = submodels.begin();
783 //while (submodel_iterator != submodels.end()) {
784 //int id = (*submodel_iterator)->id;
785 //SG_LOG(SG_GENERAL, SG_DEBUG,"after pushback "
787 // << " name " << (*submodel_iterator)->name
788 // << " sub id " << (*submodel_iterator)->sub_id);
790 //++submodel_iterator;
794 SGVec3d FGSubmodelMgr::getCartOffsetPos() const{
796 // convert geodetic positions to geocentered
797 SGVec3d cartuserPos = SGVec3d::fromGeod(userpos);
799 // Transform to the right coordinate frame, configuration is done in
800 // the x-forward, y-right, z-up coordinates (feet), computation
801 // in the simulation usual body x-forward, y-right, z-down coordinates
804 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
805 _y_offset * SG_FEET_TO_METER,
806 -_z_offset * SG_FEET_TO_METER);
808 // Transform the user position to the horizontal local coordinate system.
809 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
811 // and postrotate the orientation of the user model wrt the horizontal
813 hlTrans *= SGQuatd::fromYawPitchRollDeg(
818 // The offset converted to the usual body fixed coordinate system
819 // rotated to the earth-fixed coordinates axis
820 SGVec3d off = hlTrans.backTransform(_off);
822 // Add the position offset of the user model to get the geocentered position
823 SGVec3d offsetPos = cartuserPos + off;
828 void FGSubmodelMgr::setOffsetPos(){
829 // convert the offset geocentered position to geodetic
830 SGVec3d cartoffsetPos = getCartOffsetPos();
832 SGGeodesy::SGCartToGeod(cartoffsetPos, offsetpos);
834 // end of submodel.cxx