9 _base[0] = _base[1] = _base[2] = 0;
43 for(i=0; i<_surfs.size(); i++) {
44 SurfRec* s = (SurfRec*)_surfs.get(i);
50 int Wing::numSurfaces()
55 Surface* Wing::getSurface(int n)
57 return ((SurfRec*)_surfs.get(n))->surface;
60 float Wing::getSurfaceWeight(int n)
62 return ((SurfRec*)_surfs.get(n))->weight;
65 void Wing::setMirror(bool mirror)
70 void Wing::setBase(float* base)
73 for(i=0; i<3; i++) _base[i] = base[i];
76 void Wing::setLength(float length)
81 void Wing::setChord(float chord)
86 void Wing::setTaper(float taper)
91 void Wing::setSweep(float sweep)
96 void Wing::setDihedral(float dihedral)
101 void Wing::setStall(float aoa)
106 void Wing::setStallWidth(float angle)
111 void Wing::setStallPeak(float fraction)
113 _stallPeak = fraction;
116 void Wing::setCamber(float camber)
121 void Wing::setIncidence(float incidence)
123 _incidence = incidence;
125 for(i=0; i<_surfs.size(); i++)
126 ((SurfRec*)_surfs.get(i))->surface->setIncidence(incidence);
129 void Wing::setFlap0(float start, float end, float lift, float drag)
137 void Wing::setFlap1(float start, float end, float lift, float drag)
145 void Wing::setSlat(float start, float end, float aoa, float drag)
153 void Wing::setSpoiler(float start, float end, float lift, float drag)
155 _spoilerStart = start;
161 void Wing::setFlap0(float lval, float rval)
163 lval = Math::clamp(lval, -1, 1);
164 rval = Math::clamp(rval, -1, 1);
166 for(i=0; i<_flap0Surfs.size(); i++) {
167 ((Surface*)_flap0Surfs.get(i))->setFlap(lval);
168 if(_mirror) ((Surface*)_flap0Surfs.get(++i))->setFlap(rval);
172 void Wing::setFlap1(float lval, float rval)
174 lval = Math::clamp(lval, -1, 1);
175 rval = Math::clamp(rval, -1, 1);
177 for(i=0; i<_flap1Surfs.size(); i++) {
178 ((Surface*)_flap1Surfs.get(i))->setFlap(lval);
179 if(_mirror) ((Surface*)_flap1Surfs.get(++i))->setFlap(rval);
183 void Wing::setSpoiler(float lval, float rval)
185 lval = Math::clamp(lval, 0, 1);
186 rval = Math::clamp(rval, 0, 1);
188 for(i=0; i<_spoilerSurfs.size(); i++) {
189 ((Surface*)_spoilerSurfs.get(i))->setSpoiler(lval);
190 if(_mirror) ((Surface*)_spoilerSurfs.get(++i))->setSpoiler(rval);
194 void Wing::setSlat(float val)
196 val = Math::clamp(val, 0, 1);
198 for(i=0; i<_slatSurfs.size(); i++)
199 ((Surface*)_slatSurfs.get(i))->setSlat(val);
202 float Wing::getGroundEffect(float* posOut)
205 for(i=0; i<3; i++) posOut[i] = _base[i];
206 float span = _length * Math::cos(_sweep) * Math::cos(_dihedral);
207 span = 2*(span + Math::abs(_base[2]));
211 void Wing::getTip(float* tip)
213 tip[0] = -Math::tan(_sweep);
214 tip[1] = Math::cos(_dihedral);
215 tip[2] = Math::sin(_dihedral);
216 Math::unit3(tip, tip);
217 Math::mul3(_length, tip, tip);
218 Math::add3(_base, tip, tip);
221 bool Wing::isMirrored()
228 // Have we already been compiled?
229 if(_surfs.size() != 0) return;
231 // Assemble the start/end coordinates into an array, sort them,
232 // and remove duplicates. This gives us the boundaries of our
235 bounds[0] = _flap0Start; bounds[1] = _flap0End;
236 bounds[2] = _flap1Start; bounds[3] = _flap1End;
237 bounds[4] = _spoilerStart; bounds[5] = _spoilerEnd;
238 bounds[6] = _slatStart; bounds[7] = _slatEnd;
240 // Sort in increasing order
244 float minVal = bounds[i];
246 for(j=i+1; j<8; j++) {
247 if(bounds[j] < minVal) {
252 float tmp = bounds[i];
253 bounds[i] = minVal; bounds[minIdx] = tmp;
257 float last = bounds[0];
260 if(bounds[i] != last)
261 bounds[nbounds++] = bounds[i];
265 // Calculate a "nominal" segment length equal to an average chord,
266 // normalized to lie within 0-1 over the length of the wing.
267 float segLen = _chord * (0.5*(_taper+1)) / _length;
269 // Generating a unit vector pointing out the left wing.
271 left[0] = -Math::tan(_sweep);
272 left[1] = Math::cos(_dihedral);
273 left[2] = Math::sin(_dihedral);
274 Math::unit3(left, left);
276 // Calculate coordinates for the root and tip of the wing
277 float root[3], tip[3];
278 Math::set3(_base, root);
279 Math::set3(left, tip);
280 Math::mul3(_length, tip, tip);
281 Math::add3(root, tip, tip);
283 // The wing's Y axis will be the "left" vector. The Z axis will
284 // be perpendicular to this and the local (!) X axis, because we
285 // want motion along the local X axis to be zero AoA (i.e. in the
286 // wing's XY plane) by definition. Then the local X coordinate is
288 float orient[9], rightOrient[9];
289 float *x = orient, *y = orient+3, *z = orient+6;
290 x[0] = 1; x[1] = 0; x[2] = 0;
292 Math::cross3(x, y, z);
294 Math::cross3(y, z, x);
297 // Derive the right side orientation matrix from this one.
299 for(i=0; i<9; i++) rightOrient[i] = orient[i];
301 // Negate all Y coordinates, this gets us a valid basis, but
302 // it's left handed! So...
303 for(i=1; i<9; i+=3) rightOrient[i] = -rightOrient[i];
305 // Change the direction of the Y axis to get back to a
306 // right-handed system.
307 for(i=3; i<6; i++) rightOrient[i] = -rightOrient[i];
310 // Now go through each boundary and make segments
311 for(i=0; i<(nbounds-1); i++) {
312 float start = bounds[i];
313 float end = bounds[i+1];
314 float mid = (start+end)/2;
316 bool flap0=0, flap1=0, slat=0, spoiler=0;
317 if(_flap0Start < mid && mid < _flap0End) flap0 = 1;
318 if(_flap1Start < mid && mid < _flap1End) flap1 = 1;
319 if(_slatStart < mid && mid < _slatEnd) slat = 1;
320 if(_spoilerStart < mid && mid < _spoilerEnd) spoiler = 1;
322 // FIXME: Should probably detect an error here if both flap0
323 // and flap1 are set. Right now flap1 overrides.
325 int nSegs = (int)Math::ceil((end-start)/segLen);
326 float segWid = _length * (end - start)/nSegs;
329 for(j=0; j<nSegs; j++) {
330 float frac = start + (j+0.5) * (end-start)/nSegs;
332 interp(root, tip, frac, pos);
334 float chord = _chord * (1 - (1-_taper)*frac);
336 Surface *s = newSurface(pos, orient, chord,
337 flap0, flap1, slat, spoiler);
339 SurfRec *sr = new SurfRec();
341 sr->weight = chord * segWid;
342 s->setTotalDrag(sr->weight);
347 s = newSurface(pos, rightOrient, chord,
348 flap0, flap1, slat, spoiler);
351 sr->weight = chord * segWid;
352 s->setTotalDrag(sr->weight);
359 float Wing::getDragScale()
364 void Wing::setDragScale(float scale)
368 for(i=0; i<_surfs.size(); i++) {
369 SurfRec* s = (SurfRec*)_surfs.get(i);
370 s->surface->setTotalDrag(scale * s->weight);
374 void Wing::setLiftRatio(float ratio)
378 for(i=0; i<_surfs.size(); i++)
379 ((SurfRec*)_surfs.get(i))->surface->setZDrag(ratio);
382 float Wing::getLiftRatio()
387 Surface* Wing::newSurface(float* pos, float* orient, float chord,
388 bool flap0, bool flap1, bool slat, bool spoiler)
390 Surface* s = new Surface();
393 s->setOrientation(orient);
396 // Camber is expressed as a fraction of stall peak, so convert.
397 s->setBaseZDrag(_camber*_stallPeak);
399 // The "main" (i.e. normal) stall angle
400 float stallAoA = _stall - _stallWidth/4;
401 s->setStall(0, stallAoA);
402 s->setStallWidth(0, _stallWidth);
403 s->setStallPeak(0, _stallPeak);
405 // The negative AoA stall is the same if we're using an uncambered
406 // airfoil, otherwise a "little badder".
408 s->setStall(1, stallAoA * 0.8);
409 s->setStallWidth(1, _stallWidth * 0.5);
411 s->setStall(1, stallAoA);
412 s->setStall(1, _stallWidth);
415 // The "reverse" stalls are unmeasurable junk. Just use 13deg and
417 s->setStallPeak(1, 1);
420 s->setStall(i, 0.2267);
421 s->setStallWidth(i, 1);
424 if(flap0) s->setFlapParams(_flap0Lift, _flap0Drag);
425 if(flap1) s->setFlapParams(_flap1Lift, _flap1Drag);
426 if(slat) s->setSlatParams(_slatAoA, _slatDrag);
427 if(spoiler) s->setSpoilerParams(_spoilerLift, _spoilerDrag);
429 if(flap0) _flap0Surfs.add(s);
430 if(flap1) _flap1Surfs.add(s);
431 if(slat) _slatSurfs.add(s);
432 if(spoiler) _spoilerSurfs.add(s);
437 void Wing::interp(float* v1, float* v2, float frac, float* out)
439 out[0] = v1[0] + frac*(v2[0]-v1[0]);
440 out[1] = v1[1] + frac*(v2[1]-v1[1]);
441 out[2] = v1[2] + frac*(v2[2]-v1[2]);
444 }; // namespace yasim