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();
125 //SG_LOG(SG_GENERAL, SG_DEBUG, "Submodel: Impact " << _impact << " hit! "
126 // << _hit <<" parent_subID " << parent_subID);
128 if ( parent_subID == 0) // this entry in the list has no associated submodel
129 continue; // so we can continue
131 if (_impact || _hit || _expiry) {
132 // SG_LOG(SG_GENERAL, SG_ALERT, "Submodel: Impact " << _impact << " hit! " << _hit
133 //<< " exipiry :-( " << _expiry );
135 submodel_iterator = submodels.begin();
137 while (submodel_iterator != submodels.end()) {
138 int child_ID = (*submodel_iterator)->id;
139 //cout << "Impact: parent SubID " << parent_subID << " child_ID " << child_ID << endl;
141 if ( parent_subID == child_ID ) {
142 _parent_lat = (*sm_list_itr)->_getImpactLat();
143 _parent_lon = (*sm_list_itr)->_getImpactLon();
144 _parent_elev = (*sm_list_itr)->_getImpactElevFt();
145 _parent_hdg = (*sm_list_itr)->_getImpactHdg();
146 _parent_pitch = (*sm_list_itr)->_getImpactPitch();
147 _parent_roll = (*sm_list_itr)->_getImpactRoll();
148 _parent_speed = (*sm_list_itr)->_getImpactSpeed();
149 (*submodel_iterator)->first_time = true;
150 //cout << "Impact: parent SubID = child_ID elev " << _parent_elev << endl;
152 if (release(*submodel_iterator, dt))
153 (*sm_list_itr)->setDie(true);
162 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
165 bool in_range = true;
166 bool trigger = false;
169 submodel_iterator = submodels.begin();
170 while (submodel_iterator != submodels.end()) {
174 /*SG_LOG(SG_GENERAL, SG_DEBUG,
175 "Submodels: " << (*submodel_iterator)->id
176 << " name " << (*submodel_iterator)->name
177 << " in range " << in_range);*/
179 if ((*submodel_iterator)->trigger_node != 0) {
180 _trigger_node = (*submodel_iterator)->trigger_node;
181 trigger = _trigger_node->getBoolValue();
182 //cout << (*submodel_iterator)->name << "trigger node found " << trigger << endl;
185 //cout << (*submodel_iterator)->name << "trigger node not found " << trigger << endl;
188 if (trigger && (*submodel_iterator)->count != 0) {
190 int id = (*submodel_iterator)->id;
191 string name = (*submodel_iterator)->name;
193 /*SG_LOG(SG_GENERAL, SG_DEBUG,
194 "Submodels end: " << (*submodel_iterator)->id
195 << " name " << (*submodel_iterator)->name
196 << " count " << (*submodel_iterator)->count
197 << " in range " << in_range);*/
199 release(*submodel_iterator, dt);
201 (*submodel_iterator)->first_time = true;
207 bool FGSubmodelMgr::release(submodel *sm, double dt)
209 //cout << "release id " << sm->id << " name " << sm->name
210 //<< " first time " << sm->first_time << " repeat " << sm->repeat <<
213 // only run if first time or repeat is set to true
214 if (!sm->first_time && !sm->repeat) {
215 //cout<< "not first time " << sm->first_time<< " repeat " << sm->repeat <<endl;
221 if (sm->timer < sm->delay) {
222 //cout << "not yet: timer " << sm->timer << " delay " << sm->delay << endl;
226 //cout << "released timer: " << sm->timer << " delay " << sm->delay << endl;
230 if (sm->first_time) {
232 sm->first_time = false;
235 transform(sm); // calculate submodel's initial conditions in world-coordinates
237 FGAIBallistic* ballist = new FGAIBallistic;
238 ballist->setPath(sm->model.c_str());
239 ballist->setName(sm->name);
240 ballist->setRandom(sm->random);
241 ballist->setRandomness(sm->randomness);
242 ballist->setLatitude(offsetpos.getLatitudeDeg());
243 ballist->setLongitude(offsetpos.getLongitudeDeg());
244 ballist->setAltitude(offsetpos.getElevationFt());
245 ballist->setAzimuth(IC.azimuth);
246 ballist->setElevation(IC.elevation);
247 ballist->setRoll(IC.roll);
248 ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
249 ballist->setWind_from_east(IC.wind_from_east);
250 ballist->setWind_from_north(IC.wind_from_north);
251 ballist->setMass(IC.mass);
252 ballist->setDragArea(sm->drag_area);
253 ballist->setLife(sm->life);
254 ballist->setBuoyancy(sm->buoyancy);
255 ballist->setWind(sm->wind);
256 ballist->setCd(sm->cd);
257 ballist->setStabilisation(sm->aero_stabilised);
258 ballist->setNoRoll(sm->no_roll);
259 ballist->setCollision(sm->collision);
260 ballist->setExpiry(sm->expiry);
261 ballist->setImpact(sm->impact);
262 ballist->setImpactReportNode(sm->impact_report);
263 ballist->setFuseRange(sm->fuse_range);
264 ballist->setSubmodel(sm->submodel.c_str());
265 ballist->setSubID(sm->sub_id);
266 ballist->setForceStabilisation(sm->force_stabilised);
267 ballist->setExternalForce(sm->ext_force);
268 ballist->setForcePath(sm->force_path.c_str());
276 void FGSubmodelMgr::load()
278 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
282 string Path = path->getStringValue();
283 bool Seviceable =_serviceable_node->getBoolValue();
284 setData(id, Path, Seviceable);
288 void FGSubmodelMgr::transform(submodel *sm)
290 // set initial conditions
291 if (sm->contents_node != 0) {
292 // get the weight of the contents (lbs) and convert to mass (slugs)
293 sm->contents = sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue();
294 //cout << "transform: contents " << sm->contents << endl;
295 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
296 //cout << "mass inc contents" << IC.mass << endl;
298 // set contents to 0 in the parent
299 sm->contents_node->getChild("level-gal_us",0,1)->setDoubleValue(0);
300 /*cout << "contents " << sm->contents_node->getChild("level-gal_us")->getDoubleValue()
301 << " " << sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue()
304 IC.mass = sm->weight * lbs_to_slugs;
306 // cout << "mass " << IC.mass << endl;
308 if (sm->speed_node != 0)
309 sm->speed = sm->speed_node->getDoubleValue();
312 //int sub_id = (*submodel)->sub_id;
313 string name = sm->name;
315 //cout << " name " << name << " id " << id << " sub id" << sub_id << endl;
317 // set the Initial Conditions for the types of submodel parent
319 if (_impact || _hit || _expiry) {
320 // set the data for a submodel tied to a submodel
323 IC.lat = _parent_lat;
324 IC.lon = _parent_lon;
325 IC.alt = _parent_elev;
326 IC.roll = _parent_roll; // rotation about x axis
327 IC.elevation = _parent_pitch; // rotation about y axis
328 IC.azimuth = _parent_hdg; // rotation about z axis
329 IC.speed = _parent_speed;
330 IC.speed_down_fps = 0;
331 IC.speed_east_fps = 0;
332 IC.speed_north_fps = 0;
334 } else if (id == 0) {
335 //set the data for a submodel tied to the main model
337 IC.lat = _user_lat_node->getDoubleValue();
338 IC.lon = _user_lon_node->getDoubleValue();
339 IC.alt = _user_alt_node->getDoubleValue();
340 IC.roll = _user_roll_node->getDoubleValue(); // rotation about x axis
341 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
342 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
343 IC.speed = _user_speed_node->getDoubleValue();
344 IC.speed_down_fps = _user_speed_down_fps_node->getDoubleValue();
345 IC.speed_east_fps = _user_speed_east_fps_node->getDoubleValue();
346 IC.speed_north_fps = _user_speed_north_fps_node->getDoubleValue();
349 // set the data for a submodel tied to an AI Object
350 sm_list_iterator sm_list_itr = sm_list.begin();
351 sm_list_iterator end = sm_list.end();
353 while (sm_list_itr != end) {
354 int parent_id = (*sm_list_itr)->getID();
356 if (id != parent_id) {
361 //cout << "found id " << id << endl;
362 IC.lat = (*sm_list_itr)->_getLatitude();
363 IC.lon = (*sm_list_itr)->_getLongitude();
364 IC.alt = (*sm_list_itr)->_getAltitude();
365 IC.roll = (*sm_list_itr)->_getRoll();
366 IC.elevation = (*sm_list_itr)->_getPitch();
367 IC.azimuth = (*sm_list_itr)->_getHeading();
368 IC.alt = (*sm_list_itr)->_getAltitude();
369 IC.speed = (*sm_list_itr)->_getSpeed() * SG_KT_TO_FPS;
370 IC.speed_down_fps = -(*sm_list_itr)->_getVS_fps();
371 IC.speed_east_fps = (*sm_list_itr)->_get_speed_east_fps();
372 IC.speed_north_fps = (*sm_list_itr)->_get_speed_north_fps();
378 /*cout << "heading " << IC.azimuth << endl ;
379 cout << "speed down " << IC.speed_down_fps << endl ;
380 cout << "speed east " << IC.speed_east_fps << endl ;
381 cout << "speed north " << IC.speed_north_fps << endl ;
382 cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/
384 // Set the Initial Conditions that are common to all types of parent
385 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
386 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
388 userpos.setLatitudeDeg(IC.lat);
389 userpos.setLongitudeDeg(IC.lon);
390 userpos.setElevationFt(IC.alt);
392 _x_offset = sm->x_offset;
393 _y_offset = sm->y_offset;
394 _z_offset = sm->z_offset;
398 //IC.elevation += sm->pitch_offset;
399 //IC.azimuth += sm->yaw_offset ;
401 // pre-process the trig functions
402 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
403 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
404 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
405 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
406 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
407 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
410 // Get submodel initial velocity vector angles in XZ and XY planes.
411 // This vector should be added to aircraft's vector.
412 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
413 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
415 // calculate the total speed north
416 IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
417 * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
419 // calculate the total speed east
420 IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
421 * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
423 // calculate the total speed down
424 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
427 // re-calculate speed, elevation and azimuth
428 IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
429 + IC.total_speed_east * IC.total_speed_east
430 + IC.total_speed_down * IC.total_speed_down);
432 // if speeds are low this calculation can become unreliable
434 //IC.azimuth = atan2(IC.total_speed_east, IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
435 // cout << "azimuth1 " << IC.azimuth<<endl;
437 // rationalise the output
440 else if (IC.azimuth >= 360)
442 // cout << "azimuth2 " << IC.azimuth<<endl;
444 IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
445 * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
446 * SG_RADIANS_TO_DEGREES;
450 void FGSubmodelMgr::updatelat(double lat)
452 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
453 ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
456 void FGSubmodelMgr::loadAI()
458 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
460 sm_list = ai->get_ai_list();
462 if (sm_list.empty()) {
463 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
467 sm_list_iterator sm_list_itr = sm_list.begin();
468 sm_list_iterator end = sm_list.end();
470 while (sm_list_itr != end) {
471 string path = (*sm_list_itr)->_getSMPath();
478 int id = (*sm_list_itr)->getID();
479 bool serviceable = (*sm_list_itr)->_getServiceable();
480 setData(id, path, serviceable);
487 void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
491 SGPath config(globals->get_fg_root());
493 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: path " << path);
495 SG_LOG(SG_GENERAL, SG_DEBUG,
496 "Submodels: Trying to read AI submodels file: " << config.str());
497 readProperties(config.str(), &root);
498 } catch (const sg_exception &) {
499 SG_LOG(SG_GENERAL, SG_DEBUG,
500 "Submodels: Unable to read AI submodels file: " << config.str());
504 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
505 vector<SGPropertyNode_ptr>::iterator it = children.begin();
506 vector<SGPropertyNode_ptr>::iterator end = children.end();
508 for (int i = 0; it != end; ++it, i++) {
509 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
510 submodel* sm = new submodel;
511 SGPropertyNode * entry_node = *it;
512 sm->name = entry_node->getStringValue("name", "none_defined");
513 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
514 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
515 sm->repeat = entry_node->getBoolValue("repeat", false);
516 sm->delay = entry_node->getDoubleValue("delay", 0.25);
517 sm->count = entry_node->getIntValue("count", 1);
518 sm->slaved = entry_node->getBoolValue("slaved", false);
519 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
520 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
521 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
522 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
523 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
524 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
525 sm->life = entry_node->getDoubleValue("life", 900.0);
526 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
527 sm->wind = entry_node->getBoolValue("wind", false);
528 sm->cd = entry_node->getDoubleValue("cd", 0.193);
529 sm->weight = entry_node->getDoubleValue("weight", 0.25);
530 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
531 sm->no_roll = entry_node->getBoolValue("no-roll", false);
532 sm->collision = entry_node->getBoolValue("collision", false);
533 sm->expiry = entry_node->getBoolValue("expiry", false);
534 sm->impact = entry_node->getBoolValue("impact", false);
535 sm->impact_report = entry_node->getStringValue("impact-reports");
536 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
537 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
538 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
539 sm->submodel = entry_node->getStringValue("submodel-path", "");
540 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
541 sm->ext_force = entry_node->getBoolValue("external-force", false);
542 sm->force_path = entry_node->getStringValue("force-path", "");
543 sm->random = entry_node->getBoolValue("random", false);
544 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
547 //cout << "sm->contents_node " << sm->contents_node << endl;
548 if (sm->contents_node != 0)
549 sm->contents = sm->contents_node->getDoubleValue();
551 const char *trigger_path = entry_node->getStringValue("trigger", 0);
553 sm->trigger_node = fgGetNode(trigger_path, true);
554 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
556 sm->trigger_node = 0;
559 if (sm->speed_node != 0)
560 sm->speed = sm->speed_node->getDoubleValue();
562 sm->timer = sm->delay;
564 sm->first_time = false;
565 sm->serviceable = serviceable;
568 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
569 sm->prop->tie("delay", SGRawValuePointer<double>(&(sm->delay)));
570 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
571 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
572 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
573 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
574 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
575 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
576 string name = sm->name;
577 sm->prop->setStringValue("name", name.c_str());
579 string submodel = sm->submodel;
580 sm->prop->setStringValue("submodel", submodel.c_str());
581 //cout << " set submodel path " << submodel << endl;
583 string force_path = sm->force_path;
584 sm->prop->setStringValue("force_path", force_path.c_str());
585 //cout << "set force_path Sub " << force_path << endl;
587 if (sm->contents_node != 0)
588 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
591 submodels.push_back(sm);
595 void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable)
599 SGPath config(globals->get_fg_root());
601 SG_LOG(SG_GENERAL, SG_DEBUG,
602 "Submodels: path " << path);
604 SG_LOG(SG_GENERAL, SG_DEBUG,
605 "Submodels: Trying to read AI submodels file: " << config.str());
606 readProperties(config.str(), &root);
608 } catch (const sg_exception &) {
609 SG_LOG(SG_GENERAL, SG_DEBUG,
610 "Submodels: Unable to read AI submodels file: " << config.str());
614 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
615 vector<SGPropertyNode_ptr>::iterator it = children.begin();
616 vector<SGPropertyNode_ptr>::iterator end = children.end();
618 for (int i = 0; it != end; ++it, i++) {
619 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
620 submodel* sm = new submodel;
621 SGPropertyNode * entry_node = *it;
622 sm->name = entry_node->getStringValue("name", "none_defined");
623 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
624 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
625 sm->repeat = entry_node->getBoolValue("repeat", false);
626 sm->delay = entry_node->getDoubleValue("delay", 0.25);
627 sm->count = entry_node->getIntValue("count", 1);
628 sm->slaved = entry_node->getBoolValue("slaved", false);
629 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
630 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
631 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
632 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
633 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
634 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
635 sm->life = entry_node->getDoubleValue("life", 900.0);
636 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
637 sm->wind = entry_node->getBoolValue("wind", false);
638 sm->cd = entry_node->getDoubleValue("cd", 0.193);
639 sm->weight = entry_node->getDoubleValue("weight", 0.25);
640 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
641 sm->no_roll = entry_node->getBoolValue("no-roll", false);
642 sm->collision = entry_node->getBoolValue("collision", false);
643 sm->expiry = entry_node->getBoolValue("expiry", false);
644 sm->impact = entry_node->getBoolValue("impact", false);
645 sm->impact_report = entry_node->getStringValue("impact-reports");
646 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
647 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
648 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);
649 sm->submodel = entry_node->getStringValue("submodel-path", "");
650 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
651 sm->ext_force = entry_node->getBoolValue("external-force", false);
652 sm->force_path = entry_node->getStringValue("force-path", "");
653 sm->random = entry_node->getBoolValue("random", false);
654 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
656 //cout << "sm->contents_node " << sm->contents_node << endl;
657 if (sm->contents_node != 0)
658 sm->contents = sm->contents_node->getDoubleValue();
660 const char *trigger_path = entry_node->getStringValue("trigger", 0);
662 sm->trigger_node = fgGetNode(trigger_path, true);
663 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
665 sm->trigger_node = 0;
668 if (sm->speed_node != 0)
669 sm->speed = sm->speed_node->getDoubleValue();
671 sm->timer = sm->delay;
673 sm->first_time = false;
674 sm->serviceable = serviceable;
677 sm->prop = fgGetNode("/ai/submodels/subsubmodel", index, true);
678 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
679 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
680 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
681 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
682 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
683 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
684 string name = sm->name;
685 sm->prop->setStringValue("name", name.c_str());
687 string submodel = sm->submodel;
688 sm->prop->setStringValue("submodel", submodel.c_str());
689 // cout << " set submodel path AI" << submodel<< endl;
691 string force_path = sm->force_path;
692 sm->prop->setStringValue("force_path", force_path.c_str());
693 //cout << "set force_path AI" << force_path << endl;
695 if (sm->contents_node != 0)
696 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
699 subsubmodels.push_back(sm);
703 void FGSubmodelMgr::loadSubmodels()
705 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading sub submodels");
709 submodel_iterator = submodels.begin();
711 while (submodel_iterator != submodels.end()) {
712 string submodel = (*submodel_iterator)->submodel;
713 if (!submodel.empty()) {
714 //int id = (*submodel_iterator)->id;
715 bool serviceable = true;
716 //SG_LOG(SG_GENERAL, SG_DEBUG, "found path sub sub "
718 // << " index " << index
719 // << "name " << (*submodel_iterator)->name);
721 if ((*submodel_iterator)->sub_id == 0){
722 (*submodel_iterator)->sub_id = index;
724 setSubData(index, submodel, serviceable);
731 subsubmodel_iterator = subsubmodels.begin();
733 while (subsubmodel_iterator != subsubmodels.end()) {
734 submodels.push_back(*subsubmodel_iterator);
735 ++subsubmodel_iterator;
738 subsubmodels.clear();
740 //submodel_iterator = submodels.begin();
742 //while (submodel_iterator != submodels.end()) {
743 //int id = (*submodel_iterator)->id;
744 //SG_LOG(SG_GENERAL, SG_DEBUG,"after pushback "
746 // << " name " << (*submodel_iterator)->name
747 // << " sub id " << (*submodel_iterator)->sub_id);
749 //++submodel_iterator;
753 SGVec3d FGSubmodelMgr::getCartOffsetPos() const{
755 // convert geodetic positions to geocentered
756 SGVec3d cartuserPos = SGVec3d::fromGeod(userpos);
758 // Transform to the right coordinate frame, configuration is done in
759 // the x-forward, y-right, z-up coordinates (feet), computation
760 // in the simulation usual body x-forward, y-right, z-down coordinates
763 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
764 _y_offset * SG_FEET_TO_METER,
765 -_z_offset * SG_FEET_TO_METER);
767 // Transform the user position to the horizontal local coordinate system.
768 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
770 // and postrotate the orientation of the user model wrt the horizontal
772 hlTrans *= SGQuatd::fromYawPitchRollDeg(
777 // The offset converted to the usual body fixed coordinate system
778 // rotated to the earth-fixed coordinates axis
779 SGVec3d off = hlTrans.backTransform(_off);
781 // Add the position offset of the user model to get the geocentered position
782 SGVec3d offsetPos = cartuserPos + off;
787 void FGSubmodelMgr::setOffsetPos(){
788 // convert the offset geocentered position to geodetic
789 SGVec3d cartoffsetPos = getCartOffsetPos();
791 SGGeodesy::SGCartToGeod(cartoffsetPos, offsetpos);
793 // end of submodel.cxx