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");
82 // _model_added_node = fgGetNode("ai/models/model-added", true);
83 //_model_added_node->addChangeListener(this, false);
87 void FGSubmodelMgr::postinit() {
88 // postinit, so that the AI list is populated
94 //TODO reload submodels if an MP ac joins
96 _model_added_node = fgGetNode("ai/models/model-added", true);
97 _model_added_node->addChangeListener(this, false);
100 void FGSubmodelMgr::bind()
103 void FGSubmodelMgr::unbind()
105 submodel_iterator = submodels.begin();
106 while (submodel_iterator != submodels.end()) {
107 (*submodel_iterator)->prop->untie("count");
112 void FGSubmodelMgr::update(double dt)
114 if (!_serviceable_node->getBoolValue())
121 // check if the submodel hit an object or terrain
122 sm_list = ai->get_ai_list();
123 sm_list_iterator sm_list_itr = sm_list.begin();
124 sm_list_iterator end = sm_list.end();
126 for (; sm_list_itr != end; ++sm_list_itr) {
127 _impact = (*sm_list_itr)->_getImpactData();
128 _hit = (*sm_list_itr)->_getCollisionData();
129 _expiry = (*sm_list_itr)->_getExpiryData();
131 int parent_subID = (*sm_list_itr)->_getSubID();
133 //SG_LOG(SG_GENERAL, SG_DEBUG, "Submodel: Impact " << _impact << " hit! "
134 // << _hit <<" parent_subID " << parent_subID);
136 if ( parent_subID == 0) // this entry in the list has no associated submodel
137 continue; // so we can continue
139 if (_impact || _hit || _expiry) {
140 // SG_LOG(SG_GENERAL, SG_ALERT, "Submodel: Impact " << _impact << " hit! " << _hit
141 //<< " exipiry :-( " << _expiry );
143 submodel_iterator = submodels.begin();
145 while (submodel_iterator != submodels.end()) {
146 int child_ID = (*submodel_iterator)->id;
147 //cout << "Impact: parent SubID " << parent_subID << " child_ID " << child_ID << endl;
149 if ( parent_subID == child_ID ) {
150 _parent_lat = (*sm_list_itr)->_getImpactLat();
151 _parent_lon = (*sm_list_itr)->_getImpactLon();
152 _parent_elev = (*sm_list_itr)->_getImpactElevFt();
153 _parent_hdg = (*sm_list_itr)->_getImpactHdg();
154 _parent_pitch = (*sm_list_itr)->_getImpactPitch();
155 _parent_roll = (*sm_list_itr)->_getImpactRoll();
156 _parent_speed = (*sm_list_itr)->_getImpactSpeed();
157 (*submodel_iterator)->first_time = true;
158 //cout << "Impact: parent SubID = child_ID elev " << _parent_elev << endl;
160 if (release(*submodel_iterator, dt))
161 (*sm_list_itr)->setDie(true);
170 _contrail_trigger->setBoolValue(_user_alt_node->getDoubleValue() > contrail_altitude);
173 // bool in_range = true;
174 bool trigger = false;
177 submodel_iterator = submodels.begin();
178 while (submodel_iterator != submodels.end()) {
181 /*SG_LOG(SG_GENERAL, SG_DEBUG,
182 "Submodels: " << (*submodel_iterator)->id
183 << " name " << (*submodel_iterator)->name
186 if ((*submodel_iterator)->trigger_node != 0) {
187 _trigger_node = (*submodel_iterator)->trigger_node;
188 trigger = _trigger_node->getBoolValue();
189 //cout << (*submodel_iterator)->name << "trigger node found " << trigger << endl;
192 //cout << (*submodel_iterator)->name << "trigger node not found " << trigger << endl;
195 if (trigger && (*submodel_iterator)->count != 0) {
197 int id = (*submodel_iterator)->id;
198 string name = (*submodel_iterator)->name;
200 SG_LOG(SG_GENERAL, SG_DEBUG,
201 "Submodels release: " << (*submodel_iterator)->id
202 << " name " << (*submodel_iterator)->name
203 << " count " << (*submodel_iterator)->count
206 release(*submodel_iterator, dt);
208 (*submodel_iterator)->first_time = true;
214 bool FGSubmodelMgr::release(submodel *sm, double dt)
216 //cout << "release id " << sm->id << " name " << sm->name
217 //<< " first time " << sm->first_time << " repeat " << sm->repeat <<
220 // only run if first time or repeat is set to true
221 if (!sm->first_time && !sm->repeat) {
222 //cout<< "not first time " << sm->first_time<< " repeat " << sm->repeat <<endl;
228 if (sm->timer < sm->delay) {
229 //cout << "not yet: timer " << sm->timer << " delay " << sm->delay << endl;
233 //cout << "released timer: " << sm->timer << " delay " << sm->delay << endl;
237 if (sm->first_time) {
239 sm->first_time = false;
242 transform(sm); // calculate submodel's initial conditions in world-coordinates
244 FGAIBallistic* ballist = new FGAIBallistic;
245 ballist->setPath(sm->model.c_str());
246 ballist->setName(sm->name);
247 ballist->setRandom(sm->random);
248 ballist->setRandomness(sm->randomness);
249 ballist->setLatitude(offsetpos.getLatitudeDeg());
250 ballist->setLongitude(offsetpos.getLongitudeDeg());
251 ballist->setAltitude(offsetpos.getElevationFt());
252 ballist->setAzimuth(IC.azimuth);
253 ballist->setElevation(IC.elevation);
254 ballist->setRoll(IC.roll);
255 ballist->setSpeed(IC.speed / SG_KT_TO_FPS);
256 ballist->setWind_from_east(IC.wind_from_east);
257 ballist->setWind_from_north(IC.wind_from_north);
258 ballist->setMass(IC.mass);
259 ballist->setDragArea(sm->drag_area);
260 ballist->setLife(sm->life);
261 ballist->setBuoyancy(sm->buoyancy);
262 ballist->setWind(sm->wind);
263 ballist->setCd(sm->cd);
264 ballist->setStabilisation(sm->aero_stabilised);
265 ballist->setNoRoll(sm->no_roll);
266 ballist->setCollision(sm->collision);
267 ballist->setExpiry(sm->expiry);
268 ballist->setImpact(sm->impact);
269 ballist->setImpactReportNode(sm->impact_report);
270 ballist->setFuseRange(sm->fuse_range);
271 ballist->setSubmodel(sm->submodel.c_str());
272 ballist->setSubID(sm->sub_id);
273 ballist->setForceStabilisation(sm->force_stabilised);
274 ballist->setExternalForce(sm->ext_force);
275 ballist->setForcePath(sm->force_path.c_str());
283 void FGSubmodelMgr::load()
285 SGPropertyNode *path = fgGetNode("/sim/submodels/path");
289 string Path = path->getStringValue();
290 bool Seviceable =_serviceable_node->getBoolValue();
291 setData(id, Path, Seviceable);
295 void FGSubmodelMgr::transform(submodel *sm)
297 // set initial conditions
298 if (sm->contents_node != 0) {
299 // get the weight of the contents (lbs) and convert to mass (slugs)
300 sm->contents = sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue();
301 //cout << "transform: contents " << sm->contents << endl;
302 IC.mass = (sm->weight + sm->contents) * lbs_to_slugs;
303 //cout << "mass inc contents" << IC.mass << endl;
305 // set contents to 0 in the parent
306 sm->contents_node->getChild("level-gal_us",0,1)->setDoubleValue(0);
307 /*cout << "contents " << sm->contents_node->getChild("level-gal_us")->getDoubleValue()
308 << " " << sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue()
311 IC.mass = sm->weight * lbs_to_slugs;
313 // cout << "mass " << IC.mass << endl;
315 if (sm->speed_node != 0)
316 sm->speed = sm->speed_node->getDoubleValue();
319 int sub_id = sm->sub_id;
320 string name = sm->name;
322 //cout << " name " << name << " id " << id << " sub id" << sub_id << endl;
324 // set the Initial Conditions for the types of submodel parent
326 if (_impact || _hit || _expiry) {
327 // set the data for a submodel tied to a submodel
330 IC.lat = _parent_lat;
331 IC.lon = _parent_lon;
332 IC.alt = _parent_elev;
333 IC.roll = _parent_roll; // rotation about x axis
334 IC.elevation = _parent_pitch; // rotation about y axis
335 IC.azimuth = _parent_hdg; // rotation about z axis
336 IC.speed = _parent_speed;
337 IC.speed_down_fps = 0;
338 IC.speed_east_fps = 0;
339 IC.speed_north_fps = 0;
341 } else if (id == 0) {
342 //set the data for a submodel tied to the main model
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 //cout << " set the data for a submodel tied to an AI Object " << id << endl;
358 sm_list_iterator sm_list_itr = sm_list.begin();
359 sm_list_iterator end = sm_list.end();
361 while (sm_list_itr != end) {
362 int parent_id = (*sm_list_itr)->getID();
364 if (id != parent_id) {
369 //cout << " AI found id " << id << " alt " << (*sm_list_itr)->_getElevationFt()<< endl;
370 IC.lat = (*sm_list_itr)->_getLatitude();
371 IC.lon = (*sm_list_itr)->_getLongitude();
372 IC.alt = (*sm_list_itr)->_getElevationFt();
373 IC.roll = (*sm_list_itr)->_getRoll();
374 IC.elevation = (*sm_list_itr)->_getPitch();
375 IC.azimuth = (*sm_list_itr)->_getHeading();
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();
387 /*cout << "heading " << IC.azimuth << endl ;
388 cout << "speed down " << IC.speed_down_fps << endl ;
389 cout << "speed east " << IC.speed_east_fps << endl ;
390 cout << "speed north " << IC.speed_north_fps << endl ;
391 cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/
393 // Set the Initial Conditions that are common to all types of parent
394 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
395 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
397 userpos.setLatitudeDeg(IC.lat);
398 userpos.setLongitudeDeg(IC.lon);
399 userpos.setElevationFt(IC.alt);
401 _x_offset = sm->x_offset;
402 _y_offset = sm->y_offset;
403 _z_offset = sm->z_offset;
407 //IC.elevation += sm->pitch_offset;
408 //IC.azimuth += sm->yaw_offset ;
410 // pre-process the trig functions
411 cosRx = cos(-IC.roll * SG_DEGREES_TO_RADIANS);
412 sinRx = sin(-IC.roll * SG_DEGREES_TO_RADIANS);
413 cosRy = cos(-IC.elevation * SG_DEGREES_TO_RADIANS);
414 sinRy = sin(-IC.elevation * SG_DEGREES_TO_RADIANS);
415 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
416 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
419 // Get submodel initial velocity vector angles in XZ and XY planes.
420 // This vector should be added to aircraft's vector.
421 IC.elevation += (sm->yaw_offset * sinRx) + (sm->pitch_offset * cosRx);
422 IC.azimuth += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
424 // calculate the total speed north
425 IC.total_speed_north = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
426 * cos(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_north_fps;
428 // calculate the total speed east
429 IC.total_speed_east = sm->speed * cos(IC.elevation * SG_DEGREES_TO_RADIANS)
430 * sin(IC.azimuth * SG_DEGREES_TO_RADIANS) + IC.speed_east_fps;
432 // calculate the total speed down
433 IC.total_speed_down = sm->speed * -sin(IC.elevation * SG_DEGREES_TO_RADIANS)
436 // re-calculate speed, elevation and azimuth
437 IC.speed = sqrt(IC.total_speed_north * IC.total_speed_north
438 + IC.total_speed_east * IC.total_speed_east
439 + IC.total_speed_down * IC.total_speed_down);
441 // if speeds are low this calculation can become unreliable
443 //IC.azimuth = atan2(IC.total_speed_east, IC.total_speed_north) * SG_RADIANS_TO_DEGREES;
444 // cout << "azimuth1 " << IC.azimuth<<endl;
446 // rationalise the output
449 else if (IC.azimuth >= 360)
451 // cout << "azimuth2 " << IC.azimuth<<endl;
453 IC.elevation = -atan(IC.total_speed_down / sqrt(IC.total_speed_north
454 * IC.total_speed_north + IC.total_speed_east * IC.total_speed_east))
455 * SG_RADIANS_TO_DEGREES;
459 void FGSubmodelMgr::updatelat(double lat)
461 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
462 ft_per_deg_longitude = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
465 void FGSubmodelMgr::loadAI()
467 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading AI submodels ");
469 sm_list = ai->get_ai_list();
471 if (sm_list.empty()) {
472 SG_LOG(SG_GENERAL, SG_ALERT, "Submodels: Unable to read AI submodel list");
476 sm_list_iterator sm_list_itr = sm_list.begin();
477 sm_list_iterator end = sm_list.end();
479 while (sm_list_itr != end) {
480 string path = (*sm_list_itr)->_getSMPath();
487 int id = (*sm_list_itr)->getID();
488 string type = (*sm_list_itr)->getTypeString();
489 bool serviceable = (*sm_list_itr)->_getServiceable();
491 //cout << "loadAI: type " << type << " path "<< path << " serviceable " << serviceable << endl;
493 setData(id, path, serviceable);
500 void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
504 SGPath config(globals->get_fg_root());
506 SG_LOG(SG_GENERAL, SG_DEBUG, "setData: path " << path);
508 SG_LOG(SG_GENERAL, SG_DEBUG,
509 "Submodels: Trying to read AI submodels file: " << config.str());
510 readProperties(config.str(), &root);
511 } catch (const sg_exception &) {
512 SG_LOG(SG_GENERAL, SG_ALERT,
513 "Submodels: Unable to read AI submodels file: " << config.str());
517 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
518 vector<SGPropertyNode_ptr>::iterator it = children.begin();
519 vector<SGPropertyNode_ptr>::iterator end = children.end();
521 for (int i = 0; it != end; ++it, i++) {
522 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
523 submodel* sm = new submodel;
524 SGPropertyNode * entry_node = *it;
525 sm->name = entry_node->getStringValue("name", "none_defined");
526 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
527 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
528 sm->repeat = entry_node->getBoolValue("repeat", false);
529 sm->delay = entry_node->getDoubleValue("delay", 0.25);
530 sm->count = entry_node->getIntValue("count", 1);
531 sm->slaved = entry_node->getBoolValue("slaved", false);
532 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
533 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
534 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
535 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
536 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
537 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
538 sm->life = entry_node->getDoubleValue("life", 900.0);
539 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
540 sm->wind = entry_node->getBoolValue("wind", false);
541 sm->cd = entry_node->getDoubleValue("cd", 0.193);
542 sm->weight = entry_node->getDoubleValue("weight", 0.25);
543 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
544 sm->no_roll = entry_node->getBoolValue("no-roll", false);
545 sm->collision = entry_node->getBoolValue("collision", false);
546 sm->expiry = entry_node->getBoolValue("expiry", false);
547 sm->impact = entry_node->getBoolValue("impact", false);
548 sm->impact_report = entry_node->getStringValue("impact-reports");
549 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
550 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
551 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-prop", "none"), false);
552 sm->submodel = entry_node->getStringValue("submodel-path", "");
553 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
554 sm->ext_force = entry_node->getBoolValue("external-force", false);
555 sm->force_path = entry_node->getStringValue("force-path", "");
556 sm->random = entry_node->getBoolValue("random", false);
557 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
560 //cout << "sm->contents_node " << sm->contents_node << endl;
561 if (sm->contents_node != 0)
562 sm->contents = sm->contents_node->getDoubleValue();
564 const char *trigger_path = entry_node->getStringValue("trigger", 0);
566 sm->trigger_node = fgGetNode(trigger_path, true);
567 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
569 sm->trigger_node = 0;
572 if (sm->speed_node != 0)
573 sm->speed = sm->speed_node->getDoubleValue();
575 sm->timer = sm->delay;
577 sm->first_time = false;
578 sm->serviceable = serviceable;
581 sm->prop = fgGetNode("/ai/submodels/submodel", index, true);
582 sm->prop->tie("delay", SGRawValuePointer<double>(&(sm->delay)));
583 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
584 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
585 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
586 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
587 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
588 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
589 string name = sm->name;
590 sm->prop->setStringValue("name", name.c_str());
592 string submodel = sm->submodel;
593 sm->prop->setStringValue("submodel", submodel.c_str());
594 //cout << " set submodel path " << submodel << endl;
596 string force_path = sm->force_path;
597 sm->prop->setStringValue("force_path", force_path.c_str());
598 //cout << "set force_path Sub " << force_path << endl;
600 if (sm->contents_node != 0)
601 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
604 submodels.push_back(sm);
608 void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable)
612 SGPath config(globals->get_fg_root());
614 SG_LOG(SG_GENERAL, SG_ALERT, "setSubData: path " << path);
617 SG_LOG(SG_GENERAL, SG_ALERT,
618 "Submodels: Trying to read AI submodels file: " << config.str());
619 readProperties(config.str(), &root);
621 } catch (const sg_exception &) {
622 SG_LOG(SG_GENERAL, SG_DEBUG,
623 "Submodels: Unable to read AI submodels file: " << config.str());
627 vector<SGPropertyNode_ptr> children = root.getChildren("submodel");
628 vector<SGPropertyNode_ptr>::iterator it = children.begin();
629 vector<SGPropertyNode_ptr>::iterator end = children.end();
631 for (int i = 0; it != end; ++it, i++) {
632 //cout << "Reading AI submodel " << (*it)->getPath() << endl;
633 submodel* sm = new submodel;
634 SGPropertyNode * entry_node = *it;
635 sm->name = entry_node->getStringValue("name", "none_defined");
636 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
637 sm->speed = entry_node->getDoubleValue("speed", 2329.4);
638 sm->repeat = entry_node->getBoolValue("repeat", false);
639 sm->delay = entry_node->getDoubleValue("delay", 0.25);
640 sm->count = entry_node->getIntValue("count", 1);
641 sm->slaved = entry_node->getBoolValue("slaved", false);
642 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
643 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
644 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
645 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
646 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
647 sm->drag_area = entry_node->getDoubleValue("eda", 0.034);
648 sm->life = entry_node->getDoubleValue("life", 900.0);
649 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
650 sm->wind = entry_node->getBoolValue("wind", false);
651 sm->cd = entry_node->getDoubleValue("cd", 0.193);
652 sm->weight = entry_node->getDoubleValue("weight", 0.25);
653 sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
654 sm->no_roll = entry_node->getBoolValue("no-roll", false);
655 sm->collision = entry_node->getBoolValue("collision", false);
656 sm->expiry = entry_node->getBoolValue("expiry", false);
657 sm->impact = entry_node->getBoolValue("impact", false);
658 sm->impact_report = entry_node->getStringValue("impact-reports");
659 sm->fuse_range = entry_node->getDoubleValue("fuse-range", 0.0);
660 sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
661 sm->speed_node = fgGetNode(entry_node->getStringValue("speed-prop", "none"), false);
662 sm->submodel = entry_node->getStringValue("submodel-path", "");
663 sm->force_stabilised= entry_node->getBoolValue("force-stabilised", false);
664 sm->ext_force = entry_node->getBoolValue("external-force", false);
665 sm->force_path = entry_node->getStringValue("force-path", "");
666 sm->random = entry_node->getBoolValue("random", false);
667 sm->randomness = entry_node->getDoubleValue("randomness", 0.5);
669 //cout << "sm->contents_node " << sm->contents_node << endl;
670 if (sm->contents_node != 0)
671 sm->contents = sm->contents_node->getDoubleValue();
673 const char *trigger_path = entry_node->getStringValue("trigger", 0);
675 sm->trigger_node = fgGetNode(trigger_path, true);
676 sm->trigger_node->setBoolValue(sm->trigger_node->getBoolValue());
678 sm->trigger_node = 0;
681 if (sm->speed_node != 0)
682 sm->speed = sm->speed_node->getDoubleValue();
684 sm->timer = sm->delay;
686 sm->first_time = false;
687 sm->serviceable = serviceable;
690 sm->prop = fgGetNode("/ai/submodels/subsubmodel", index, true);
691 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
692 sm->prop->tie("repeat", SGRawValuePointer<bool>(&(sm->repeat)));
693 sm->prop->tie("id", SGRawValuePointer<int>(&(sm->id)));
694 sm->prop->tie("sub-id", SGRawValuePointer<int>(&(sm->sub_id)));
695 sm->prop->tie("serviceable", SGRawValuePointer<bool>(&(sm->serviceable)));
696 sm->prop->tie("random", SGRawValuePointer<bool>(&(sm->random)));
697 string name = sm->name;
698 sm->prop->setStringValue("name", name.c_str());
700 string submodel = sm->submodel;
701 sm->prop->setStringValue("submodel", submodel.c_str());
702 // cout << " set submodel path AI" << submodel<< endl;
704 string force_path = sm->force_path;
705 sm->prop->setStringValue("force_path", force_path.c_str());
706 //cout << "set force_path AI" << force_path << endl;
708 if (sm->contents_node != 0)
709 sm->prop->tie("contents-lbs", SGRawValuePointer<double>(&(sm->contents)));
712 subsubmodels.push_back(sm);
716 void FGSubmodelMgr::loadSubmodels()
718 SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Loading sub submodels");
722 submodel_iterator = submodels.begin();
724 while (submodel_iterator != submodels.end()) {
725 string submodel = (*submodel_iterator)->submodel;
726 if (!submodel.empty()) {
727 //int id = (*submodel_iterator)->id;
728 bool serviceable = true;
729 //SG_LOG(SG_GENERAL, SG_DEBUG, "found path sub sub "
731 // << " index " << index
732 // << "name " << (*submodel_iterator)->name);
734 if ((*submodel_iterator)->sub_id == 0){
735 (*submodel_iterator)->sub_id = index;
737 setSubData(index, submodel, serviceable);
744 subsubmodel_iterator = subsubmodels.begin();
746 while (subsubmodel_iterator != subsubmodels.end()) {
747 submodels.push_back(*subsubmodel_iterator);
748 ++subsubmodel_iterator;
751 subsubmodels.clear();
753 //submodel_iterator = submodels.begin();
755 //while (submodel_iterator != submodels.end()) {
756 //int id = (*submodel_iterator)->id;
757 //SG_LOG(SG_GENERAL, SG_DEBUG,"after pushback "
759 // << " name " << (*submodel_iterator)->name
760 // << " sub id " << (*submodel_iterator)->sub_id);
762 //++submodel_iterator;
766 SGVec3d FGSubmodelMgr::getCartOffsetPos() const{
768 // convert geodetic positions to geocentered
769 SGVec3d cartuserPos = SGVec3d::fromGeod(userpos);
771 // Transform to the right coordinate frame, configuration is done in
772 // the x-forward, y-right, z-up coordinates (feet), computation
773 // in the simulation usual body x-forward, y-right, z-down coordinates
776 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
777 _y_offset * SG_FEET_TO_METER,
778 -_z_offset * SG_FEET_TO_METER);
780 // Transform the user position to the horizontal local coordinate system.
781 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
783 // and postrotate the orientation of the user model wrt the horizontal
785 hlTrans *= SGQuatd::fromYawPitchRollDeg(
790 // The offset converted to the usual body fixed coordinate system
791 // rotated to the earth-fixed coordinates axis
792 SGVec3d off = hlTrans.backTransform(_off);
794 // Add the position offset of the user model to get the geocentered position
795 SGVec3d offsetPos = cartuserPos + off;
800 void FGSubmodelMgr::setOffsetPos(){
801 // convert the offset geocentered position to geodetic
802 SGVec3d cartoffsetPos = getCartOffsetPos();
804 SGGeodesy::SGCartToGeod(cartoffsetPos, offsetpos);
807 void FGSubmodelMgr::valueChanged(SGPropertyNode *prop)
810 const char* _model_added = _model_added_node->getStringValue();
812 basic_string <char>::size_type indexCh2b;
814 string str2 = _model_added;
815 const char *cstr2b = "ballistic";
816 indexCh2b = str2.find( cstr2b, 0 );
818 if (indexCh2b != string::npos ){ // we will ignore Ballistic Objects - there are potentially too many
821 //cout << "model added - " << str2 <<" now do something "<< endl;
823 SGPropertyNode *a_node = fgGetNode(_model_added, true );
824 SGPropertyNode *sub_node = a_node->getChild("submodels", 0, true);
825 SGPropertyNode_ptr path_node = sub_node->getChild("path", 0, true);
827 string path = path_node->getStringValue();
830 // nothing to do - return
831 //cout << "subpath empty - return" << endl << endl;
834 //cout << "subpath not empty: " << path << endl << endl;
835 SGPropertyNode_ptr ident_node = a_node->getChild("id", 0, true);
836 int id = ident_node->getIntValue();
838 setData(id, path, true);
861 // end of submodel.cxx