7 // Start in a "sane" mode, so unset stuff doesn't freak us out
11 _peaks[0] = _peaks[1] = 1;
15 _widths[i] = 0.01; // half a degree
17 _orient[0] = 1; _orient[1] = 0; _orient[2] = 0;
18 _orient[3] = 0; _orient[4] = 1; _orient[5] = 0;
19 _orient[6] = 0; _orient[7] = 0; _orient[8] = 1;
23 _slatPos = _spoilerPos = _flapPos = 0;
24 _slatDrag = _spoilerDrag = _flapDrag = 1;
32 void Surface::setPosition(float* p)
35 for(i=0; i<3; i++) _pos[i] = p[i];
38 void Surface::getPosition(float* out)
41 for(i=0; i<3; i++) out[i] = _pos[i];
44 void Surface::setChord(float chord)
49 void Surface::setTotalDrag(float c0)
54 float Surface::getTotalDrag()
59 void Surface::setXDrag(float cx)
64 void Surface::setYDrag(float cy)
69 void Surface::setZDrag(float cz)
74 void Surface::setBaseZDrag(float cz0)
79 void Surface::setStallPeak(int i, float peak)
84 void Surface::setStall(int i, float alpha)
89 void Surface::setStallWidth(int i, float width)
94 void Surface::setOrientation(float* o)
101 void Surface::setIncidence(float angle)
106 void Surface::setSlatParams(float stallDelta, float dragPenalty)
108 _slatAlpha = stallDelta;
109 _slatDrag = dragPenalty;
112 void Surface::setFlapParams(float liftAdd, float dragPenalty)
115 _flapDrag = dragPenalty;
118 void Surface::setSpoilerParams(float liftPenalty, float dragPenalty)
120 _spoilerLift = liftPenalty;
121 _spoilerDrag = dragPenalty;
124 void Surface::setFlap(float pos)
129 void Surface::setSlat(float pos)
134 void Surface::setSpoiler(float pos)
139 // Calculate the aerodynamic force given a wind vector v (in the
140 // aircraft's "local" coordinates) and an air density rho. Returns a
141 // torque about the Y axis, too.
142 void Surface::calcForce(float* v, float rho, float* out, float* torque)
144 // Split v into magnitude and direction:
145 float vel = Math::mag3(v);
147 // Handle the blowup condition. Zero velocity means zero force by
151 for(i=0; i<3; i++) out[i] = torque[i] = 0;
155 Math::mul3(1/vel, v, out);
157 // Convert to the surface's coordinates
158 Math::vmul33(_orient, out, out);
160 // "Rotate" by the incidence angle. Assume small angles, so we
161 // need to diddle only the Z component, X is relatively unchanged
162 // by small rotations.
163 out[2] += _incidence * out[0]; // z' = z + incidence * x
165 // Hold onto the local wind vector so we can multiply the induced
168 Math::set3(out, lwind);
170 // Diddle the Z force according to our configuration
171 float stallMul = stallFunc(out);
172 stallMul *= 1 + _spoilerPos * (_spoilerLift - 1);
173 float stallLift = (stallMul - 1) * _cz * out[2];
174 float flaplift = flapLift(out[2]);
176 out[2] *= _cz; // scaling factor
177 out[2] += _cz*_cz0; // zero-alpha lift
181 // Airfoil lift (pre-stall and zero-alpha) torques "up" (negative
182 // torque) around the Y axis, while flap lift pushes down. Both
183 // forces are considered to act at one third chord from the
184 // edge. Convert to local (i.e. airplane) coordiantes and store
187 torque[1] = 0.1667f * _chord * (flaplift - (_cz*_cz0 + stallLift));
189 Math::tmul33(_orient, torque, torque);
191 // The X (drag) force gets diddled for control deflection
192 out[0] = controlDrag(out[2], _cx * out[0]);
194 // Add in any specific Y (side force) coefficient.
197 // Diddle the induced drag
199 Math::mul3(-1*_inducedDrag*out[2]*lwind[2], lwind, lwind);
200 Math::add3(lwind, out, out);
202 // Reverse the incidence rotation to get back to surface
204 out[2] -= _incidence * out[0];
206 // Convert back to external coordinates
207 Math::tmul33(_orient, out, out);
209 // Add in the units to make a real force:
210 float scale = 0.5f*rho*vel*vel*_c0;
211 Math::mul3(scale, out, out);
212 Math::mul3(scale, torque, torque);
215 // Returns a multiplier for the "plain" force equations that
216 // approximates an airfoil's lift/stall curve.
217 float Surface::stallFunc(float* v)
219 // Sanity check to treat FPU psychopathology
220 if(v[0] == 0) return 1;
222 float alpha = Math::abs(v[2]/v[0]);
224 // Wacky use of indexing, see setStall*() methods.
225 int fwdBak = v[0] > 0; // set if this is "backward motion"
226 int posNeg = v[2] < 0; // set if the lift is toward -z
227 int i = (fwdBak<<1) | posNeg;
229 float stallAlpha = _stalls[i];
234 stallAlpha += _slatAlpha;
237 if(alpha > stallAlpha+_widths[i])
240 // (note mask: we want to use the "positive" stall angle here)
241 float scale = 0.5f*_peaks[fwdBak]/_stalls[i&2];
244 if(alpha <= stallAlpha)
247 // Inside the stall. Compute a cubic interpolation between the
248 // pre-stall "scale" value and the post-stall unity.
249 float frac = (alpha - stallAlpha) / _widths[i];
250 frac = frac*frac*(3-2*frac);
252 return scale*(1-frac) + frac;
255 // Similar to the above -- interpolates out the flap lift past the
257 float Surface::flapLift(float alpha)
259 float flapLift = _cz * _flapPos * (_flapLift-1);
261 if(alpha < 0) alpha = -alpha;
262 if(alpha < _stalls[0])
264 else if(alpha > _stalls[0] + _widths[0])
267 float frac = (alpha - _stalls[0]) / _widths[0];
268 frac = frac*frac*(3-2*frac);
269 return flapLift * (1-frac) + frac;
272 float Surface::controlDrag(float lift, float drag)
274 // Negative flap deflections don't affect drag until their lift
275 // multiplier exceeds the "camber" (cz0) of the surface. Use a
276 // synthesized "fp" number instead of the actual flap position.
280 fp -= _cz0/(_flapLift-1);
284 // Calculate an "effective" drag -- this is the drag that would
285 // have been produced by an unflapped surface at the same lift.
286 float flapDragAoA = (_flapLift - 1 - _cz0) * _stalls[0];
287 float fd = Math::abs(lift * flapDragAoA * fp);
288 if(drag < 0) fd = -fd;
291 // Now multiply by the various control factors
292 drag *= 1 + fp * (_flapDrag - 1);
293 drag *= 1 + _spoilerPos * (_spoilerDrag - 1);
294 drag *= 1 + _slatPos * (_slatDrag - 1);
299 }; // namespace yasim