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);
71 void Airplane::iterate(float dt)
73 // The gear might have moved. Change their aerodynamics.
79 void Airplane::consumeFuel(float dt)
81 // This is a really simple implementation that assumes all engines
82 // draw equally from all tanks in proportion to the amount of fuel
83 // stored there. Needs to be fixed, but that has to wait for a
84 // decision as to what the property interface will look like.
86 float fuelFlow = 0, totalFuel = 0.00001; // <-- overflow protection
87 for(i=0; i<_thrusters.size(); i++)
88 fuelFlow += ((ThrustRec*)_thrusters.get(i))->thruster->getFuelFlow();
89 for(i=0; i<_tanks.size(); i++)
90 totalFuel += ((Tank*)_tanks.get(i))->fill;
91 for(i=0; i<_tanks.size(); i++) {
92 Tank* t = (Tank*)_tanks.get(i);
93 t->fill -= dt * fuelFlow * (t->fill/totalFuel);
100 for(int i=0; i<_thrusters.size(); i++)
101 ((ThrustRec*)_thrusters.get(i))->thruster->setFuelState(false);
103 // Set the tank masses on the RigidBody
104 for(i=0; i<_tanks.size(); i++) {
105 Tank* t = (Tank*)_tanks.get(i);
106 _model.getBody()->setMass(t->handle, t->fill);
110 ControlMap* Airplane::getControlMap()
115 Model* Airplane::getModel()
120 void Airplane::getPilotAccel(float* out)
122 State* s = _model.getState();
125 Glue::geodUp(s->pos, out);
126 Math::mul3(-9.8f, out, out);
128 // The regular acceleration
130 Math::mul3(-1, s->acc, tmp);
131 Math::add3(tmp, out, out);
133 // Convert to aircraft coordinates
134 Math::vmul33(s->orient, out, out);
136 // FIXME: rotational & centripetal acceleration needed
139 void Airplane::setPilotPos(float* pos)
142 for(i=0; i<3; i++) _pilotPos[i] = pos[i];
145 void Airplane::getPilotPos(float* out)
148 for(i=0; i<3; i++) out[i] = _pilotPos[i];
151 int Airplane::numGear()
153 return _gears.size();
156 Gear* Airplane::getGear(int g)
158 return ((GearRec*)_gears.get(g))->gear;
161 void Airplane::updateGearState()
163 for(int i=0; i<_gears.size(); i++) {
164 GearRec* gr = (GearRec*)_gears.get(i);
165 float ext = gr->gear->getExtension();
167 gr->surf->setXDrag(ext);
168 gr->surf->setYDrag(ext);
169 gr->surf->setZDrag(ext);
173 void Airplane::setApproach(float speed, float altitude, float aoa, float fuel)
175 _approachSpeed = speed;
176 _approachP = Atmosphere::getStdPressure(altitude);
177 _approachT = Atmosphere::getStdTemperature(altitude);
179 _approachFuel = fuel;
182 void Airplane::setCruise(float speed, float altitude, float fuel)
184 _cruiseSpeed = speed;
185 _cruiseP = Atmosphere::getStdPressure(altitude);
186 _cruiseT = Atmosphere::getStdTemperature(altitude);
192 void Airplane::setElevatorControl(int control)
194 _approachElevator.control = control;
195 _approachElevator.val = 0;
196 _approachControls.add(&_approachElevator);
199 void Airplane::addApproachControl(int control, float val)
201 Control* c = new Control();
202 c->control = control;
204 _approachControls.add(c);
207 void Airplane::addCruiseControl(int control, float val)
209 Control* c = new Control();
210 c->control = control;
212 _cruiseControls.add(c);
215 int Airplane::numTanks()
217 return _tanks.size();
220 float Airplane::getFuel(int tank)
222 return ((Tank*)_tanks.get(tank))->fill;
225 float Airplane::getFuelDensity(int tank)
227 return ((Tank*)_tanks.get(tank))->density;
230 float Airplane::getTankCapacity(int tank)
232 return ((Tank*)_tanks.get(tank))->cap;
235 void Airplane::setWeight(float weight)
237 _emptyWeight = weight;
240 void Airplane::setWing(Wing* wing)
245 void Airplane::setTail(Wing* tail)
250 void Airplane::addVStab(Wing* vstab)
255 void Airplane::addRotor(Rotor* rotor)
260 void Airplane::addFuselage(float* front, float* back, float width,
261 float taper, float mid)
263 Fuselage* f = new Fuselage();
266 f->front[i] = front[i];
267 f->back[i] = back[i];
275 int Airplane::addTank(float* pos, float cap, float density)
277 Tank* t = new Tank();
279 for(i=0; i<3; i++) t->pos[i] = pos[i];
282 t->density = density;
283 t->handle = 0xffffffff;
284 return _tanks.add(t);
287 void Airplane::addGear(Gear* gear)
289 GearRec* g = new GearRec();
295 void Airplane::addThruster(Thruster* thruster, float mass, float* cg)
297 ThrustRec* t = new ThrustRec();
298 t->thruster = thruster;
301 for(i=0; i<3; i++) t->cg[i] = cg[i];
305 void Airplane::addBallast(float* pos, float mass)
307 _model.getBody()->addMass(mass, pos);
311 int Airplane::addWeight(float* pos, float size)
313 WeightRec* wr = new WeightRec();
314 wr->handle = _model.getBody()->addMass(0, pos);
316 wr->surf = new Surface();
317 wr->surf->setPosition(pos);
318 wr->surf->setTotalDrag(size*size);
319 _model.addSurface(wr->surf);
320 _surfs.add(wr->surf);
322 return _weights.add(wr);
325 void Airplane::setWeight(int handle, float mass)
327 WeightRec* wr = (WeightRec*)_weights.get(handle);
329 _model.getBody()->setMass(wr->handle, mass);
331 // Kill the aerodynamic drag if the mass is exactly zero. This is
332 // how we simulate droppable stores.
334 wr->surf->setXDrag(0);
335 wr->surf->setYDrag(0);
336 wr->surf->setZDrag(0);
338 wr->surf->setXDrag(1);
339 wr->surf->setYDrag(1);
340 wr->surf->setZDrag(1);
344 void Airplane::setFuelFraction(float frac)
347 for(i=0; i<_tanks.size(); i++) {
348 Tank* t = (Tank*)_tanks.get(i);
349 t->fill = frac * t->cap;
350 _model.getBody()->setMass(t->handle, t->cap * frac);
354 float Airplane::getDragCoefficient()
359 float Airplane::getLiftRatio()
364 float Airplane::getCruiseAoA()
369 float Airplane::getTailIncidence()
371 return _tailIncidence;
374 char* Airplane::getFailureMsg()
379 int Airplane::getSolutionIterations()
381 return _solutionIterations;
384 void Airplane::setupState(float aoa, float speed, State* s)
386 float cosAoA = Math::cos(aoa);
387 float sinAoA = Math::sin(aoa);
388 s->orient[0] = cosAoA; s->orient[1] = 0; s->orient[2] = sinAoA;
389 s->orient[3] = 0; s->orient[4] = 1; s->orient[5] = 0;
390 s->orient[6] = -sinAoA; s->orient[7] = 0; s->orient[8] = cosAoA;
392 s->v[0] = speed; s->v[1] = 0; s->v[2] = 0;
396 s->pos[i] = s->rot[i] = s->acc[i] = s->racc[i] = 0;
398 // Put us 1m above the origin, or else the gravity computation in
403 void Airplane::addContactPoint(float* pos)
405 float* cp = new float[3];
412 float Airplane::compileWing(Wing* w)
414 // The tip of the wing is a contact point
417 addContactPoint(tip);
418 if(w->isMirrored()) {
420 addContactPoint(tip);
423 // Make sure it's initialized. The surfaces will pop out with
424 // total drag coefficients equal to their areas, which is what we
430 for(i=0; i<w->numSurfaces(); i++) {
431 Surface* s = (Surface*)w->getSurface(i);
433 float td = s->getTotalDrag();
436 _model.addSurface(s);
438 float mass = w->getSurfaceWeight(i);
439 mass = mass * Math::sqrt(mass);
442 _model.getBody()->addMass(mass, pos);
448 float Airplane::compileRotor(Rotor* r)
450 // Todo: add rotor to model!!!
451 // Todo: calc and add mass!!!
457 for(i=0; i<r->numRotorparts(); i++) {
458 Rotorpart* s = (Rotorpart*)r->getRotorpart(i);
460 _model.addRotorpart(s);
462 float mass = s->getWeight();
463 mass = mass * Math::sqrt(mass);
466 _model.getBody()->addMass(mass, pos);
470 for(i=0; i<r->numRotorblades(); i++) {
471 Rotorblade* b = (Rotorblade*)r->getRotorblade(i);
473 _model.addRotorblade(b);
475 float mass = b->getWeight();
476 mass = mass * Math::sqrt(mass);
479 _model.getBody()->addMass(mass, pos);
485 float Airplane::compileFuselage(Fuselage* f)
487 // The front and back are contact points
488 addContactPoint(f->front);
489 addContactPoint(f->back);
493 Math::sub3(f->front, f->back, fwd);
494 float len = Math::mag3(fwd);
495 float wid = f->width;
496 int segs = (int)Math::ceil(len/wid);
497 float segWgt = len*wid/segs;
499 for(j=0; j<segs; j++) {
500 float frac = (j+0.5f) / segs;
504 scale = f->taper+(1-f->taper) * (frac / f->mid);
506 scale = f->taper+(1-f->taper) * (frac - f->mid) / (1 - f->mid);
510 Math::mul3(frac, fwd, pos);
511 Math::add3(f->back, pos, pos);
513 // _Mass_ weighting goes as surface area^(3/2)
514 float mass = scale*segWgt * Math::sqrt(scale*segWgt);
515 _model.getBody()->addMass(mass, pos);
518 // Make a Surface too
519 Surface* s = new Surface();
521 float sideDrag = len/wid;
522 s->setYDrag(sideDrag);
523 s->setZDrag(sideDrag);
524 s->setTotalDrag(scale*segWgt);
526 // FIXME: fails for fuselages aligned along the Y axis
528 float *x=o, *y=o+3, *z=o+6; // nicknames for the axes
530 y[0] = 0; y[1] = 1; y[2] = 0;
531 Math::cross3(x, y, z);
533 Math::cross3(z, x, y);
534 s->setOrientation(o);
536 _model.addSurface(s);
542 // FIXME: should probably add a mass for the gear, too
543 void Airplane::compileGear(GearRec* gr)
547 // Make a Surface object for the aerodynamic behavior
548 Surface* s = new Surface();
551 // Put the surface at the half-way point on the gear strut, give
552 // it a drag coefficient equal to a square of the same dimension
553 // (gear are really draggy) and make it symmetric. Assume that
554 // the "length" of the gear is 3x the compression distance
555 float pos[3], cmp[3];
556 g->getCompression(cmp);
557 float length = 3 * Math::mag3(cmp);
559 Math::mul3(0.5, cmp, cmp);
560 Math::add3(pos, cmp, pos);
563 s->setTotalDrag(length*length);
566 _model.addSurface(s);
570 void Airplane::compileContactPoints()
572 // Figure it will compress by 20cm
575 comp[0] = 0; comp[1] = 0; comp[2] = DIST;
577 // Give it a spring constant such that at full compression it will
578 // hold up 10 times the planes mass. That's about right. Yeah.
579 float mass = _model.getBody()->getTotalMass();
580 float spring = (1/DIST) * 9.8f * 10.0f * mass;
581 float damp = 2 * Math::sqrt(spring * mass);
584 for(i=0; i<_contacts.size(); i++) {
585 float *cp = (float*)_contacts.get(i);
587 Gear* g = new Gear();
590 g->setCompression(comp);
591 g->setSpring(spring);
596 g->setStaticFriction(0.6f);
597 g->setDynamicFriction(0.5f);
603 void Airplane::compile()
606 ground[0] = 0; ground[1] = 0; ground[2] = 1;
607 _model.setGroundPlane(ground, -100000);
609 RigidBody* body = _model.getBody();
610 int firstMass = body->numMasses();
612 // Generate the point masses for the plane. Just use unitless
613 // numbers for a first pass, then go back through and rescale to
614 // make the weight right.
619 aeroWgt += compileWing(_wing);
621 aeroWgt += compileWing(_tail);
623 for(i=0; i<_vstabs.size(); i++)
624 aeroWgt += compileWing((Wing*)_vstabs.get(i));
625 for(i=0; i<_rotors.size(); i++)
626 aeroWgt += compileRotor((Rotor*)_rotors.get(i));
629 for(i=0; i<_fuselages.size(); i++)
630 aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i));
632 // Count up the absolute weight we have
633 float nonAeroWgt = _ballast;
634 for(i=0; i<_thrusters.size(); i++)
635 nonAeroWgt += ((ThrustRec*)_thrusters.get(i))->mass;
637 // Rescale to the specified empty weight
638 float wscale = (_emptyWeight-nonAeroWgt)/aeroWgt;
639 for(i=firstMass; i<body->numMasses(); i++)
640 body->setMass(i, body->getMass(i)*wscale);
642 // Add the thruster masses
643 for(i=0; i<_thrusters.size(); i++) {
644 ThrustRec* t = (ThrustRec*)_thrusters.get(i);
645 body->addMass(t->mass, t->cg);
648 // Add the tanks, empty for now.
650 for(i=0; i<_tanks.size(); i++) {
651 Tank* t = (Tank*)_tanks.get(i);
652 t->handle = body->addMass(0, t->pos);
655 _cruiseWeight = _emptyWeight + totalFuel*0.5f;
656 _approachWeight = _emptyWeight + totalFuel*0.2f;
660 // Add surfaces for the landing gear.
661 for(i=0; i<_gears.size(); i++)
662 compileGear((GearRec*)_gears.get(i));
664 // The Thruster objects
665 for(i=0; i<_thrusters.size(); i++) {
666 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
667 tr->handle = _model.addThruster(tr->thruster);
674 gespan = _wing->getGroundEffect(gepos);
675 _model.setGroundEffect(gepos, gespan, 0.15f);
678 if(_wing && _tail) solve();
679 else solveHelicopter();
681 // Do this after solveGear, because it creates "gear" objects that
682 // we don't want to affect.
683 compileContactPoints();
686 void Airplane::solveGear()
689 _model.getBody()->getCG(cg);
691 // Calculate spring constant weightings for the gear. Weight by
692 // the inverse of the distance to the c.g. in the XY plane, which
693 // should be correct for most gear arrangements. Add 50cm of
694 // "buffer" to keep things from blowing up with aircraft with a
695 // single gear very near the c.g. (AV-8, for example).
698 for(i=0; i<_gears.size(); i++) {
699 GearRec* gr = (GearRec*)_gears.get(i);
702 Math::sub3(cg, pos, pos);
703 gr->wgt = 1.0f/(0.5f+Math::sqrt(pos[0]*pos[0] + pos[1]*pos[1]));
707 // Renormalize so they sum to 1
708 for(i=0; i<_gears.size(); i++)
709 ((GearRec*)_gears.get(i))->wgt /= total;
711 // The force at max compression should be sufficient to stop a
712 // plane moving downwards at 2x the approach descent rate. Assume
713 // a 3 degree approach.
714 float descentRate = 2.0f*_approachSpeed/19.1f;
716 // Spread the kinetic energy according to the gear weights. This
717 // will results in an equal compression fraction (not distance) of
719 float energy = 0.5f*_approachWeight*descentRate*descentRate;
721 for(i=0; i<_gears.size(); i++) {
722 GearRec* gr = (GearRec*)_gears.get(i);
723 float e = energy * gr->wgt;
725 gr->gear->getCompression(comp);
726 float len = Math::mag3(comp);
728 // Energy in a spring: e = 0.5 * k * len^2
729 float k = 2 * e / (len*len);
731 gr->gear->setSpring(k * gr->gear->getSpring());
733 // Critically damped (too damped, too!)
734 gr->gear->setDamping(2*Math::sqrt(k*_approachWeight*gr->wgt)
735 * gr->gear->getDamping());
737 // These are pretty generic
738 gr->gear->setStaticFriction(0.8f);
739 gr->gear->setDynamicFriction(0.7f);
743 void Airplane::initEngines()
745 for(int i=0; i<_thrusters.size(); i++) {
746 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
747 tr->thruster->init();
751 void Airplane::stabilizeThrust()
754 for(i=0; i<_thrusters.size(); i++)
755 _model.getThruster(i)->stabilize();
758 void Airplane::runCruise()
760 setupState(_cruiseAoA, _cruiseSpeed, &_cruiseState);
761 _model.setState(&_cruiseState);
762 _model.setAir(_cruiseP, _cruiseT,
763 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
765 // The control configuration
768 for(i=0; i<_cruiseControls.size(); i++) {
769 Control* c = (Control*)_cruiseControls.get(i);
770 _controls.setInput(c->control, c->val);
772 _controls.applyControls(1000000); // Huge dt value
776 Math::mul3(-1, _cruiseState.v, wind);
777 Math::vmul33(_cruiseState.orient, wind, wind);
779 setFuelFraction(_cruiseFuel);
781 // Set up the thruster parameters and iterate until the thrust
783 for(i=0; i<_thrusters.size(); i++) {
784 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
786 t->setAir(_cruiseP, _cruiseT,
787 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
793 // Precompute thrust in the model, and calculate aerodynamic forces
794 _model.getBody()->recalc();
795 _model.getBody()->reset();
796 _model.initIteration();
797 _model.calcForces(&_cruiseState);
800 void Airplane::runApproach()
802 setupState(_approachAoA, _approachSpeed, &_approachState);
803 _model.setState(&_approachState);
804 _model.setAir(_approachP, _approachT,
805 Atmosphere::calcStdDensity(_approachP, _approachT));
807 // The control configuration
810 for(i=0; i<_approachControls.size(); i++) {
811 Control* c = (Control*)_approachControls.get(i);
812 _controls.setInput(c->control, c->val);
814 _controls.applyControls(1000000);
818 Math::mul3(-1, _approachState.v, wind);
819 Math::vmul33(_approachState.orient, wind, wind);
821 // Approach is by convention at 20% tank capacity
822 setFuelFraction(_approachFuel);
824 // Run the thrusters until they get to a stable setting. FIXME:
825 // this is lots of wasted work.
826 for(i=0; i<_thrusters.size(); i++) {
827 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
829 t->setAir(_approachP, _approachT,
830 Atmosphere::calcStdDensity(_approachP, _approachT));
836 // Precompute thrust in the model, and calculate aerodynamic forces
837 _model.getBody()->recalc();
838 _model.getBody()->reset();
839 _model.initIteration();
840 _model.calcForces(&_approachState);
843 void Airplane::applyDragFactor(float factor)
845 float applied = Math::pow(factor, SOLVE_TWEAK);
846 _dragFactor *= applied;
848 _wing->setDragScale(_wing->getDragScale() * applied);
850 _tail->setDragScale(_tail->getDragScale() * applied);
852 for(i=0; i<_vstabs.size(); i++) {
853 Wing* w = (Wing*)_vstabs.get(i);
854 w->setDragScale(w->getDragScale() * applied);
856 for(i=0; i<_surfs.size(); i++) {
857 Surface* s = (Surface*)_surfs.get(i);
858 s->setTotalDrag(s->getTotalDrag() * applied);
862 void Airplane::applyLiftRatio(float factor)
864 float applied = Math::pow(factor, SOLVE_TWEAK);
865 _liftRatio *= applied;
867 _wing->setLiftRatio(_wing->getLiftRatio() * applied);
869 _tail->setLiftRatio(_tail->getLiftRatio() * applied);
871 for(i=0; i<_vstabs.size(); i++) {
872 Wing* w = (Wing*)_vstabs.get(i);
873 w->setLiftRatio(w->getLiftRatio() * applied);
877 float Airplane::clamp(float val, float min, float max)
879 if(val < min) return min;
880 if(val > max) return max;
884 float Airplane::normFactor(float f)
891 void Airplane::solve()
893 static const float ARCMIN = 0.0002909f;
896 _solutionIterations = 0;
900 if(_solutionIterations++ > 10000) {
901 _failureMsg = "Solution failed to converge after 10000 iterations";
905 // Run an iteration at cruise, and extract the needed numbers:
908 _model.getThrust(tmp);
909 float thrust = tmp[0];
911 _model.getBody()->getAccel(tmp);
912 Math::tmul33(_cruiseState.orient, tmp, tmp);
913 float xforce = _cruiseWeight * tmp[0];
914 float clift0 = _cruiseWeight * tmp[2];
916 _model.getBody()->getAngularAccel(tmp);
917 Math::tmul33(_cruiseState.orient, tmp, tmp);
918 float pitch0 = tmp[1];
920 // Run an approach iteration, and do likewise
923 _model.getBody()->getAngularAccel(tmp);
924 Math::tmul33(_approachState.orient, tmp, tmp);
925 double apitch0 = tmp[1];
927 _model.getBody()->getAccel(tmp);
928 Math::tmul33(_approachState.orient, tmp, tmp);
929 float alift = _approachWeight * tmp[2];
931 // Modify the cruise AoA a bit to get a derivative
932 _cruiseAoA += ARCMIN;
934 _cruiseAoA -= ARCMIN;
936 _model.getBody()->getAccel(tmp);
937 Math::tmul33(_cruiseState.orient, tmp, tmp);
938 float clift1 = _cruiseWeight * tmp[2];
940 // Do the same with the tail incidence
941 _tail->setIncidence(_tailIncidence + ARCMIN);
943 _tail->setIncidence(_tailIncidence);
945 _model.getBody()->getAngularAccel(tmp);
946 Math::tmul33(_cruiseState.orient, tmp, tmp);
947 float pitch1 = tmp[1];
950 float awgt = 9.8f * _approachWeight;
952 float dragFactor = thrust / (thrust-xforce);
953 float liftFactor = awgt / (awgt+alift);
954 float aoaDelta = -clift0 * (ARCMIN/(clift1-clift0));
955 float tailDelta = -pitch0 * (ARCMIN/(pitch1-pitch0));
958 if(dragFactor <= 0 || liftFactor <= 0)
961 // And the elevator control in the approach. This works just
962 // like the tail incidence computation (it's solving for the
963 // same thing -- pitching moment -- by diddling a different
965 const float ELEVDIDDLE = 0.001f;
966 _approachElevator.val += ELEVDIDDLE;
968 _approachElevator.val -= ELEVDIDDLE;
970 _model.getBody()->getAngularAccel(tmp);
971 Math::tmul33(_approachState.orient, tmp, tmp);
972 double apitch1 = tmp[1];
973 float elevDelta = -apitch0 * (ELEVDIDDLE/(apitch1-apitch0));
975 // Now apply the values we just computed. Note that the
976 // "minor" variables are deferred until we get the lift/drag
977 // numbers in the right ballpark.
979 applyDragFactor(dragFactor);
980 applyLiftRatio(liftFactor);
982 // DON'T do the following until the above are sane
983 if(normFactor(dragFactor) > STHRESH*1.0001
984 || normFactor(liftFactor) > STHRESH*1.0001)
989 // OK, now we can adjust the minor variables:
990 _cruiseAoA += SOLVE_TWEAK*aoaDelta;
991 _tailIncidence += SOLVE_TWEAK*tailDelta;
993 _cruiseAoA = clamp(_cruiseAoA, -0.175f, 0.175f);
994 _tailIncidence = clamp(_tailIncidence, -0.175f, 0.175f);
996 if(abs(xforce/_cruiseWeight) < STHRESH*0.0001 &&
997 abs(alift/_approachWeight) < STHRESH*0.0001 &&
998 abs(aoaDelta) < STHRESH*.000017 &&
999 abs(tailDelta) < STHRESH*.000017)
1001 // If this finaly value is OK, then we're all done
1002 if(abs(elevDelta) < STHRESH*0.0001)
1005 // Otherwise, adjust and do the next iteration
1006 _approachElevator.val += SOLVE_TWEAK * elevDelta;
1007 if(abs(_approachElevator.val) > 1) {
1008 _failureMsg = "Insufficient elevator to trim for approach";
1014 if(_dragFactor < 1e-06 || _dragFactor > 1e6) {
1015 _failureMsg = "Drag factor beyond reasonable bounds.";
1017 } else if(_liftRatio < 1e-04 || _liftRatio > 1e4) {
1018 _failureMsg = "Lift ratio beyond reasonable bounds.";
1020 } else if(Math::abs(_cruiseAoA) >= .17453293) {
1021 _failureMsg = "Cruise AoA > 10 degrees";
1023 } else if(Math::abs(_tailIncidence) >= .17453293) {
1024 _failureMsg = "Tail incidence > 10 degrees";
1029 void Airplane::solveHelicopter()
1031 _solutionIterations = 0;
1034 applyDragFactor(Math::pow(15.7/1000, 1/SOLVE_TWEAK));
1035 applyLiftRatio(Math::pow(104, 1/SOLVE_TWEAK));
1036 setupState(0,0, &_cruiseState);
1037 _model.setState(&_cruiseState);
1039 _model.getBody()->reset();
1042 }; // namespace yasim