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)
175 // The zero AoA will become a calculated stall AoA in compile()
176 setApproach(speed, altitude, 0);
179 void Airplane::setApproach(float speed, float altitude, float aoa)
181 _approachSpeed = speed;
182 _approachP = Atmosphere::getStdPressure(altitude);
183 _approachT = Atmosphere::getStdTemperature(altitude);
187 void Airplane::setCruise(float speed, float altitude)
189 _cruiseSpeed = speed;
190 _cruiseP = Atmosphere::getStdPressure(altitude);
191 _cruiseT = Atmosphere::getStdTemperature(altitude);
196 void Airplane::setElevatorControl(int control)
198 _approachElevator.control = control;
199 _approachElevator.val = 0;
200 _approachControls.add(&_approachElevator);
203 void Airplane::addApproachControl(int control, float val)
205 Control* c = new Control();
206 c->control = control;
208 _approachControls.add(c);
211 void Airplane::addCruiseControl(int control, float val)
213 Control* c = new Control();
214 c->control = control;
216 _cruiseControls.add(c);
219 int Airplane::numTanks()
221 return _tanks.size();
224 float Airplane::getFuel(int tank)
226 return ((Tank*)_tanks.get(tank))->fill;
229 float Airplane::getFuelDensity(int tank)
231 return ((Tank*)_tanks.get(tank))->density;
234 float Airplane::getTankCapacity(int tank)
236 return ((Tank*)_tanks.get(tank))->cap;
239 void Airplane::setWeight(float weight)
241 _emptyWeight = weight;
244 void Airplane::setWing(Wing* wing)
249 void Airplane::setTail(Wing* tail)
254 void Airplane::addVStab(Wing* vstab)
259 void Airplane::addRotor(Rotor* rotor)
264 void Airplane::addFuselage(float* front, float* back, float width,
265 float taper, float mid)
267 Fuselage* f = new Fuselage();
270 f->front[i] = front[i];
271 f->back[i] = back[i];
279 int Airplane::addTank(float* pos, float cap, float density)
281 Tank* t = new Tank();
283 for(i=0; i<3; i++) t->pos[i] = pos[i];
286 t->density = density;
287 t->handle = 0xffffffff;
288 return _tanks.add(t);
291 void Airplane::addGear(Gear* gear)
293 GearRec* g = new GearRec();
299 void Airplane::addThruster(Thruster* thruster, float mass, float* cg)
301 ThrustRec* t = new ThrustRec();
302 t->thruster = thruster;
305 for(i=0; i<3; i++) t->cg[i] = cg[i];
309 void Airplane::addBallast(float* pos, float mass)
311 _model.getBody()->addMass(mass, pos);
315 int Airplane::addWeight(float* pos, float size)
317 WeightRec* wr = new WeightRec();
318 wr->handle = _model.getBody()->addMass(0, pos);
320 wr->surf = new Surface();
321 wr->surf->setPosition(pos);
322 wr->surf->setTotalDrag(size*size);
323 _model.addSurface(wr->surf);
324 _surfs.add(wr->surf);
326 return _weights.add(wr);
329 void Airplane::setWeight(int handle, float mass)
331 WeightRec* wr = (WeightRec*)_weights.get(handle);
333 _model.getBody()->setMass(wr->handle, mass);
335 // Kill the aerodynamic drag if the mass is exactly zero. This is
336 // how we simulate droppable stores.
338 wr->surf->setXDrag(0);
339 wr->surf->setYDrag(0);
340 wr->surf->setZDrag(0);
342 wr->surf->setXDrag(1);
343 wr->surf->setYDrag(1);
344 wr->surf->setZDrag(1);
348 void Airplane::setFuelFraction(float frac)
351 for(i=0; i<_tanks.size(); i++) {
352 Tank* t = (Tank*)_tanks.get(i);
353 t->fill = frac * t->cap;
354 _model.getBody()->setMass(t->handle, t->cap * frac);
358 float Airplane::getDragCoefficient()
363 float Airplane::getLiftRatio()
368 float Airplane::getCruiseAoA()
373 float Airplane::getTailIncidence()
375 return _tailIncidence;
378 char* Airplane::getFailureMsg()
383 int Airplane::getSolutionIterations()
385 return _solutionIterations;
388 void Airplane::setupState(float aoa, float speed, State* s)
390 float cosAoA = Math::cos(aoa);
391 float sinAoA = Math::sin(aoa);
392 s->orient[0] = cosAoA; s->orient[1] = 0; s->orient[2] = sinAoA;
393 s->orient[3] = 0; s->orient[4] = 1; s->orient[5] = 0;
394 s->orient[6] = -sinAoA; s->orient[7] = 0; s->orient[8] = cosAoA;
396 s->v[0] = speed; s->v[1] = 0; s->v[2] = 0;
400 s->pos[i] = s->rot[i] = s->acc[i] = s->racc[i] = 0;
402 // Put us 1m above the origin, or else the gravity computation in
407 void Airplane::addContactPoint(float* pos)
409 float* cp = new float[3];
416 float Airplane::compileWing(Wing* w)
418 // The tip of the wing is a contact point
421 addContactPoint(tip);
422 if(w->isMirrored()) {
424 addContactPoint(tip);
427 // Make sure it's initialized. The surfaces will pop out with
428 // total drag coefficients equal to their areas, which is what we
434 for(i=0; i<w->numSurfaces(); i++) {
435 Surface* s = (Surface*)w->getSurface(i);
437 float td = s->getTotalDrag();
440 _model.addSurface(s);
442 float mass = w->getSurfaceWeight(i);
443 mass = mass * Math::sqrt(mass);
446 _model.getBody()->addMass(mass, pos);
452 float Airplane::compileRotor(Rotor* r)
454 // Todo: add rotor to model!!!
455 // Todo: calc and add mass!!!
461 for(i=0; i<r->numRotorparts(); i++) {
462 Rotorpart* s = (Rotorpart*)r->getRotorpart(i);
464 _model.addRotorpart(s);
466 float mass = s->getWeight();
467 mass = mass * Math::sqrt(mass);
470 _model.getBody()->addMass(mass, pos);
474 for(i=0; i<r->numRotorblades(); i++) {
475 Rotorblade* b = (Rotorblade*)r->getRotorblade(i);
477 _model.addRotorblade(b);
479 float mass = b->getWeight();
480 mass = mass * Math::sqrt(mass);
483 _model.getBody()->addMass(mass, pos);
489 float Airplane::compileFuselage(Fuselage* f)
491 // The front and back are contact points
492 addContactPoint(f->front);
493 addContactPoint(f->back);
497 Math::sub3(f->front, f->back, fwd);
498 float len = Math::mag3(fwd);
499 float wid = f->width;
500 int segs = (int)Math::ceil(len/wid);
501 float segWgt = len*wid/segs;
503 for(j=0; j<segs; j++) {
504 float frac = (j+0.5f) / segs;
508 scale = f->taper+(1-f->taper) * (frac / f->mid);
510 scale = f->taper+(1-f->taper) * (frac - f->mid) / (1 - f->mid);
514 Math::mul3(frac, fwd, pos);
515 Math::add3(f->back, pos, pos);
517 // _Mass_ weighting goes as surface area^(3/2)
518 float mass = scale*segWgt * Math::sqrt(scale*segWgt);
519 _model.getBody()->addMass(mass, pos);
522 // Make a Surface too
523 Surface* s = new Surface();
525 float sideDrag = len/wid;
526 s->setYDrag(sideDrag);
527 s->setZDrag(sideDrag);
528 s->setTotalDrag(scale*segWgt);
530 // FIXME: fails for fuselages aligned along the Y axis
532 float *x=o, *y=o+3, *z=o+6; // nicknames for the axes
534 y[0] = 0; y[1] = 1; y[2] = 0;
535 Math::cross3(x, y, z);
537 Math::cross3(z, x, y);
538 s->setOrientation(o);
540 _model.addSurface(s);
546 // FIXME: should probably add a mass for the gear, too
547 void Airplane::compileGear(GearRec* gr)
551 // Make a Surface object for the aerodynamic behavior
552 Surface* s = new Surface();
555 // Put the surface at the half-way point on the gear strut, give
556 // it a drag coefficient equal to a square of the same dimension
557 // (gear are really draggy) and make it symmetric. Assume that
558 // the "length" of the gear is 3x the compression distance
559 float pos[3], cmp[3];
560 g->getCompression(cmp);
561 float length = 3 * Math::mag3(cmp);
563 Math::mul3(0.5, cmp, cmp);
564 Math::add3(pos, cmp, pos);
567 s->setTotalDrag(length*length);
570 _model.addSurface(s);
574 void Airplane::compileContactPoints()
576 // Figure it will compress by 20cm
579 comp[0] = 0; comp[1] = 0; comp[2] = DIST;
581 // Give it a spring constant such that at full compression it will
582 // hold up 10 times the planes mass. That's about right. Yeah.
583 float mass = _model.getBody()->getTotalMass();
584 float spring = (1/DIST) * 9.8f * 10.0f * mass;
585 float damp = 2 * Math::sqrt(spring * mass);
588 for(i=0; i<_contacts.size(); i++) {
589 float *cp = (float*)_contacts.get(i);
591 Gear* g = new Gear();
594 g->setCompression(comp);
595 g->setSpring(spring);
600 g->setStaticFriction(0.6f);
601 g->setDynamicFriction(0.5f);
607 void Airplane::compile()
610 ground[0] = 0; ground[1] = 0; ground[2] = 1;
611 _model.setGroundPlane(ground, -100000);
613 RigidBody* body = _model.getBody();
614 int firstMass = body->numMasses();
616 // Generate the point masses for the plane. Just use unitless
617 // numbers for a first pass, then go back through and rescale to
618 // make the weight right.
623 aeroWgt += compileWing(_wing);
625 aeroWgt += compileWing(_tail);
627 for(i=0; i<_vstabs.size(); i++)
628 aeroWgt += compileWing((Wing*)_vstabs.get(i));
629 for(i=0; i<_rotors.size(); i++)
630 aeroWgt += compileRotor((Rotor*)_rotors.get(i));
633 for(i=0; i<_fuselages.size(); i++)
634 aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i));
636 // Count up the absolute weight we have
637 float nonAeroWgt = _ballast;
638 for(i=0; i<_thrusters.size(); i++)
639 nonAeroWgt += ((ThrustRec*)_thrusters.get(i))->mass;
641 // Rescale to the specified empty weight
642 float wscale = (_emptyWeight-nonAeroWgt)/aeroWgt;
643 for(i=firstMass; i<body->numMasses(); i++)
644 body->setMass(i, body->getMass(i)*wscale);
646 // Add the thruster masses
647 for(i=0; i<_thrusters.size(); i++) {
648 ThrustRec* t = (ThrustRec*)_thrusters.get(i);
649 body->addMass(t->mass, t->cg);
652 // Add the tanks, empty for now.
654 for(i=0; i<_tanks.size(); i++) {
655 Tank* t = (Tank*)_tanks.get(i);
656 t->handle = body->addMass(0, t->pos);
659 _cruiseWeight = _emptyWeight + totalFuel*0.5f;
660 _approachWeight = _emptyWeight + totalFuel*0.2f;
664 // Add surfaces for the landing gear.
665 for(i=0; i<_gears.size(); i++)
666 compileGear((GearRec*)_gears.get(i));
668 // The Thruster objects
669 for(i=0; i<_thrusters.size(); i++) {
670 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
671 tr->handle = _model.addThruster(tr->thruster);
678 gespan = _wing->getGroundEffect(gepos);
679 _model.setGroundEffect(gepos, gespan, 0.15f);
682 if(_wing && _tail) solve();
683 else solveHelicopter();
685 // Do this after solveGear, because it creates "gear" objects that
686 // we don't want to affect.
687 compileContactPoints();
690 void Airplane::solveGear()
693 _model.getBody()->getCG(cg);
695 // Calculate spring constant weightings for the gear. Weight by
696 // the inverse of the distance to the c.g. in the XY plane, which
697 // should be correct for most gear arrangements. Add 50cm of
698 // "buffer" to keep things from blowing up with aircraft with a
699 // single gear very near the c.g. (AV-8, for example).
702 for(i=0; i<_gears.size(); i++) {
703 GearRec* gr = (GearRec*)_gears.get(i);
706 Math::sub3(cg, pos, pos);
707 gr->wgt = 1.0f/(0.5f+Math::sqrt(pos[0]*pos[0] + pos[1]*pos[1]));
711 // Renormalize so they sum to 1
712 for(i=0; i<_gears.size(); i++)
713 ((GearRec*)_gears.get(i))->wgt /= total;
715 // The force at max compression should be sufficient to stop a
716 // plane moving downwards at 2x the approach descent rate. Assume
717 // a 3 degree approach.
718 float descentRate = 2.0f*_approachSpeed/19.1f;
720 // Spread the kinetic energy according to the gear weights. This
721 // will results in an equal compression fraction (not distance) of
723 float energy = 0.5f*_approachWeight*descentRate*descentRate;
725 for(i=0; i<_gears.size(); i++) {
726 GearRec* gr = (GearRec*)_gears.get(i);
727 float e = energy * gr->wgt;
729 gr->gear->getCompression(comp);
730 float len = Math::mag3(comp);
732 // Energy in a spring: e = 0.5 * k * len^2
733 float k = 2 * e / (len*len);
735 gr->gear->setSpring(k * gr->gear->getSpring());
737 // Critically damped (too damped, too!)
738 gr->gear->setDamping(2*Math::sqrt(k*_approachWeight*gr->wgt)
739 * gr->gear->getDamping());
741 // These are pretty generic
742 gr->gear->setStaticFriction(0.8f);
743 gr->gear->setDynamicFriction(0.7f);
747 void Airplane::initEngines()
749 for(int i=0; i<_thrusters.size(); i++) {
750 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
751 tr->thruster->init();
755 void Airplane::stabilizeThrust()
758 for(i=0; i<_thrusters.size(); i++)
759 _model.getThruster(i)->stabilize();
762 void Airplane::runCruise()
764 setupState(_cruiseAoA, _cruiseSpeed, &_cruiseState);
765 _model.setState(&_cruiseState);
766 _model.setAir(_cruiseP, _cruiseT,
767 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
769 // The control configuration
772 for(i=0; i<_cruiseControls.size(); i++) {
773 Control* c = (Control*)_cruiseControls.get(i);
774 _controls.setInput(c->control, c->val);
776 _controls.applyControls(1000000); // Huge dt value
780 Math::mul3(-1, _cruiseState.v, wind);
781 Math::vmul33(_cruiseState.orient, wind, wind);
783 // Cruise is by convention at 50% tank capacity
784 setFuelFraction(0.5);
786 // Set up the thruster parameters and iterate until the thrust
788 for(i=0; i<_thrusters.size(); i++) {
789 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
791 t->setAir(_cruiseP, _cruiseT,
792 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
798 // Precompute thrust in the model, and calculate aerodynamic forces
799 _model.getBody()->recalc();
800 _model.getBody()->reset();
801 _model.initIteration();
802 _model.calcForces(&_cruiseState);
805 void Airplane::runApproach()
807 setupState(_approachAoA, _approachSpeed, &_approachState);
808 _model.setState(&_approachState);
809 _model.setAir(_approachP, _approachT,
810 Atmosphere::calcStdDensity(_approachP, _approachT));
812 // The control configuration
815 for(i=0; i<_approachControls.size(); i++) {
816 Control* c = (Control*)_approachControls.get(i);
817 _controls.setInput(c->control, c->val);
819 _controls.applyControls(1000000);
823 Math::mul3(-1, _approachState.v, wind);
824 Math::vmul33(_approachState.orient, wind, wind);
826 // Approach is by convention at 20% tank capacity
827 setFuelFraction(0.2f);
829 // Run the thrusters until they get to a stable setting. FIXME:
830 // this is lots of wasted work.
831 for(i=0; i<_thrusters.size(); i++) {
832 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
834 t->setAir(_approachP, _approachT,
835 Atmosphere::calcStdDensity(_approachP, _approachT));
841 // Precompute thrust in the model, and calculate aerodynamic forces
842 _model.getBody()->recalc();
843 _model.getBody()->reset();
844 _model.initIteration();
845 _model.calcForces(&_approachState);
848 void Airplane::applyDragFactor(float factor)
850 float applied = Math::pow(factor, SOLVE_TWEAK);
851 _dragFactor *= applied;
853 _wing->setDragScale(_wing->getDragScale() * applied);
855 _tail->setDragScale(_tail->getDragScale() * applied);
857 for(i=0; i<_vstabs.size(); i++) {
858 Wing* w = (Wing*)_vstabs.get(i);
859 w->setDragScale(w->getDragScale() * applied);
861 for(i=0; i<_surfs.size(); i++) {
862 Surface* s = (Surface*)_surfs.get(i);
863 s->setTotalDrag(s->getTotalDrag() * applied);
867 void Airplane::applyLiftRatio(float factor)
869 float applied = Math::pow(factor, SOLVE_TWEAK);
870 _liftRatio *= applied;
872 _wing->setLiftRatio(_wing->getLiftRatio() * applied);
874 _tail->setLiftRatio(_tail->getLiftRatio() * applied);
876 for(i=0; i<_vstabs.size(); i++) {
877 Wing* w = (Wing*)_vstabs.get(i);
878 w->setLiftRatio(w->getLiftRatio() * applied);
882 float Airplane::clamp(float val, float min, float max)
884 if(val < min) return min;
885 if(val > max) return max;
889 float Airplane::normFactor(float f)
896 void Airplane::solve()
898 static const float ARCMIN = 0.0002909f;
901 _solutionIterations = 0;
905 if(_solutionIterations++ > 10000) {
906 _failureMsg = "Solution failed to converge after 10000 iterations";
910 // Run an iteration at cruise, and extract the needed numbers:
913 _model.getThrust(tmp);
914 float thrust = tmp[0];
916 _model.getBody()->getAccel(tmp);
917 Math::tmul33(_cruiseState.orient, tmp, tmp);
918 float xforce = _cruiseWeight * tmp[0];
919 float clift0 = _cruiseWeight * tmp[2];
921 _model.getBody()->getAngularAccel(tmp);
922 Math::tmul33(_cruiseState.orient, tmp, tmp);
923 float pitch0 = tmp[1];
925 // Run an approach iteration, and do likewise
928 _model.getBody()->getAngularAccel(tmp);
929 Math::tmul33(_approachState.orient, tmp, tmp);
930 double apitch0 = tmp[1];
932 _model.getBody()->getAccel(tmp);
933 Math::tmul33(_approachState.orient, tmp, tmp);
934 float alift = _approachWeight * tmp[2];
936 // Modify the cruise AoA a bit to get a derivative
937 _cruiseAoA += ARCMIN;
939 _cruiseAoA -= ARCMIN;
941 _model.getBody()->getAccel(tmp);
942 Math::tmul33(_cruiseState.orient, tmp, tmp);
943 float clift1 = _cruiseWeight * tmp[2];
945 // Do the same with the tail incidence
946 _tail->setIncidence(_tailIncidence + ARCMIN);
948 _tail->setIncidence(_tailIncidence);
950 _model.getBody()->getAngularAccel(tmp);
951 Math::tmul33(_cruiseState.orient, tmp, tmp);
952 float pitch1 = tmp[1];
955 float awgt = 9.8f * _approachWeight;
957 float dragFactor = thrust / (thrust-xforce);
958 float liftFactor = awgt / (awgt+alift);
959 float aoaDelta = -clift0 * (ARCMIN/(clift1-clift0));
960 float tailDelta = -pitch0 * (ARCMIN/(pitch1-pitch0));
963 if(dragFactor <= 0 || liftFactor <= 0)
966 // And the elevator control in the approach. This works just
967 // like the tail incidence computation (it's solving for the
968 // same thing -- pitching moment -- by diddling a different
970 const float ELEVDIDDLE = 0.001f;
971 _approachElevator.val += ELEVDIDDLE;
973 _approachElevator.val -= ELEVDIDDLE;
975 _model.getBody()->getAngularAccel(tmp);
976 Math::tmul33(_approachState.orient, tmp, tmp);
977 double apitch1 = tmp[1];
978 float elevDelta = -apitch0 * (ELEVDIDDLE/(apitch1-apitch0));
980 // Now apply the values we just computed. Note that the
981 // "minor" variables are deferred until we get the lift/drag
982 // numbers in the right ballpark.
984 applyDragFactor(dragFactor);
985 applyLiftRatio(liftFactor);
987 // DON'T do the following until the above are sane
988 if(normFactor(dragFactor) > STHRESH*1.0001
989 || normFactor(liftFactor) > STHRESH*1.0001)
994 // OK, now we can adjust the minor variables:
995 _cruiseAoA += SOLVE_TWEAK*aoaDelta;
996 _tailIncidence += SOLVE_TWEAK*tailDelta;
998 _cruiseAoA = clamp(_cruiseAoA, -0.175f, 0.175f);
999 _tailIncidence = clamp(_tailIncidence, -0.175f, 0.175f);
1001 if(abs(xforce/_cruiseWeight) < STHRESH*0.0001 &&
1002 abs(alift/_approachWeight) < STHRESH*0.0001 &&
1003 abs(aoaDelta) < STHRESH*.000017 &&
1004 abs(tailDelta) < STHRESH*.000017)
1006 // If this finaly value is OK, then we're all done
1007 if(abs(elevDelta) < STHRESH*0.0001)
1010 // Otherwise, adjust and do the next iteration
1011 _approachElevator.val += SOLVE_TWEAK * elevDelta;
1012 if(abs(_approachElevator.val) > 1) {
1013 _failureMsg = "Insufficient elevator to trim for approach";
1019 if(_dragFactor < 1e-06 || _dragFactor > 1e6) {
1020 _failureMsg = "Drag factor beyond reasonable bounds.";
1022 } else if(_liftRatio < 1e-04 || _liftRatio > 1e4) {
1023 _failureMsg = "Lift ratio beyond reasonable bounds.";
1025 } else if(Math::abs(_cruiseAoA) >= .17453293) {
1026 _failureMsg = "Cruise AoA > 10 degrees";
1028 } else if(Math::abs(_tailIncidence) >= .17453293) {
1029 _failureMsg = "Tail incidence > 10 degrees";
1034 void Airplane::solveHelicopter()
1036 _solutionIterations = 0;
1039 applyDragFactor(Math::pow(15.7/1000, 1/SOLVE_TWEAK));
1040 applyLiftRatio(Math::pow(104, 1/SOLVE_TWEAK));
1041 setupState(0,0, &_cruiseState);
1042 _model.setState(&_cruiseState);
1044 _model.getBody()->reset();
1047 }; // namespace yasim