9 _base[0] = _base[1] = _base[2] = 0;
44 for(i=0; i<_surfs.size(); i++) {
45 SurfRec* s = (SurfRec*)_surfs.get(i);
51 int Wing::numSurfaces()
56 Surface* Wing::getSurface(int n)
58 return ((SurfRec*)_surfs.get(n))->surface;
61 float Wing::getSurfaceWeight(int n)
63 return ((SurfRec*)_surfs.get(n))->weight;
66 void Wing::setMirror(bool mirror)
71 void Wing::setBase(float* base)
74 for(i=0; i<3; i++) _base[i] = base[i];
77 void Wing::setLength(float length)
82 void Wing::setChord(float chord)
87 void Wing::setTaper(float taper)
92 void Wing::setSweep(float sweep)
97 void Wing::setDihedral(float dihedral)
102 void Wing::setStall(float aoa)
107 void Wing::setStallWidth(float angle)
112 void Wing::setStallPeak(float fraction)
114 _stallPeak = fraction;
117 void Wing::setCamber(float camber)
122 void Wing::setIncidence(float incidence)
124 _incidence = incidence;
126 for(i=0; i<_surfs.size(); i++)
127 ((SurfRec*)_surfs.get(i))->surface->setIncidence(incidence);
130 void Wing::setFlap0(float start, float end, float lift, float drag)
138 void Wing::setFlap1(float start, float end, float lift, float drag)
146 void Wing::setSlat(float start, float end, float aoa, float drag)
154 void Wing::setSpoiler(float start, float end, float lift, float drag)
156 _spoilerStart = start;
162 void Wing::setFlap0(float lval, float rval)
164 lval = Math::clamp(lval, -1, 1);
165 rval = Math::clamp(rval, -1, 1);
167 for(i=0; i<_flap0Surfs.size(); i++) {
168 ((Surface*)_flap0Surfs.get(i))->setFlap(lval);
169 if(_mirror) ((Surface*)_flap0Surfs.get(++i))->setFlap(rval);
173 void Wing::setFlap1(float lval, float rval)
175 lval = Math::clamp(lval, -1, 1);
176 rval = Math::clamp(rval, -1, 1);
178 for(i=0; i<_flap1Surfs.size(); i++) {
179 ((Surface*)_flap1Surfs.get(i))->setFlap(lval);
180 if(_mirror) ((Surface*)_flap1Surfs.get(++i))->setFlap(rval);
184 void Wing::setSpoiler(float lval, float rval)
186 lval = Math::clamp(lval, 0, 1);
187 rval = Math::clamp(rval, 0, 1);
189 for(i=0; i<_spoilerSurfs.size(); i++) {
190 ((Surface*)_spoilerSurfs.get(i))->setSpoiler(lval);
191 if(_mirror) ((Surface*)_spoilerSurfs.get(++i))->setSpoiler(rval);
195 void Wing::setSlat(float val)
197 val = Math::clamp(val, 0, 1);
199 for(i=0; i<_slatSurfs.size(); i++)
200 ((Surface*)_slatSurfs.get(i))->setSlat(val);
203 float Wing::getGroundEffect(float* posOut)
206 for(i=0; i<3; i++) posOut[i] = _base[i];
207 float span = _length * Math::cos(_sweep) * Math::cos(_dihedral);
208 span = 2*(span + Math::abs(_base[2]));
212 void Wing::getTip(float* tip)
214 tip[0] = -Math::tan(_sweep);
215 tip[1] = Math::cos(_dihedral);
216 tip[2] = Math::sin(_dihedral);
217 Math::unit3(tip, tip);
218 Math::mul3(_length, tip, tip);
219 Math::add3(_base, tip, tip);
222 bool Wing::isMirrored()
229 // Have we already been compiled?
230 if(_surfs.size() != 0) return;
232 // Assemble the start/end coordinates into an array, sort them,
233 // and remove duplicates. This gives us the boundaries of our
236 bounds[0] = _flap0Start; bounds[1] = _flap0End;
237 bounds[2] = _flap1Start; bounds[3] = _flap1End;
238 bounds[4] = _spoilerStart; bounds[5] = _spoilerEnd;
239 bounds[6] = _slatStart; bounds[7] = _slatEnd;
241 // Sort in increasing order
245 float minVal = bounds[i];
247 for(j=i+1; j<8; j++) {
248 if(bounds[j] < minVal) {
253 float tmp = bounds[i];
254 bounds[i] = minVal; bounds[minIdx] = tmp;
258 float last = bounds[0];
261 if(bounds[i] != last)
262 bounds[nbounds++] = bounds[i];
266 // Calculate a "nominal" segment length equal to an average chord,
267 // normalized to lie within 0-1 over the length of the wing.
268 float segLen = _chord * (0.5f*(_taper+1)) / _length;
270 // Generating a unit vector pointing out the left wing.
272 left[0] = -Math::tan(_sweep);
273 left[1] = Math::cos(_dihedral);
274 left[2] = Math::sin(_dihedral);
275 Math::unit3(left, left);
277 // Calculate coordinates for the root and tip of the wing
278 float root[3], tip[3];
279 Math::set3(_base, root);
280 Math::set3(left, tip);
281 Math::mul3(_length, tip, tip);
282 Math::add3(root, tip, tip);
284 // The wing's Y axis will be the "left" vector. The Z axis will
285 // be perpendicular to this and the local (!) X axis, because we
286 // want motion along the local X axis to be zero AoA (i.e. in the
287 // wing's XY plane) by definition. Then the local X coordinate is
289 float orient[9], rightOrient[9];
290 float *x = orient, *y = orient+3, *z = orient+6;
291 x[0] = 1; x[1] = 0; x[2] = 0;
293 Math::cross3(x, y, z);
295 Math::cross3(y, z, x);
298 // Derive the right side orientation matrix from this one.
300 for(i=0; i<9; i++) rightOrient[i] = orient[i];
302 // Negate all Y coordinates, this gets us a valid basis, but
303 // it's left handed! So...
304 for(i=1; i<9; i+=3) rightOrient[i] = -rightOrient[i];
306 // Change the direction of the Y axis to get back to a
307 // right-handed system.
308 for(i=3; i<6; i++) rightOrient[i] = -rightOrient[i];
311 // Now go through each boundary and make segments
312 for(i=0; i<(nbounds-1); i++) {
313 float start = bounds[i];
314 float end = bounds[i+1];
315 float mid = (start+end)/2;
317 bool flap0=0, flap1=0, slat=0, spoiler=0;
318 if(_flap0Start < mid && mid < _flap0End) flap0 = 1;
319 if(_flap1Start < mid && mid < _flap1End) flap1 = 1;
320 if(_slatStart < mid && mid < _slatEnd) slat = 1;
321 if(_spoilerStart < mid && mid < _spoilerEnd) spoiler = 1;
323 // FIXME: Should probably detect an error here if both flap0
324 // and flap1 are set. Right now flap1 overrides.
326 int nSegs = (int)Math::ceil((end-start)/segLen);
327 float segWid = _length * (end - start)/nSegs;
330 for(j=0; j<nSegs; j++) {
331 float frac = start + (j+0.5f) * (end-start)/nSegs;
333 interp(root, tip, frac, pos);
335 float chord = _chord * (1 - (1-_taper)*frac);
337 Surface *s = newSurface(pos, orient, chord,
338 flap0, flap1, slat, spoiler);
340 SurfRec *sr = new SurfRec();
342 sr->weight = chord * segWid;
343 s->setTotalDrag(sr->weight);
348 s = newSurface(pos, rightOrient, chord,
349 flap0, flap1, slat, spoiler);
352 sr->weight = chord * segWid;
353 s->setTotalDrag(sr->weight);
359 // Last of all, re-set the incidence in case setIncidence() was
360 // called before we were compiled.
361 setIncidence(_incidence);
364 float Wing::getDragScale()
369 void Wing::setDragScale(float scale)
373 for(i=0; i<_surfs.size(); i++) {
374 SurfRec* s = (SurfRec*)_surfs.get(i);
375 s->surface->setTotalDrag(scale * s->weight);
379 void Wing::setLiftRatio(float ratio)
383 for(i=0; i<_surfs.size(); i++)
384 ((SurfRec*)_surfs.get(i))->surface->setZDrag(ratio);
387 float Wing::getLiftRatio()
392 Surface* Wing::newSurface(float* pos, float* orient, float chord,
393 bool flap0, bool flap1, bool slat, bool spoiler)
395 Surface* s = new Surface();
398 s->setOrientation(orient);
401 // Camber is expressed as a fraction of stall peak, so convert.
402 s->setBaseZDrag(_camber*_stallPeak);
404 // The "main" (i.e. normal) stall angle
405 float stallAoA = _stall - _stallWidth/4;
406 s->setStall(0, stallAoA);
407 s->setStallWidth(0, _stallWidth);
408 s->setStallPeak(0, _stallPeak);
410 // The negative AoA stall is the same if we're using an uncambered
411 // airfoil, otherwise a "little badder".
413 s->setStall(1, stallAoA * 0.8f);
414 s->setStallWidth(1, _stallWidth * 0.5f);
416 s->setStall(1, stallAoA);
417 s->setStall(1, _stallWidth);
420 // The "reverse" stalls are unmeasurable junk. Just use 13deg and
422 s->setStallPeak(1, 1);
425 s->setStall(i, 0.2267f);
426 s->setStallWidth(i, 1);
429 if(flap0) s->setFlapParams(_flap0Lift, _flap0Drag);
430 if(flap1) s->setFlapParams(_flap1Lift, _flap1Drag);
431 if(slat) s->setSlatParams(_slatAoA, _slatDrag);
432 if(spoiler) s->setSpoilerParams(_spoilerLift, _spoilerDrag);
434 if(flap0) _flap0Surfs.add(s);
435 if(flap1) _flap1Surfs.add(s);
436 if(slat) _slatSurfs.add(s);
437 if(spoiler) _spoilerSurfs.add(s);
439 s->setInducedDrag(_inducedDrag);
444 void Wing::interp(float* v1, float* v2, float frac, float* out)
446 out[0] = v1[0] + frac*(v2[0]-v1[0]);
447 out[1] = v1[1] + frac*(v2[1]-v1[1]);
448 out[2] = v1[2] + frac*(v2[2]-v1[2]);
451 }; // namespace yasim