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;
21 _slatPos = _spoilerPos = _flapPos = 0;
22 _slatDrag = _spoilerDrag = _flapDrag = 1;
28 void Surface::setPosition(float* p)
31 for(i=0; i<3; i++) _pos[i] = p[i];
34 void Surface::getPosition(float* out)
37 for(i=0; i<3; i++) out[i] = _pos[i];
40 void Surface::setChord(float chord)
45 void Surface::setTotalDrag(float c0)
50 float Surface::getTotalDrag()
55 void Surface::setXDrag(float cx)
60 void Surface::setYDrag(float cy)
65 void Surface::setZDrag(float cz)
70 void Surface::setBaseZDrag(float cz0)
75 void Surface::setStallPeak(int i, float peak)
80 void Surface::setStall(int i, float alpha)
85 void Surface::setStallWidth(int i, float width)
90 void Surface::setOrientation(float* o)
97 void Surface::setIncidence(float angle)
102 void Surface::setSlatParams(float stallDelta, float dragPenalty)
104 _slatAlpha = stallDelta;
105 _slatDrag = dragPenalty;
108 void Surface::setFlapParams(float liftAdd, float dragPenalty)
111 _flapDrag = dragPenalty;
114 void Surface::setSpoilerParams(float liftPenalty, float dragPenalty)
116 _spoilerLift = liftPenalty;
117 _spoilerDrag = dragPenalty;
120 void Surface::setFlap(float pos)
125 void Surface::setSlat(float pos)
130 void Surface::setSpoiler(float pos)
135 // Calculate the aerodynamic force given a wind vector v (in the
136 // aircraft's "local" coordinates) and an air density rho. Returns a
137 // torque about the Y axis, too.
138 void Surface::calcForce(float* v, float rho, float* out, float* torque)
140 // Split v into magnitude and direction:
141 float vel = Math::mag3(v);
143 // Handle the blowup condition. Zero velocity means zero force by
147 for(i=0; i<3; i++) out[i] = torque[i] = 0;
151 Math::mul3(1/vel, v, out);
153 // Convert to the surface's coordinates
154 Math::vmul33(_orient, out, out);
156 // "Rotate" by the incidence angle. Assume small angles, so we
157 // need to diddle only the Z component, X is relatively unchanged
158 // by small rotations.
159 out[2] += _incidence * out[0]; // z' = z + incidence * x
161 // Diddle the Z force according to our configuration
162 float stallMul = stallFunc(out);
163 stallMul *= 1 + _spoilerPos * (_spoilerLift - 1);
164 float stallLift = (stallMul - 1) * _cz * out[2];
165 float flapLift = _cz * _flapPos * (_flapLift-1);
167 out[2] *= _cz; // scaling factor
168 out[2] += _cz*_cz0; // zero-alpha lift
172 // Airfoil lift (pre-stall and zero-alpha) torques "up" (negative
173 // torque) around the Y axis, while flap lift pushes down. Both
174 // forces are considered to act at one third chord from the
175 // edge. Convert to local (i.e. airplane) coordiantes and store
178 torque[1] = 0.1667f * _chord * (flapLift - (_cz*_cz0 + stallLift));
180 Math::tmul33(_orient, torque, torque);
182 // Diddle X (drag) and Y (side force) in the same manner
183 out[0] *= _cx * controlDrag();
186 // Reverse the incidence rotation to get back to surface
188 out[2] -= _incidence * out[0];
190 // Convert back to external coordinates
191 Math::tmul33(_orient, out, out);
193 // Add in the units to make a real force:
194 float scale = 0.5f*rho*vel*vel*_c0;
195 Math::mul3(scale, out, out);
196 Math::mul3(scale, torque, torque);
199 // Returns a multiplier for the "plain" force equations that
200 // approximates an airfoil's lift/stall curve.
201 float Surface::stallFunc(float* v)
203 // Sanity check to treat FPU psychopathology
204 if(v[0] == 0) return 1;
206 float alpha = Math::abs(v[2]/v[0]);
208 // Wacky use of indexing, see setStall*() methods.
209 int fwdBak = v[0] > 0; // set if this is "backward motion"
210 int posNeg = v[2] < 0; // set if the lift is toward -z
211 int i = (fwdBak<<1) | posNeg;
213 float stallAlpha = _stalls[i];
218 stallAlpha += _slatAlpha;
221 if(alpha > stallAlpha+_widths[i])
224 // (note mask: we want to use the "positive" stall angle here)
225 float scale = 0.5f*_peaks[fwdBak]/_stalls[i&2];
228 if(alpha <= stallAlpha)
231 // Inside the stall. Compute a cubic interpolation between the
232 // pre-stall "scale" value and the post-stall unity.
233 float frac = (alpha - stallAlpha) / _widths[i];
234 frac = frac*frac*(3-2*frac);
236 return scale*(1-frac) + frac;
239 float Surface::controlDrag()
242 d *= 1 + _spoilerPos * (_spoilerDrag - 1);
243 d *= 1 + _slatPos * (_slatDrag - 1);
245 // Negative flap deflections don't affect drag until their lift
246 // multiplier exceeds the "camber" (cz0) of the surface.
250 fp -= _cz0/(_flapLift-1);
254 d *= 1 + fp * (_flapDrag - 1);
259 }; // namespace yasim