1 // submodel.cxx - models a releasable submodel.
2 // Written by Dave Culp, started Aug 2004
4 // This file is in the Public Domain and comes with no warranty.
6 #include "submodel.hxx"
8 #include <simgear/structure/exception.hxx>
9 #include <simgear/misc/sg_path.hxx>
11 #include <Main/fg_props.hxx>
12 #include <Main/util.hxx>
13 #include <AIModel/AIManager.hxx>
16 SubmodelSystem::SubmodelSystem ()
19 x_offset = y_offset = 0.0;
24 out[0] = out[1] = out[2] = 0;
28 SubmodelSystem::~SubmodelSystem ()
33 SubmodelSystem::init ()
36 _serviceable_node = fgGetNode("/sim/systems/submodels/serviceable", true);
38 _user_lat_node = fgGetNode("/position/latitude-deg", true);
39 _user_lon_node = fgGetNode("/position/longitude-deg", true);
40 _user_alt_node = fgGetNode("/position/altitude-ft", true);
42 _user_heading_node = fgGetNode("/orientation/heading-deg", true);
43 _user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
44 _user_roll_node = fgGetNode("/orientation/roll-deg", true);
45 _user_yaw_node = fgGetNode("/orientation/yaw-deg", true);
47 _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
49 _user_wind_from_east_node = fgGetNode("/environment/wind-from-east-fps",true);
50 _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
52 ai = (FGAIManager*)globals->get_subsystem("ai_model");
58 SubmodelSystem::bind ()
63 SubmodelSystem::unbind ()
65 submodel_iterator = submodels.begin();
66 while(submodel_iterator != submodels.end()) {
67 (*submodel_iterator)->prop->untie("count");
73 SubmodelSystem::update (double dt)
75 if (!(_serviceable_node->getBoolValue())) return;
77 submodel_iterator = submodels.begin();
78 while(submodel_iterator != submodels.end()) {
80 if ((*submodel_iterator)->trigger->getBoolValue()) {
81 if ((*submodel_iterator)->count != 0) {
82 release( (*submodel_iterator), dt);
91 SubmodelSystem::release (submodel* sm, double dt)
94 if (sm->timer < sm->delay) return false;
97 transform(sm); // calculate submodel's initial conditions in world-coordinates
99 //cout << "Creating a submodel." << endl;
100 FGAIModelEntity entity;
102 entity.path = sm->model.c_str();
103 entity.latitude = IC.lat;
104 entity.longitude = IC.lon;
105 entity.altitude = IC.alt;
106 entity.azimuth = IC.azimuth;
107 entity.elevation = IC.elevation;
108 entity.speed = IC.speed;
109 entity.eda = sm->drag_area;
110 entity.life = sm->life;
111 entity.buoyancy = sm->buoyancy;
112 entity.wind_from_east = IC.wind_from_east;
113 entity.wind_from_north = IC.wind_from_north;
114 entity.wind = sm->wind;
115 ai->createBallistic( &entity );
117 //cout << "Submodel created." << endl;
118 if (sm->count > 0) (sm->count)--;
124 SubmodelSystem::load ()
127 SGPropertyNode *path = fgGetNode("/sim/systems/submodels/path");
131 SGPath config( globals->get_fg_root() );
132 config.append( path->getStringValue() );
135 readProperties(config.str(), &root);
136 } catch (const sg_exception &e) {
137 SG_LOG(SG_GENERAL, SG_ALERT,
138 "Unable to read submodels file: ");
139 cout << config.str() << endl;
144 int count = root.nChildren();
145 for (i = 0; i < count; i++) {
146 // cout << "Reading submodel " << i << endl;
147 SGPropertyNode *prop;
148 submodel* sm = new submodel;
149 SGPropertyNode * entry_node = root.getChild(i);
150 sm->trigger = fgGetNode(entry_node->getStringValue("trigger", "none"), true);
151 sm->name = entry_node->getStringValue("name", "none_defined");
152 sm->model = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
153 sm->speed = entry_node->getDoubleValue("speed", 0.0);
154 sm->repeat = entry_node->getBoolValue ("repeat", false);
155 sm->delay = entry_node->getDoubleValue("delay", 0.25);
156 sm->count = entry_node->getIntValue ("count", 1);
157 sm->slaved = entry_node->getBoolValue ("slaved", false);
158 sm->x_offset = entry_node->getDoubleValue("x-offset", 0.0);
159 sm->y_offset = entry_node->getDoubleValue("y-offset", 0.0);
160 sm->z_offset = entry_node->getDoubleValue("z-offset", 0.0);
161 sm->yaw_offset = entry_node->getDoubleValue("yaw-offset", 0.0);
162 sm->pitch_offset = entry_node->getDoubleValue("pitch-offset", 0.0);
163 sm->drag_area = entry_node->getDoubleValue("eda", 0.007);
164 sm->life = entry_node->getDoubleValue("life", 900.0);
165 sm->buoyancy = entry_node->getDoubleValue("buoyancy", 0);
166 sm->wind = entry_node->getBoolValue ("wind", false);
168 sm->trigger->setBoolValue(false);
169 sm->timer = sm->delay;
171 sm->prop = fgGetNode("/systems/submodels/submodel", i, true);
172 sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
174 // in[0] = sm->x_offset;
175 // in[1] = sm->y_offset;
176 // in[2] = sm->z_offset;
179 submodels.push_back( sm );
182 submodel_iterator = submodels.begin();
183 // cout << submodels.size() << " submodels read." << endl;
188 SubmodelSystem::transform( submodel* sm)
191 // get initial conditions
193 IC.lat = _user_lat_node->getDoubleValue();
194 IC.lon = _user_lon_node->getDoubleValue();
195 IC.alt = _user_alt_node->getDoubleValue();
196 IC.roll = - _user_roll_node->getDoubleValue(); // rotation about x axis
197 IC.elevation = _user_pitch_node->getDoubleValue(); // rotation about y axis
198 IC.azimuth = _user_heading_node->getDoubleValue(); // rotation about z axis
200 IC.speed = _user_speed_node->getDoubleValue() + sm->speed;
201 IC.wind_from_east = _user_wind_from_east_node->getDoubleValue();
202 IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
204 in[0] = sm->x_offset;
205 in[1] = sm->y_offset;
206 in[2] = sm->z_offset;
210 // pre-process the trig functions
212 cosRx = cos(IC.roll * SG_DEGREES_TO_RADIANS);
213 sinRx = sin(IC.roll * SG_DEGREES_TO_RADIANS);
214 cosRy = cos(IC.elevation * SG_DEGREES_TO_RADIANS);
215 sinRy = sin(IC.elevation * SG_DEGREES_TO_RADIANS);
216 cosRz = cos(IC.azimuth * SG_DEGREES_TO_RADIANS);
217 sinRz = sin(IC.azimuth * SG_DEGREES_TO_RADIANS);
220 // set up the transform matrix
222 trans[0][0] = cosRy * cosRz;
223 trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
224 trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
226 trans[1][0] = cosRy * sinRz;
227 trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
228 trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
230 trans[2][0] = -1 * sinRy;
231 trans[2][1] = sinRx * cosRy;
232 trans[2][2] = cosRx * cosRy;
235 // multiply the input and transform matrices
237 // for( int i=0; i<4; i++) {
238 // for( int j=0; j<4; j++ ) {
239 // out[i] += in[j] * trans[i][j];
243 out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
244 out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
245 out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
247 // convert ft to degrees of latitude
249 out[0] = out[0] /(366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
251 // convert ft to degrees of longitude
253 out[1] = out[1] /(365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
259 IC.elevation += sm->pitch_offset;
260 IC.azimuth += sm->yaw_offset;
266 SubmodelSystem::updatelat(double lat)
268 double latitude = lat;
269 ft_per_deg_latitude = 366468.96 - 3717.12 * cos(latitude / SG_RADIANS_TO_DEGREES);
270 ft_per_deg_longitude = 365228.16 * cos(latitude / SG_RADIANS_TO_DEGREES);
273 // end of submodel.cxx