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 into an array, sort them,
239 // and remove duplicates. This gives us the boundaries of our
242 bounds[0] = _flap0Start; bounds[1] = _flap0End;
243 bounds[2] = _flap1Start; bounds[3] = _flap1End;
244 bounds[4] = _spoilerStart; bounds[5] = _spoilerEnd;
245 bounds[6] = _slatStart; bounds[7] = _slatEnd;
247 // Sort in increasing order
251 float minVal = bounds[i];
253 for(j=i+1; j<8; j++) {
254 if(bounds[j] < minVal) {
259 float tmp = bounds[i];
260 bounds[i] = minVal; bounds[minIdx] = tmp;
264 float last = bounds[0];
267 if(bounds[i] != last)
268 bounds[nbounds++] = bounds[i];
272 // Calculate a "nominal" segment length equal to an average chord,
273 // normalized to lie within 0-1 over the length of the wing.
274 float segLen = _chord * (0.5f*(_taper+1)) / _length;
276 // Generating a unit vector pointing out the left wing.
278 left[0] = -Math::tan(_sweep);
279 left[1] = Math::cos(_dihedral);
280 left[2] = Math::sin(_dihedral);
281 Math::unit3(left, left);
283 // Calculate coordinates for the root and tip of the wing
284 float root[3], tip[3];
285 Math::set3(_base, root);
286 Math::set3(left, tip);
287 Math::mul3(_length, tip, tip);
288 Math::add3(root, tip, tip);
290 // The wing's Y axis will be the "left" vector. The Z axis will
291 // be perpendicular to this and the local (!) X axis, because we
292 // want motion along the local X axis to be zero AoA (i.e. in the
293 // wing's XY plane) by definition. Then the local X coordinate is
295 float orient[9], rightOrient[9];
296 float *x = orient, *y = orient+3, *z = orient+6;
297 x[0] = 1; x[1] = 0; x[2] = 0;
299 Math::cross3(x, y, z);
301 Math::cross3(y, z, x);
304 // Derive the right side orientation matrix from this one.
306 for(i=0; i<9; i++) rightOrient[i] = orient[i];
308 // Negate all Y coordinates, this gets us a valid basis, but
309 // it's left handed! So...
310 for(i=1; i<9; i+=3) rightOrient[i] = -rightOrient[i];
312 // Change the direction of the Y axis to get back to a
313 // right-handed system.
314 for(i=3; i<6; i++) rightOrient[i] = -rightOrient[i];
317 // Now go through each boundary and make segments
318 for(i=0; i<(nbounds-1); i++) {
319 float start = bounds[i];
320 float end = bounds[i+1];
321 float mid = (start+end)/2;
323 bool flap0=0, flap1=0, slat=0, spoiler=0;
324 if(_flap0Start < mid && mid < _flap0End) flap0 = 1;
325 if(_flap1Start < mid && mid < _flap1End) flap1 = 1;
326 if(_slatStart < mid && mid < _slatEnd) slat = 1;
327 if(_spoilerStart < mid && mid < _spoilerEnd) spoiler = 1;
329 // FIXME: Should probably detect an error here if both flap0
330 // and flap1 are set. Right now flap1 overrides.
332 int nSegs = (int)Math::ceil((end-start)/segLen);
333 if (_twist != 0 && nSegs < 16) // more segments if twisted
335 float segWid = _length * (end - start)/nSegs;
338 for(j=0; j<nSegs; j++) {
339 float frac = start + (j+0.5f) * (end-start)/nSegs;
341 interp(root, tip, frac, pos);
343 float chord = _chord * (1 - (1-_taper)*frac);
345 Surface *s = newSurface(pos, orient, chord,
346 flap0, flap1, slat, spoiler);
348 SurfRec *sr = new SurfRec();
350 sr->weight = chord * segWid;
351 s->setTotalDrag(sr->weight);
352 s->setTwist(_twist * Math::sqrt(1-frac));
357 s = newSurface(pos, rightOrient, chord,
358 flap0, flap1, slat, spoiler);
361 sr->weight = chord * segWid;
362 s->setTotalDrag(sr->weight);
363 s->setTwist(_twist * Math::sqrt(frac));
369 // Last of all, re-set the incidence in case setIncidence() was
370 // called before we were compiled.
371 setIncidence(_incidence);
374 float Wing::getDragScale()
379 void Wing::setDragScale(float scale)
383 for(i=0; i<_surfs.size(); i++) {
384 SurfRec* s = (SurfRec*)_surfs.get(i);
385 s->surface->setTotalDrag(scale * s->weight);
389 void Wing::setLiftRatio(float ratio)
393 for(i=0; i<_surfs.size(); i++)
394 ((SurfRec*)_surfs.get(i))->surface->setZDrag(ratio);
397 float Wing::getLiftRatio()
402 Surface* Wing::newSurface(float* pos, float* orient, float chord,
403 bool flap0, bool flap1, bool slat, bool spoiler)
405 Surface* s = new Surface();
408 s->setOrientation(orient);
411 // Camber is expressed as a fraction of stall peak, so convert.
412 s->setBaseZDrag(_camber*_stallPeak);
414 // The "main" (i.e. normal) stall angle
415 float stallAoA = _stall - _stallWidth/4;
416 s->setStall(0, stallAoA);
417 s->setStallWidth(0, _stallWidth);
418 s->setStallPeak(0, _stallPeak);
420 // The negative AoA stall is the same if we're using an uncambered
421 // airfoil, otherwise a "little badder".
423 s->setStall(1, stallAoA * 0.8f);
424 s->setStallWidth(1, _stallWidth * 0.5f);
426 s->setStall(1, stallAoA);
427 s->setStall(1, _stallWidth);
430 // The "reverse" stalls are unmeasurable junk. Just use 13deg and
432 s->setStallPeak(1, 1);
435 s->setStall(i, 0.2267f);
436 s->setStallWidth(i, 1);
439 if(flap0) s->setFlapParams(_flap0Lift, _flap0Drag);
440 if(flap1) s->setFlapParams(_flap1Lift, _flap1Drag);
441 if(slat) s->setSlatParams(_slatAoA, _slatDrag);
442 if(spoiler) s->setSpoilerParams(_spoilerLift, _spoilerDrag);
444 if(flap0) _flap0Surfs.add(s);
445 if(flap1) _flap1Surfs.add(s);
446 if(slat) _slatSurfs.add(s);
447 if(spoiler) _spoilerSurfs.add(s);
449 s->setInducedDrag(_inducedDrag);
454 void Wing::interp(float* v1, float* v2, float frac, float* out)
456 out[0] = v1[0] + frac*(v2[0]-v1[0]);
457 out[1] = v1[1] + frac*(v2[1]-v1[1]);
458 out[2] = v1[2] + frac*(v2[2]-v1[2]);
461 }; // namespace yasim