9 _base[0] = _base[1] = _base[2] = 0;
45 for(i=0; i<_surfs.size(); i++) {
46 SurfRec* s = (SurfRec*)_surfs.get(i);
52 int Wing::numSurfaces()
57 Surface* Wing::getSurface(int n)
59 return ((SurfRec*)_surfs.get(n))->surface;
62 float Wing::getSurfaceWeight(int n)
64 return ((SurfRec*)_surfs.get(n))->weight;
67 void Wing::setMirror(bool mirror)
72 void Wing::setBase(float* base)
75 for(i=0; i<3; i++) _base[i] = base[i];
78 void Wing::setLength(float length)
83 void Wing::setChord(float chord)
88 void Wing::setTaper(float taper)
93 void Wing::setSweep(float sweep)
98 void Wing::setDihedral(float dihedral)
100 _dihedral = dihedral;
103 void Wing::setStall(float aoa)
108 void Wing::setStallWidth(float angle)
113 void Wing::setStallPeak(float fraction)
115 _stallPeak = fraction;
118 void Wing::setTwist(float angle)
123 void Wing::setCamber(float camber)
128 void Wing::setIncidence(float incidence)
130 _incidence = incidence;
132 for(i=0; i<_surfs.size(); i++)
133 ((SurfRec*)_surfs.get(i))->surface->setIncidence(incidence);
136 void Wing::setFlap0(float start, float end, float lift, float drag)
144 void Wing::setFlap1(float start, float end, float lift, float drag)
152 void Wing::setSlat(float start, float end, float aoa, float drag)
160 void Wing::setSpoiler(float start, float end, float lift, float drag)
162 _spoilerStart = start;
168 void Wing::setFlap0(float lval, float rval)
170 lval = Math::clamp(lval, -1, 1);
171 rval = Math::clamp(rval, -1, 1);
173 for(i=0; i<_flap0Surfs.size(); i++) {
174 ((Surface*)_flap0Surfs.get(i))->setFlap(lval);
175 if(_mirror) ((Surface*)_flap0Surfs.get(++i))->setFlap(rval);
179 void Wing::setFlap1(float lval, float rval)
181 lval = Math::clamp(lval, -1, 1);
182 rval = Math::clamp(rval, -1, 1);
184 for(i=0; i<_flap1Surfs.size(); i++) {
185 ((Surface*)_flap1Surfs.get(i))->setFlap(lval);
186 if(_mirror) ((Surface*)_flap1Surfs.get(++i))->setFlap(rval);
190 void Wing::setSpoiler(float lval, float rval)
192 lval = Math::clamp(lval, 0, 1);
193 rval = Math::clamp(rval, 0, 1);
195 for(i=0; i<_spoilerSurfs.size(); i++) {
196 ((Surface*)_spoilerSurfs.get(i))->setSpoiler(lval);
197 if(_mirror) ((Surface*)_spoilerSurfs.get(++i))->setSpoiler(rval);
201 void Wing::setSlat(float val)
203 val = Math::clamp(val, 0, 1);
205 for(i=0; i<_slatSurfs.size(); i++)
206 ((Surface*)_slatSurfs.get(i))->setSlat(val);
209 float Wing::getGroundEffect(float* posOut)
212 for(i=0; i<3; i++) posOut[i] = _base[i];
213 float span = _length * Math::cos(_sweep) * Math::cos(_dihedral);
214 span = 2*(span + Math::abs(_base[2]));
218 void Wing::getTip(float* tip)
220 tip[0] = -Math::tan(_sweep);
221 tip[1] = Math::cos(_dihedral);
222 tip[2] = Math::sin(_dihedral);
223 Math::unit3(tip, tip);
224 Math::mul3(_length, tip, tip);
225 Math::add3(_base, tip, tip);
228 bool Wing::isMirrored()
235 // Have we already been compiled?
236 if(_surfs.size() != 0) return;
238 // Assemble the start/end coordinates of all control surfaces
239 // and the wing itself into an array, sort them,
240 // and remove duplicates. This gives us the boundaries of our
243 bounds[0] = _flap0Start; bounds[1] = _flap0End;
244 bounds[2] = _flap1Start; bounds[3] = _flap1End;
245 bounds[4] = _spoilerStart; bounds[5] = _spoilerEnd;
246 bounds[6] = _slatStart; bounds[7] = _slatEnd;
247 //and don't forget the root and the tip of the wing itself
248 bounds[8] = 0; bounds[9] = 1;
250 // Sort in increasing order
252 for(i=0; i<10; i++) {
254 float minVal = bounds[i];
256 for(j=i+1; j<10; j++) {
257 if(bounds[j] < minVal) {
262 float tmp = bounds[i];
263 bounds[i] = minVal; bounds[minIdx] = tmp;
267 float last = bounds[0];
269 for(i=1; i<10; i++) {
270 if(bounds[i] != last)
271 bounds[nbounds++] = bounds[i];
275 // Calculate a "nominal" segment length equal to an average chord,
276 // normalized to lie within 0-1 over the length of the wing.
277 float segLen = _chord * (0.5f*(_taper+1)) / _length;
279 // Generating a unit vector pointing out the left wing.
281 left[0] = -Math::tan(_sweep);
282 left[1] = Math::cos(_dihedral);
283 left[2] = Math::sin(_dihedral);
284 Math::unit3(left, left);
286 // Calculate coordinates for the root and tip of the wing
287 float root[3], tip[3];
288 Math::set3(_base, root);
289 Math::set3(left, tip);
290 Math::mul3(_length, tip, tip);
291 Math::add3(root, tip, tip);
293 // The wing's Y axis will be the "left" vector. The Z axis will
294 // be perpendicular to this and the local (!) X axis, because we
295 // want motion along the local X axis to be zero AoA (i.e. in the
296 // wing's XY plane) by definition. Then the local X coordinate is
298 float orient[9], rightOrient[9];
299 float *x = orient, *y = orient+3, *z = orient+6;
300 x[0] = 1; x[1] = 0; x[2] = 0;
302 Math::cross3(x, y, z);
304 Math::cross3(y, z, x);
307 // Derive the right side orientation matrix from this one.
309 for(i=0; i<9; i++) rightOrient[i] = orient[i];
311 // Negate all Y coordinates, this gets us a valid basis, but
312 // it's left handed! So...
313 for(i=1; i<9; i+=3) rightOrient[i] = -rightOrient[i];
315 // Change the direction of the Y axis to get back to a
316 // right-handed system.
317 for(i=3; i<6; i++) rightOrient[i] = -rightOrient[i];
320 // Now go through each boundary and make segments
321 for(i=0; i<(nbounds-1); i++) {
322 float start = bounds[i];
323 float end = bounds[i+1];
324 float mid = (start+end)/2;
326 bool flap0=0, flap1=0, slat=0, spoiler=0;
327 if(_flap0Start < mid && mid < _flap0End) flap0 = 1;
328 if(_flap1Start < mid && mid < _flap1End) flap1 = 1;
329 if(_slatStart < mid && mid < _slatEnd) slat = 1;
330 if(_spoilerStart < mid && mid < _spoilerEnd) spoiler = 1;
332 // FIXME: Should probably detect an error here if both flap0
333 // and flap1 are set. Right now flap1 overrides.
335 int nSegs = (int)Math::ceil((end-start)/segLen);
336 if (_twist != 0 && nSegs < 8) // more segments if twisted
338 float segWid = _length * (end - start)/nSegs;
341 for(j=0; j<nSegs; j++) {
342 float frac = start + (j+0.5f) * (end-start)/nSegs;
344 interp(root, tip, frac, pos);
346 float chord = _chord * (1 - (1-_taper)*frac);
348 Surface *s = newSurface(pos, orient, chord,
349 flap0, flap1, slat, spoiler);
351 SurfRec *sr = new SurfRec();
353 sr->weight = chord * segWid;
354 s->setTotalDrag(sr->weight);
355 s->setTwist(_twist * frac);
360 s = newSurface(pos, rightOrient, chord,
361 flap0, flap1, slat, spoiler);
364 sr->weight = chord * segWid;
365 s->setTotalDrag(sr->weight);
366 s->setTwist(_twist * frac);
372 // Last of all, re-set the incidence in case setIncidence() was
373 // called before we were compiled.
374 setIncidence(_incidence);
377 float Wing::getDragScale()
382 void Wing::setDragScale(float scale)
386 for(i=0; i<_surfs.size(); i++) {
387 SurfRec* s = (SurfRec*)_surfs.get(i);
388 s->surface->setTotalDrag(scale * s->weight);
392 void Wing::setLiftRatio(float ratio)
396 for(i=0; i<_surfs.size(); i++)
397 ((SurfRec*)_surfs.get(i))->surface->setZDrag(ratio);
400 float Wing::getLiftRatio()
405 Surface* Wing::newSurface(float* pos, float* orient, float chord,
406 bool flap0, bool flap1, bool slat, bool spoiler)
408 Surface* s = new Surface();
411 s->setOrientation(orient);
414 // Camber is expressed as a fraction of stall peak, so convert.
415 s->setBaseZDrag(_camber*_stallPeak);
417 // The "main" (i.e. normal) stall angle
418 float stallAoA = _stall - _stallWidth/4;
419 s->setStall(0, stallAoA);
420 s->setStallWidth(0, _stallWidth);
421 s->setStallPeak(0, _stallPeak);
423 // The negative AoA stall is the same if we're using an uncambered
424 // airfoil, otherwise a "little badder".
426 s->setStall(1, stallAoA * 0.8f);
427 s->setStallWidth(1, _stallWidth * 0.5f);
429 s->setStall(1, stallAoA);
430 s->setStall(1, _stallWidth);
433 // The "reverse" stalls are unmeasurable junk. Just use 13deg and
435 s->setStallPeak(1, 1);
438 s->setStall(i, 0.2267f);
439 s->setStallWidth(i, 0.01);
442 if(flap0) s->setFlapParams(_flap0Lift, _flap0Drag);
443 if(flap1) s->setFlapParams(_flap1Lift, _flap1Drag);
444 if(slat) s->setSlatParams(_slatAoA, _slatDrag);
445 if(spoiler) s->setSpoilerParams(_spoilerLift, _spoilerDrag);
447 if(flap0) _flap0Surfs.add(s);
448 if(flap1) _flap1Surfs.add(s);
449 if(slat) _slatSurfs.add(s);
450 if(spoiler) _spoilerSurfs.add(s);
452 s->setInducedDrag(_inducedDrag);
457 void Wing::interp(float* v1, float* v2, float frac, float* out)
459 out[0] = v1[0] + frac*(v2[0]-v1[0]);
460 out[1] = v1[1] + frac*(v2[1]-v1[1]);
461 out[2] = v1[2] + frac*(v2[2]-v1[2]);
464 }; // namespace yasim