]> git.mxchange.org Git - flightgear.git/blob - src/Systems/submodel.cxx
David Culp:
[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     _user_alpha_node =   fgGetNode("/orientation/alpha-deg", true);
47
48     _user_speed_node = fgGetNode("/velocities/uBody-fps", true);
49
50     _user_wind_from_east_node  = fgGetNode("/environment/wind-from-east-fps",true);
51     _user_wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
52
53     ai = (FGAIManager*)globals->get_subsystem("ai_model");
54
55
56 }
57
58 void
59 SubmodelSystem::bind ()
60 {
61 }
62
63 void
64 SubmodelSystem::unbind ()
65 {
66   submodel_iterator = submodels.begin();
67   while(submodel_iterator != submodels.end()) {
68     (*submodel_iterator)->prop->untie("count");
69     ++submodel_iterator;
70   }
71 }
72
73 void
74 SubmodelSystem::update (double dt)
75 {
76   if (!(_serviceable_node->getBoolValue())) return;
77   int i=-1;
78   submodel_iterator = submodels.begin();
79   while(submodel_iterator != submodels.end()) {
80     i++;
81     if ((*submodel_iterator)->trigger->getBoolValue()) {
82         if ((*submodel_iterator)->count != 0) {
83           release( (*submodel_iterator), dt);
84         } 
85     } else {
86       (*submodel_iterator)->first_time = true;
87     }  
88     ++submodel_iterator;
89   }
90    
91 }
92
93 bool
94 SubmodelSystem::release (submodel* sm, double dt)
95 {
96   sm->timer += dt;
97   if (sm->timer < sm->delay) return false;
98   sm->timer = 0.0;
99
100   if (sm->first_time) {
101     dt = 0.0;
102     sm->first_time = false;
103   }
104
105   transform(sm);  // calculate submodel's initial conditions in world-coordinates
106
107   FGAIModelEntity entity;
108
109   entity.path = sm->model.c_str();
110   entity.latitude = IC.lat;
111   entity.longitude = IC.lon;
112   entity.altitude = IC.alt;
113   entity.azimuth = IC.azimuth;
114   entity.elevation = IC.elevation;
115   entity.speed = IC.speed;
116   entity.eda = sm->drag_area;
117   entity.life = sm->life;
118   entity.buoyancy = sm->buoyancy;
119   entity.wind_from_east = IC.wind_from_east;
120   entity.wind_from_north = IC.wind_from_north;
121   entity.wind = sm->wind;
122   ai->createBallistic( &entity );
123  
124   if (sm->count > 0) (sm->count)--; 
125
126   return true;                    
127 }
128
129 void
130 SubmodelSystem::load ()
131 {
132     int i;
133     SGPropertyNode *path = fgGetNode("/sim/systems/submodels/path");
134     SGPropertyNode root;
135
136     if (path) {
137       SGPath config( globals->get_fg_root() );
138       config.append( path->getStringValue() );
139
140       try {
141         readProperties(config.str(), &root);
142       } catch (const sg_exception &e) {
143         SG_LOG(SG_GENERAL, SG_ALERT,
144         "Unable to read submodels file: ");
145         cout << config.str() << endl;
146         return;
147       }
148     }
149
150    int count = root.nChildren();
151    for (i = 0; i < count; i++) { 
152      // cout << "Reading submodel " << i << endl;        
153      SGPropertyNode *prop;
154      submodel* sm = new submodel;
155      SGPropertyNode * entry_node = root.getChild(i);
156      sm->trigger        = fgGetNode(entry_node->getStringValue("trigger", "none"), true);
157      sm->name           = entry_node->getStringValue("name", "none_defined");
158      sm->model          = entry_node->getStringValue("model", "Models/Geometry/rocket.ac");
159      sm->speed          = entry_node->getDoubleValue("speed", 0.0);
160      sm->repeat         = entry_node->getBoolValue  ("repeat", false); 
161      sm->delay          = entry_node->getDoubleValue("delay", 0.25); 
162      sm->count          = entry_node->getIntValue   ("count", 1); 
163      sm->slaved         = entry_node->getBoolValue  ("slaved", false); 
164      sm->x_offset       = entry_node->getDoubleValue("x-offset", 0.0); 
165      sm->y_offset       = entry_node->getDoubleValue("y-offset", 0.0); 
166      sm->z_offset       = entry_node->getDoubleValue("z-offset", 0.0); 
167      sm->yaw_offset     = entry_node->getDoubleValue("yaw-offset", 0.0); 
168      sm->pitch_offset   = entry_node->getDoubleValue("pitch-offset", 0.0);
169      sm->drag_area      = entry_node->getDoubleValue("eda", 0.007);
170      sm->life           = entry_node->getDoubleValue("life", 900.0);
171      sm->buoyancy       = entry_node->getDoubleValue("buoyancy", 0);
172      sm->wind           = entry_node->getBoolValue  ("wind", false); 
173      sm->first_time     = false;
174
175      sm->trigger->setBoolValue(false);
176      sm->timer = sm->delay;
177
178      sm->prop = fgGetNode("/systems/submodels/submodel", i, true);
179      sm->prop->tie("count", SGRawValuePointer<int>(&(sm->count)));
180
181      submodels.push_back( sm );
182    }
183
184   submodel_iterator = submodels.begin();
185   
186 }
187
188
189 void
190 SubmodelSystem::transform( submodel* sm) 
191 {
192
193  // get initial conditions 
194     
195   IC.lat =        _user_lat_node->getDoubleValue();    
196   IC.lon =        _user_lon_node->getDoubleValue();        
197   IC.alt =        _user_alt_node->getDoubleValue();    
198   IC.roll =     - _user_roll_node->getDoubleValue();    // rotation about x axis
199   IC.elevation =  _user_pitch_node->getDoubleValue();   // rotation about y axis
200   IC.azimuth =    _user_heading_node->getDoubleValue(); // rotation about z axis
201   
202   IC.speed =           _user_speed_node->getDoubleValue();
203   IC.wind_from_east =  _user_wind_from_east_node->getDoubleValue();
204   IC.wind_from_north = _user_wind_from_north_node->getDoubleValue();
205   
206   in[0] = sm->x_offset;
207   in[1] = sm->y_offset;
208   in[2] = sm->z_offset; 
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 // set up the transform matrix
220
221     trans[0][0] =  cosRy * cosRz;
222     trans[0][1] =  -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
223     trans[0][2] =  sinRx * sinRz + cosRx * sinRy * cosRz;
224
225     trans[1][0] =  cosRy * sinRz;
226     trans[1][1] =  cosRx * cosRz + sinRx * sinRy * sinRz;
227     trans[1][2] =  -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
228
229     trans[2][0] =  -1 * sinRy;
230     trans[2][1] =  sinRx * cosRy;
231     trans[2][2] =  cosRx * cosRy;
232
233
234 // multiply the input and transform matrices
235
236    out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
237    out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
238    out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
239
240    // convert ft to degrees of latitude
241    out[0] = out[0] /(366468.96 - 3717.12 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
242
243    // convert ft to degrees of longitude
244    out[1] = out[1] /(365228.16 * cos(IC.lat * SG_DEGREES_TO_RADIANS));
245
246    // set submodel initial position
247    IC.lat += out[0];
248    IC.lon += out[1];
249    IC.alt += out[2];
250
251    // get aircraft velocity vector angles in XZ and XY planes
252     //double alpha = _user_alpha_node->getDoubleValue();
253     //double velXZ = IC.elevation - alpha * cosRx;
254     //double velXY = IC.azimuth - (IC.elevation - alpha * sinRx);
255    
256    // Get submodel initial velocity vector angles in XZ and XY planes.
257    // This needs to be fixed. This vector should be added to aircraft's vector. 
258    IC.elevation += (sm->pitch_offset * cosRx) + (sm->yaw_offset * sinRx);
259    IC.azimuth   += (sm->yaw_offset * cosRx) - (sm->pitch_offset * sinRx);
260  
261    // For now assume vector is close to airplane's vector.  This needs to be fixed.
262    IC.speed += sm->speed; 
263
264 }
265
266 void 
267 SubmodelSystem::updatelat(double lat) 
268 {
269     double latitude = lat;
270     ft_per_deg_latitude = 366468.96 - 3717.12 * cos(latitude / SG_RADIANS_TO_DEGREES);
271     ft_per_deg_longitude = 365228.16 * cos(latitude / SG_RADIANS_TO_DEGREES);
272 }
273
274 // end of submodel.cxx
275