1 #include "Atmosphere.hpp"
2 #include "ControlMap.hpp"
6 #include "RigidBody.hpp"
8 #include "Rotorpart.hpp"
9 #include "Rotorblade.hpp"
10 #include "Thruster.hpp"
11 #include "Airplane.hpp"
16 inline float norm(float f) { return f<1 ? 1/f : f; }
17 inline float abs(float f) { return f<0 ? -f : f; }
19 // Solver threshold. How close to the solution are we trying
20 // to get? Trying too hard can result in oscillations about
21 // the correct solution, which is bad. Stick this in as a
22 // compile time constant for now, and consider making it
23 // settable per-model.
24 const float STHRESH = 1;
26 // How slowly do we change values in the solver. Too slow, and
27 // the solution converges very slowly. Too fast, and it can
29 const float SOLVE_TWEAK = 0.3226;
34 _pilotPos[0] = _pilotPos[1] = _pilotPos[2] = 0;
57 for(i=0; i<_fuselages.size(); i++)
58 delete (Fuselage*)_fuselages.get(i);
59 for(i=0; i<_tanks.size(); i++)
60 delete (Tank*)_tanks.get(i);
61 for(i=0; i<_thrusters.size(); i++)
62 delete (ThrustRec*)_thrusters.get(i);
63 for(i=0; i<_gears.size(); i++)
64 delete (GearRec*)_gears.get(i);
65 for(i=0; i<_surfs.size(); i++)
66 delete (Surface*)_surfs.get(i);
67 for(i=0; i<_contacts.size(); i++)
68 delete[] (float*)_contacts.get(i);
69 for(i=0; i<_solveWeights.size(); i++)
70 delete[] (SolveWeight*)_solveWeights.get(i);
73 void Airplane::iterate(float dt)
75 // The gear might have moved. Change their aerodynamics.
81 void Airplane::calcFuelWeights()
83 for(int i=0; i<_tanks.size(); i++) {
84 Tank* t = (Tank*)_tanks.get(i);
85 _model.getBody()->setMass(t->handle, t->fill);
89 ControlMap* Airplane::getControlMap()
94 Model* Airplane::getModel()
99 void Airplane::getPilotAccel(float* out)
101 State* s = _model.getState();
104 Glue::geodUp(s->pos, out);
105 Math::mul3(-9.8f, out, out);
107 // The regular acceleration
109 Math::mul3(-1, s->acc, tmp);
110 Math::add3(tmp, out, out);
112 // Convert to aircraft coordinates
113 Math::vmul33(s->orient, out, out);
115 // FIXME: rotational & centripetal acceleration needed
118 void Airplane::setPilotPos(float* pos)
121 for(i=0; i<3; i++) _pilotPos[i] = pos[i];
124 void Airplane::getPilotPos(float* out)
127 for(i=0; i<3; i++) out[i] = _pilotPos[i];
130 int Airplane::numGear()
132 return _gears.size();
135 Gear* Airplane::getGear(int g)
137 return ((GearRec*)_gears.get(g))->gear;
140 void Airplane::updateGearState()
142 for(int i=0; i<_gears.size(); i++) {
143 GearRec* gr = (GearRec*)_gears.get(i);
144 float ext = gr->gear->getExtension();
146 gr->surf->setXDrag(ext);
147 gr->surf->setYDrag(ext);
148 gr->surf->setZDrag(ext);
152 void Airplane::setApproach(float speed, float altitude, float aoa, float fuel)
154 _approachSpeed = speed;
155 _approachP = Atmosphere::getStdPressure(altitude);
156 _approachT = Atmosphere::getStdTemperature(altitude);
158 _approachFuel = fuel;
161 void Airplane::setCruise(float speed, float altitude, float fuel)
163 _cruiseSpeed = speed;
164 _cruiseP = Atmosphere::getStdPressure(altitude);
165 _cruiseT = Atmosphere::getStdTemperature(altitude);
171 void Airplane::setElevatorControl(int control)
173 _approachElevator.control = control;
174 _approachElevator.val = 0;
175 _approachControls.add(&_approachElevator);
178 void Airplane::addApproachControl(int control, float val)
180 Control* c = new Control();
181 c->control = control;
183 _approachControls.add(c);
186 void Airplane::addCruiseControl(int control, float val)
188 Control* c = new Control();
189 c->control = control;
191 _cruiseControls.add(c);
194 void Airplane::addSolutionWeight(bool approach, int idx, float wgt)
196 SolveWeight* w = new SolveWeight();
197 w->approach = approach;
200 _solveWeights.add(w);
203 int Airplane::numTanks()
205 return _tanks.size();
208 float Airplane::getFuel(int tank)
210 return ((Tank*)_tanks.get(tank))->fill;
213 float Airplane::setFuel(int tank, float fuel)
215 return ((Tank*)_tanks.get(tank))->fill = fuel;
218 float Airplane::getFuelDensity(int tank)
220 return ((Tank*)_tanks.get(tank))->density;
223 float Airplane::getTankCapacity(int tank)
225 return ((Tank*)_tanks.get(tank))->cap;
228 void Airplane::setWeight(float weight)
230 _emptyWeight = weight;
233 void Airplane::setWing(Wing* wing)
238 void Airplane::setTail(Wing* tail)
243 void Airplane::addVStab(Wing* vstab)
248 void Airplane::addRotor(Rotor* rotor)
253 void Airplane::addFuselage(float* front, float* back, float width,
254 float taper, float mid)
256 Fuselage* f = new Fuselage();
259 f->front[i] = front[i];
260 f->back[i] = back[i];
268 int Airplane::addTank(float* pos, float cap, float density)
270 Tank* t = new Tank();
272 for(i=0; i<3; i++) t->pos[i] = pos[i];
275 t->density = density;
276 t->handle = 0xffffffff;
277 return _tanks.add(t);
280 void Airplane::addGear(Gear* gear)
282 GearRec* g = new GearRec();
288 void Airplane::addThruster(Thruster* thruster, float mass, float* cg)
290 ThrustRec* t = new ThrustRec();
291 t->thruster = thruster;
294 for(i=0; i<3; i++) t->cg[i] = cg[i];
298 void Airplane::addBallast(float* pos, float mass)
300 _model.getBody()->addMass(mass, pos);
304 int Airplane::addWeight(float* pos, float size)
306 WeightRec* wr = new WeightRec();
307 wr->handle = _model.getBody()->addMass(0, pos);
309 wr->surf = new Surface();
310 wr->surf->setPosition(pos);
311 wr->surf->setTotalDrag(size*size);
312 _model.addSurface(wr->surf);
313 _surfs.add(wr->surf);
315 return _weights.add(wr);
318 void Airplane::setWeight(int handle, float mass)
320 WeightRec* wr = (WeightRec*)_weights.get(handle);
322 _model.getBody()->setMass(wr->handle, mass);
324 // Kill the aerodynamic drag if the mass is exactly zero. This is
325 // how we simulate droppable stores.
327 wr->surf->setXDrag(0);
328 wr->surf->setYDrag(0);
329 wr->surf->setZDrag(0);
331 wr->surf->setXDrag(1);
332 wr->surf->setYDrag(1);
333 wr->surf->setZDrag(1);
337 void Airplane::setFuelFraction(float frac)
340 for(i=0; i<_tanks.size(); i++) {
341 Tank* t = (Tank*)_tanks.get(i);
342 t->fill = frac * t->cap;
343 _model.getBody()->setMass(t->handle, t->cap * frac);
347 float Airplane::getDragCoefficient()
352 float Airplane::getLiftRatio()
357 float Airplane::getCruiseAoA()
362 float Airplane::getTailIncidence()
364 return _tailIncidence;
367 char* Airplane::getFailureMsg()
372 int Airplane::getSolutionIterations()
374 return _solutionIterations;
377 void Airplane::setupState(float aoa, float speed, State* s)
379 float cosAoA = Math::cos(aoa);
380 float sinAoA = Math::sin(aoa);
381 s->orient[0] = cosAoA; s->orient[1] = 0; s->orient[2] = sinAoA;
382 s->orient[3] = 0; s->orient[4] = 1; s->orient[5] = 0;
383 s->orient[6] = -sinAoA; s->orient[7] = 0; s->orient[8] = cosAoA;
385 s->v[0] = speed; s->v[1] = 0; s->v[2] = 0;
389 s->pos[i] = s->rot[i] = s->acc[i] = s->racc[i] = 0;
391 // Put us 1m above the origin, or else the gravity computation in
396 void Airplane::addContactPoint(float* pos)
398 float* cp = new float[3];
405 float Airplane::compileWing(Wing* w)
407 // The tip of the wing is a contact point
410 addContactPoint(tip);
411 if(w->isMirrored()) {
413 addContactPoint(tip);
416 // Make sure it's initialized. The surfaces will pop out with
417 // total drag coefficients equal to their areas, which is what we
423 for(i=0; i<w->numSurfaces(); i++) {
424 Surface* s = (Surface*)w->getSurface(i);
426 float td = s->getTotalDrag();
429 _model.addSurface(s);
431 float mass = w->getSurfaceWeight(i);
432 mass = mass * Math::sqrt(mass);
435 _model.getBody()->addMass(mass, pos);
441 float Airplane::compileRotor(Rotor* r)
443 // Todo: add rotor to model!!!
444 // Todo: calc and add mass!!!
450 for(i=0; i<r->numRotorparts(); i++) {
451 Rotorpart* s = (Rotorpart*)r->getRotorpart(i);
453 _model.addRotorpart(s);
455 float mass = s->getWeight();
456 mass = mass * Math::sqrt(mass);
459 _model.getBody()->addMass(mass, pos);
463 for(i=0; i<r->numRotorblades(); i++) {
464 Rotorblade* b = (Rotorblade*)r->getRotorblade(i);
466 _model.addRotorblade(b);
468 float mass = b->getWeight();
469 mass = mass * Math::sqrt(mass);
472 _model.getBody()->addMass(mass, pos);
478 float Airplane::compileFuselage(Fuselage* f)
480 // The front and back are contact points
481 addContactPoint(f->front);
482 addContactPoint(f->back);
486 Math::sub3(f->front, f->back, fwd);
487 float len = Math::mag3(fwd);
488 float wid = f->width;
489 int segs = (int)Math::ceil(len/wid);
490 float segWgt = len*wid/segs;
492 for(j=0; j<segs; j++) {
493 float frac = (j+0.5f) / segs;
497 scale = f->taper+(1-f->taper) * (frac / f->mid);
499 scale = f->taper+(1-f->taper) * (frac - f->mid) / (1 - f->mid);
503 Math::mul3(frac, fwd, pos);
504 Math::add3(f->back, pos, pos);
506 // _Mass_ weighting goes as surface area^(3/2)
507 float mass = scale*segWgt * Math::sqrt(scale*segWgt);
508 _model.getBody()->addMass(mass, pos);
511 // Make a Surface too
512 Surface* s = new Surface();
514 float sideDrag = len/wid;
515 s->setYDrag(sideDrag);
516 s->setZDrag(sideDrag);
517 s->setTotalDrag(scale*segWgt);
519 // FIXME: fails for fuselages aligned along the Y axis
521 float *x=o, *y=o+3, *z=o+6; // nicknames for the axes
523 y[0] = 0; y[1] = 1; y[2] = 0;
524 Math::cross3(x, y, z);
526 Math::cross3(z, x, y);
527 s->setOrientation(o);
529 _model.addSurface(s);
535 // FIXME: should probably add a mass for the gear, too
536 void Airplane::compileGear(GearRec* gr)
540 // Make a Surface object for the aerodynamic behavior
541 Surface* s = new Surface();
544 // Put the surface at the half-way point on the gear strut, give
545 // it a drag coefficient equal to a square of the same dimension
546 // (gear are really draggy) and make it symmetric. Assume that
547 // the "length" of the gear is 3x the compression distance
548 float pos[3], cmp[3];
549 g->getCompression(cmp);
550 float length = 3 * Math::mag3(cmp);
552 Math::mul3(0.5, cmp, cmp);
553 Math::add3(pos, cmp, pos);
556 s->setTotalDrag(length*length);
559 _model.addSurface(s);
563 void Airplane::compileContactPoints()
565 // Figure it will compress by 20cm
568 comp[0] = 0; comp[1] = 0; comp[2] = DIST;
570 // Give it a spring constant such that at full compression it will
571 // hold up 10 times the planes mass. That's about right. Yeah.
572 float mass = _model.getBody()->getTotalMass();
573 float spring = (1/DIST) * 9.8f * 10.0f * mass;
574 float damp = 2 * Math::sqrt(spring * mass);
577 for(i=0; i<_contacts.size(); i++) {
578 float *cp = (float*)_contacts.get(i);
580 Gear* g = new Gear();
583 g->setCompression(comp);
584 g->setSpring(spring);
589 g->setStaticFriction(0.6f);
590 g->setDynamicFriction(0.5f);
596 void Airplane::compile()
599 ground[0] = 0; ground[1] = 0; ground[2] = 1;
600 _model.setGroundPlane(ground, -100000);
602 RigidBody* body = _model.getBody();
603 int firstMass = body->numMasses();
605 // Generate the point masses for the plane. Just use unitless
606 // numbers for a first pass, then go back through and rescale to
607 // make the weight right.
612 aeroWgt += compileWing(_wing);
614 aeroWgt += compileWing(_tail);
616 for(i=0; i<_vstabs.size(); i++)
617 aeroWgt += compileWing((Wing*)_vstabs.get(i));
618 for(i=0; i<_rotors.size(); i++)
619 aeroWgt += compileRotor((Rotor*)_rotors.get(i));
622 for(i=0; i<_fuselages.size(); i++)
623 aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i));
625 // Count up the absolute weight we have
626 float nonAeroWgt = _ballast;
627 for(i=0; i<_thrusters.size(); i++)
628 nonAeroWgt += ((ThrustRec*)_thrusters.get(i))->mass;
630 // Rescale to the specified empty weight
631 float wscale = (_emptyWeight-nonAeroWgt)/aeroWgt;
632 for(i=firstMass; i<body->numMasses(); i++)
633 body->setMass(i, body->getMass(i)*wscale);
635 // Add the thruster masses
636 for(i=0; i<_thrusters.size(); i++) {
637 ThrustRec* t = (ThrustRec*)_thrusters.get(i);
638 body->addMass(t->mass, t->cg);
641 // Add the tanks, empty for now.
643 for(i=0; i<_tanks.size(); i++) {
644 Tank* t = (Tank*)_tanks.get(i);
645 t->handle = body->addMass(0, t->pos);
648 _cruiseWeight = _emptyWeight + totalFuel*0.5f;
649 _approachWeight = _emptyWeight + totalFuel*0.2f;
653 // Add surfaces for the landing gear.
654 for(i=0; i<_gears.size(); i++)
655 compileGear((GearRec*)_gears.get(i));
657 // The Thruster objects
658 for(i=0; i<_thrusters.size(); i++) {
659 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
660 tr->handle = _model.addThruster(tr->thruster);
667 gespan = _wing->getGroundEffect(gepos);
668 _model.setGroundEffect(gepos, gespan, 0.15f);
671 if(_wing && _tail) solve();
672 else solveHelicopter();
674 // Do this after solveGear, because it creates "gear" objects that
675 // we don't want to affect.
676 compileContactPoints();
679 void Airplane::solveGear()
682 _model.getBody()->getCG(cg);
684 // Calculate spring constant weightings for the gear. Weight by
685 // the inverse of the distance to the c.g. in the XY plane, which
686 // should be correct for most gear arrangements. Add 50cm of
687 // "buffer" to keep things from blowing up with aircraft with a
688 // single gear very near the c.g. (AV-8, for example).
691 for(i=0; i<_gears.size(); i++) {
692 GearRec* gr = (GearRec*)_gears.get(i);
695 Math::sub3(cg, pos, pos);
696 gr->wgt = 1.0f/(0.5f+Math::sqrt(pos[0]*pos[0] + pos[1]*pos[1]));
700 // Renormalize so they sum to 1
701 for(i=0; i<_gears.size(); i++)
702 ((GearRec*)_gears.get(i))->wgt /= total;
704 // The force at max compression should be sufficient to stop a
705 // plane moving downwards at 2x the approach descent rate. Assume
706 // a 3 degree approach.
707 float descentRate = 2.0f*_approachSpeed/19.1f;
709 // Spread the kinetic energy according to the gear weights. This
710 // will results in an equal compression fraction (not distance) of
712 float energy = 0.5f*_approachWeight*descentRate*descentRate;
714 for(i=0; i<_gears.size(); i++) {
715 GearRec* gr = (GearRec*)_gears.get(i);
716 float e = energy * gr->wgt;
718 gr->gear->getCompression(comp);
719 float len = Math::mag3(comp);
721 // Energy in a spring: e = 0.5 * k * len^2
722 float k = 2 * e / (len*len);
724 gr->gear->setSpring(k * gr->gear->getSpring());
726 // Critically damped (too damped, too!)
727 gr->gear->setDamping(2*Math::sqrt(k*_approachWeight*gr->wgt)
728 * gr->gear->getDamping());
730 // These are pretty generic
731 gr->gear->setStaticFriction(0.8f);
732 gr->gear->setDynamicFriction(0.7f);
736 void Airplane::initEngines()
738 for(int i=0; i<_thrusters.size(); i++) {
739 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
740 tr->thruster->init();
744 void Airplane::stabilizeThrust()
747 for(i=0; i<_thrusters.size(); i++)
748 _model.getThruster(i)->stabilize();
751 void Airplane::setupWeights(bool isApproach)
754 for(i=0; i<_weights.size(); i++)
756 for(i=0; i<_solveWeights.size(); i++) {
757 SolveWeight* w = (SolveWeight*)_solveWeights.get(i);
758 if(w->approach == isApproach)
759 setWeight(w->idx, w->wgt);
763 void Airplane::runCruise()
765 setupState(_cruiseAoA, _cruiseSpeed, &_cruiseState);
766 _model.setState(&_cruiseState);
767 _model.setAir(_cruiseP, _cruiseT,
768 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
770 // The control configuration
773 for(i=0; i<_cruiseControls.size(); i++) {
774 Control* c = (Control*)_cruiseControls.get(i);
775 _controls.setInput(c->control, c->val);
777 _controls.applyControls(1000000); // Huge dt value
781 Math::mul3(-1, _cruiseState.v, wind);
782 Math::vmul33(_cruiseState.orient, wind, wind);
784 setFuelFraction(_cruiseFuel);
787 // Set up the thruster parameters and iterate until the thrust
789 for(i=0; i<_thrusters.size(); i++) {
790 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
792 t->setAir(_cruiseP, _cruiseT,
793 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
799 // Precompute thrust in the model, and calculate aerodynamic forces
800 _model.getBody()->recalc();
801 _model.getBody()->reset();
802 _model.initIteration();
803 _model.calcForces(&_cruiseState);
806 void Airplane::runApproach()
808 setupState(_approachAoA, _approachSpeed, &_approachState);
809 _model.setState(&_approachState);
810 _model.setAir(_approachP, _approachT,
811 Atmosphere::calcStdDensity(_approachP, _approachT));
813 // The control configuration
816 for(i=0; i<_approachControls.size(); i++) {
817 Control* c = (Control*)_approachControls.get(i);
818 _controls.setInput(c->control, c->val);
820 _controls.applyControls(1000000);
824 Math::mul3(-1, _approachState.v, wind);
825 Math::vmul33(_approachState.orient, wind, wind);
827 setFuelFraction(_approachFuel);
831 // Run the thrusters until they get to a stable setting. FIXME:
832 // this is lots of wasted work.
833 for(i=0; i<_thrusters.size(); i++) {
834 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
836 t->setAir(_approachP, _approachT,
837 Atmosphere::calcStdDensity(_approachP, _approachT));
843 // Precompute thrust in the model, and calculate aerodynamic forces
844 _model.getBody()->recalc();
845 _model.getBody()->reset();
846 _model.initIteration();
847 _model.calcForces(&_approachState);
850 void Airplane::applyDragFactor(float factor)
852 float applied = Math::pow(factor, SOLVE_TWEAK);
853 _dragFactor *= applied;
855 _wing->setDragScale(_wing->getDragScale() * applied);
857 _tail->setDragScale(_tail->getDragScale() * applied);
859 for(i=0; i<_vstabs.size(); i++) {
860 Wing* w = (Wing*)_vstabs.get(i);
861 w->setDragScale(w->getDragScale() * applied);
863 for(i=0; i<_surfs.size(); i++) {
864 Surface* s = (Surface*)_surfs.get(i);
865 s->setTotalDrag(s->getTotalDrag() * applied);
869 void Airplane::applyLiftRatio(float factor)
871 float applied = Math::pow(factor, SOLVE_TWEAK);
872 _liftRatio *= applied;
874 _wing->setLiftRatio(_wing->getLiftRatio() * applied);
876 _tail->setLiftRatio(_tail->getLiftRatio() * applied);
878 for(i=0; i<_vstabs.size(); i++) {
879 Wing* w = (Wing*)_vstabs.get(i);
880 w->setLiftRatio(w->getLiftRatio() * applied);
884 float Airplane::clamp(float val, float min, float max)
886 if(val < min) return min;
887 if(val > max) return max;
891 float Airplane::normFactor(float f)
898 void Airplane::solve()
900 static const float ARCMIN = 0.0002909f;
903 _solutionIterations = 0;
907 if(_solutionIterations++ > 10000) {
908 _failureMsg = "Solution failed to converge after 10000 iterations";
912 // Run an iteration at cruise, and extract the needed numbers:
915 _model.getThrust(tmp);
916 float thrust = tmp[0];
918 _model.getBody()->getAccel(tmp);
919 Math::tmul33(_cruiseState.orient, tmp, tmp);
920 float xforce = _cruiseWeight * tmp[0];
921 float clift0 = _cruiseWeight * tmp[2];
923 _model.getBody()->getAngularAccel(tmp);
924 Math::tmul33(_cruiseState.orient, tmp, tmp);
925 float pitch0 = tmp[1];
927 // Run an approach iteration, and do likewise
930 _model.getBody()->getAngularAccel(tmp);
931 Math::tmul33(_approachState.orient, tmp, tmp);
932 double apitch0 = tmp[1];
934 _model.getBody()->getAccel(tmp);
935 Math::tmul33(_approachState.orient, tmp, tmp);
936 float alift = _approachWeight * tmp[2];
938 // Modify the cruise AoA a bit to get a derivative
939 _cruiseAoA += ARCMIN;
941 _cruiseAoA -= ARCMIN;
943 _model.getBody()->getAccel(tmp);
944 Math::tmul33(_cruiseState.orient, tmp, tmp);
945 float clift1 = _cruiseWeight * tmp[2];
947 // Do the same with the tail incidence
948 _tail->setIncidence(_tailIncidence + ARCMIN);
950 _tail->setIncidence(_tailIncidence);
952 _model.getBody()->getAngularAccel(tmp);
953 Math::tmul33(_cruiseState.orient, tmp, tmp);
954 float pitch1 = tmp[1];
957 float awgt = 9.8f * _approachWeight;
959 float dragFactor = thrust / (thrust-xforce);
960 float liftFactor = awgt / (awgt+alift);
961 float aoaDelta = -clift0 * (ARCMIN/(clift1-clift0));
962 float tailDelta = -pitch0 * (ARCMIN/(pitch1-pitch0));
965 if(dragFactor <= 0 || liftFactor <= 0)
968 // And the elevator control in the approach. This works just
969 // like the tail incidence computation (it's solving for the
970 // same thing -- pitching moment -- by diddling a different
972 const float ELEVDIDDLE = 0.001f;
973 _approachElevator.val += ELEVDIDDLE;
975 _approachElevator.val -= ELEVDIDDLE;
977 _model.getBody()->getAngularAccel(tmp);
978 Math::tmul33(_approachState.orient, tmp, tmp);
979 double apitch1 = tmp[1];
980 float elevDelta = -apitch0 * (ELEVDIDDLE/(apitch1-apitch0));
982 // Now apply the values we just computed. Note that the
983 // "minor" variables are deferred until we get the lift/drag
984 // numbers in the right ballpark.
986 applyDragFactor(dragFactor);
987 applyLiftRatio(liftFactor);
989 // DON'T do the following until the above are sane
990 if(normFactor(dragFactor) > STHRESH*1.0001
991 || normFactor(liftFactor) > STHRESH*1.0001)
996 // OK, now we can adjust the minor variables:
997 _cruiseAoA += SOLVE_TWEAK*aoaDelta;
998 _tailIncidence += SOLVE_TWEAK*tailDelta;
1000 _cruiseAoA = clamp(_cruiseAoA, -0.175f, 0.175f);
1001 _tailIncidence = clamp(_tailIncidence, -0.175f, 0.175f);
1003 if(abs(xforce/_cruiseWeight) < STHRESH*0.0001 &&
1004 abs(alift/_approachWeight) < STHRESH*0.0001 &&
1005 abs(aoaDelta) < STHRESH*.000017 &&
1006 abs(tailDelta) < STHRESH*.000017)
1008 // If this finaly value is OK, then we're all done
1009 if(abs(elevDelta) < STHRESH*0.0001)
1012 // Otherwise, adjust and do the next iteration
1013 _approachElevator.val += SOLVE_TWEAK * elevDelta;
1014 if(abs(_approachElevator.val) > 1) {
1015 _failureMsg = "Insufficient elevator to trim for approach";
1021 if(_dragFactor < 1e-06 || _dragFactor > 1e6) {
1022 _failureMsg = "Drag factor beyond reasonable bounds.";
1024 } else if(_liftRatio < 1e-04 || _liftRatio > 1e4) {
1025 _failureMsg = "Lift ratio beyond reasonable bounds.";
1027 } else if(Math::abs(_cruiseAoA) >= .17453293) {
1028 _failureMsg = "Cruise AoA > 10 degrees";
1030 } else if(Math::abs(_tailIncidence) >= .17453293) {
1031 _failureMsg = "Tail incidence > 10 degrees";
1036 void Airplane::solveHelicopter()
1038 _solutionIterations = 0;
1041 applyDragFactor(Math::pow(15.7/1000, 1/SOLVE_TWEAK));
1042 applyLiftRatio(Math::pow(104, 1/SOLVE_TWEAK));
1043 setupState(0,0, &_cruiseState);
1044 _model.setState(&_cruiseState);
1046 _model.getBody()->reset();
1049 }; // namespace yasim