7 // Start in a "sane" mode, so unset stuff doesn't freak us out
11 _peaks[0] = _peaks[1] = 1;
14 _stalls[i] = _widths[i] = 0;
15 _orient[0] = 1; _orient[1] = 0; _orient[2] = 0;
16 _orient[3] = 0; _orient[4] = 1; _orient[5] = 0;
17 _orient[6] = 0; _orient[7] = 0; _orient[8] = 1;
20 _slatPos = _spoilerPos = _flapPos = 0;
21 _slatDrag = _spoilerDrag = _flapDrag = 1;
27 void Surface::setPosition(float* p)
30 for(i=0; i<3; i++) _pos[i] = p[i];
33 void Surface::getPosition(float* out)
36 for(i=0; i<3; i++) out[i] = _pos[i];
39 void Surface::setChord(float chord)
44 void Surface::setTotalDrag(float c0)
49 float Surface::getTotalDrag()
54 void Surface::setXDrag(float cx)
59 void Surface::setYDrag(float cy)
64 void Surface::setZDrag(float cz)
69 void Surface::setBaseZDrag(float cz0)
74 void Surface::setStallPeak(int i, float peak)
79 void Surface::setStall(int i, float alpha)
84 void Surface::setStallWidth(int i, float width)
89 void Surface::setOrientation(float* o)
96 void Surface::setIncidence(float angle)
101 void Surface::setSlatParams(float stallDelta, float dragPenalty)
103 _slatAlpha = stallDelta;
104 _slatDrag = dragPenalty;
107 void Surface::setFlapParams(float liftAdd, float dragPenalty)
110 _flapDrag = dragPenalty;
113 void Surface::setSpoilerParams(float liftPenalty, float dragPenalty)
115 _spoilerLift = liftPenalty;
116 _spoilerDrag = dragPenalty;
119 void Surface::setFlap(float pos)
124 void Surface::setSlat(float pos)
129 void Surface::setSpoiler(float pos)
134 // Calculate the aerodynamic force given a wind vector v (in the
135 // aircraft's "local" coordinates) and an air density rho. Returns a
136 // torque about the Y axis, too.
137 void Surface::calcForce(float* v, float rho, float* out, float* torque)
139 // Split v into magnitude and direction:
140 float vel = Math::mag3(v);
142 // Handle the blowup condition. Zero velocity means zero force by
146 for(i=0; i<3; i++) out[i] = torque[i] = 0;
150 Math::mul3(1/vel, v, out);
152 // Convert to the surface's coordinates
153 Math::vmul33(_orient, out, out);
155 // "Rotate" by the incidence angle. Assume small angles, so we
156 // need to diddle only the Z component, X is relatively unchanged
157 // by small rotations.
158 out[2] += _incidence * out[0]; // z' = z + incidence * x
160 // Diddle the Z force according to our configuration
161 float stallMul = stallFunc(out);
162 stallMul *= 1 + _spoilerPos * (_spoilerLift - 1);
163 float stallLift = (stallMul - 1) * _cz * out[2];
164 float flapLift = _cz * _flapPos * (_flapLift-1);
166 out[2] *= _cz; // scaling factor
167 out[2] += _cz*_cz0; // zero-alpha lift
171 // Airfoil lift (pre-stall and zero-alpha) torques "up" (negative
172 // torque) around the Y axis, while flap lift pushes down. Both
173 // forces are considered to act at one third chord from the
174 // edge. Convert to local (i.e. airplane) coordiantes and store
177 torque[1] = 0.1667 * _chord * (flapLift - (_cz*_cz0 + stallLift));
179 Math::tmul33(_orient, torque, torque);
181 // Diddle X (drag) and Y (side force) in the same manner
182 out[0] *= _cx * controlDrag();
185 // Reverse the incidence rotation to get back to surface
187 out[2] -= _incidence * out[0];
189 // Convert back to external coordinates
190 Math::tmul33(_orient, out, out);
192 // Add in the units to make a real force:
193 float scale = 0.5*rho*vel*vel*_c0;
194 Math::mul3(scale, out, out);
195 Math::mul3(scale, torque, torque);
198 // Returns a multiplier for the "plain" force equations that
199 // approximates an airfoil's lift/stall curve.
200 float Surface::stallFunc(float* v)
202 // Sanity check to treat FPU psychopathology
203 if(v[0] == 0) return 1;
205 float alpha = Math::abs(v[2]/v[0]);
207 // Wacky use of indexing, see setStall*() methods.
208 int fwdBak = v[0] > 0; // set if this is "backward motion"
209 int posNeg = v[2] < 0; // set if the lift is toward -z
210 int i = (fwdBak<<1) | posNeg;
212 float stallAlpha = _stalls[i];
217 stallAlpha += _slatAlpha;
220 if(alpha > stallAlpha+_widths[i])
223 // (note mask: we want to use the "positive" stall angle here)
224 float scale = 0.5*_peaks[fwdBak]/_stalls[i&2];
227 if(alpha <= stallAlpha)
230 // Inside the stall. Compute a cubic interpolation between the
231 // pre-stall "scale" value and the post-stall unity.
232 float frac = (alpha - stallAlpha) / _widths[i];
233 frac = frac*frac*(3-2*frac);
235 return scale*(1-frac) + frac;
238 float Surface::controlDrag()
241 d *= 1 + _spoilerPos * (_spoilerDrag - 1);
242 d *= 1 + _slatPos * (_slatDrag - 1);
244 // Negative flap deflections don't affect drag until their lift
245 // multiplier exceeds the "camber" (cz0) of the surface.
249 fp -= _cz0/(_flapLift-1);
253 d *= 1 + fp * (_flapDrag - 1);
258 }; // namespace yasim