]> git.mxchange.org Git - flightgear.git/blob - src/Systems/submodel.cxx
Rearrange ID related code. The (this) pointer is now the unique ID of the AIModel...
[flightgear.git] / src / Systems / submodel.cxx
1 // submodel.cxx - models a releasable submodel.
2 // Written by Dave Culp, started Aug 2004
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6 #include "submodel.hxx"
7
8 #include <simgear/structure/exception.hxx>
9 #include <simgear/misc/sg_path.hxx>
10
11 #include <Main/fg_props.hxx>
12 #include <Main/util.hxx>
13 #include <AIModel/AIManager.hxx>
14
15
16 SubmodelSystem::SubmodelSystem ()
17 {
18     
19   x_offset = y_offset = 0.0;
20   z_offset = -4.0;
21   pitch_offset = 2.0;
22   yaw_offset = 0.0;
23   
24   out[0] = out[1] = out[2] = 0;
25   in[3] = out[3] = 1;
26 }
27
28 SubmodelSystem::~SubmodelSystem ()
29 {
30 }
31
32 void
33 SubmodelSystem::init ()
34 {
35     load();
36     _serviceable_node = fgGetNode("/sim/systems/submodels/serviceable", true);
37
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);
41
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);
46
47     _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
48
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);
51
52     ai = (FGAIManager*)globals->get_subsystem("ai_model");
53
54
55 }
56
57 void
58 SubmodelSystem::bind ()
59 {
60 }
61
62 void
63 SubmodelSystem::unbind ()
64 {
65   submodel_iterator = submodels.begin();
66   while(submodel_iterator != submodels.end()) {
67     (*submodel_iterator)->prop->untie("count");
68     ++submodel_iterator;
69   }
70 }
71
72 void
73 SubmodelSystem::update (double dt)
74 {
75   if (!(_serviceable_node->getBoolValue())) return;
76   int i=-1;
77   submodel_iterator = submodels.begin();
78   while(submodel_iterator != submodels.end()) {
79     i++;
80     if ((*submodel_iterator)->trigger->getBoolValue()) {
81         if ((*submodel_iterator)->count != 0) {
82           release( (*submodel_iterator), dt);
83         } 
84     } 
85     ++submodel_iterator;
86   }
87    
88 }
89
90 bool
91 SubmodelSystem::release (submodel* sm, double dt)
92 {
93   sm->timer += dt;
94   if (sm->timer < sm->delay) return false;
95   sm->timer = 0.0;
96
97   transform(sm);  // calculate submodel's initial conditions in world-coordinates
98
99   //cout << "Creating a submodel." << endl; 
100   FGAIModelEntity entity;
101
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 );
116
117   //cout << "Submodel created." << endl;
118   if (sm->count > 0) (sm->count)--; 
119
120   return true;                    
121 }
122
123 void
124 SubmodelSystem::load ()
125 {
126     int i;
127     SGPropertyNode *path = fgGetNode("/sim/systems/submodels/path");
128     SGPropertyNode root;
129
130     if (path) {
131       SGPath config( globals->get_fg_root() );
132       config.append( path->getStringValue() );
133
134       try {
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;
140         return;
141       }
142     }
143
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); 
167
168      sm->trigger->setBoolValue(false);
169      sm->timer = sm->delay;
170
171      sm->prop = fgGetNode("/systems/submodels/submodel", i, true);
172      sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
173
174 //       in[0] = sm->x_offset;
175 //       in[1] = sm->y_offset;
176 //       in[2] = sm->z_offset; 
177  
178
179      submodels.push_back( sm );
180    }
181
182   submodel_iterator = submodels.begin();
183   // cout << submodels.size() << " submodels read." << endl;
184 }
185
186
187 void
188 SubmodelSystem::transform( submodel* sm) 
189 {
190
191  // get initial conditions 
192     
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
199   
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();
203   
204   in[0] = sm->x_offset;
205   in[1] = sm->y_offset;
206   in[2] = sm->z_offset; 
207   
208  
209
210 // pre-process the trig functions
211
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);
218
219
220 // set up the transform matrix
221
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;
225
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;
229
230     trans[2][0] =       -1 * sinRy;
231     trans[2][1] =       sinRx * cosRy;
232     trans[2][2] =   cosRx * cosRy;
233
234
235 // multiply the input and transform matrices
236
237 //      for( int i=0; i<4; i++) {
238 //              for( int j=0; j<4; j++ ) {
239 //                      out[i] += in[j] * trans[i][j];
240 //              }
241 //      }
242
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];
246
247 // convert ft to degrees of latitude
248
249     out[0] = out[0] /(366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
250
251 // convert ft to degrees of longitude
252
253     out[1] = out[1] /(365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
254
255
256     IC.lat += out[0];
257     IC.lon += out[1];
258     IC.alt += out[2];
259     IC.elevation += sm->pitch_offset;
260     IC.azimuth   += sm->yaw_offset;
261
262
263 }
264
265 void 
266 SubmodelSystem::updatelat(double lat) 
267 {
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);
271 }
272
273 // end of submodel.cxx
274